-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
95 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
Exiled.CustomModules/API/Features/CustomItems/Items/Candies/CandyTracker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="CandyTracker.cs" company="Exiled Team"> | ||
// Copyright (c) Exiled Team. All rights reserved. | ||
// Licensed under the CC BY-SA 3.0 license. | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
namespace Exiled.CustomModules.API.Features.CustomItems.Items.Candies | ||
{ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using Exiled.API.Features.Items; | ||
using Exiled.Events.EventArgs.Player; | ||
using Exiled.Events.EventArgs.Scp330; | ||
using UnityEngine; | ||
|
||
/// <summary> | ||
/// A custom tracker for candies. | ||
/// </summary> | ||
public class CandyTracker : ItemTracker | ||
{ | ||
/// <inheritdoc cref="Exiled.Events.Handlers.Scp330.OnInteractingScp330"/> | ||
internal void OnInteractingScp330(InteractingScp330EventArgs ev) | ||
{ | ||
if (!ev.Player.TryGetItems(x => x.Type == ItemType.SCP330, out IEnumerable<Item> items) || | ||
!items.Single().Is(out Scp330 scp330)) | ||
return; | ||
|
||
if (!scp330.TryGetComponent(out CandyBehaviour behaviour)) | ||
{ | ||
behaviour = scp330.AddComponent<CandyBehaviour>(); | ||
|
||
if (ev.Candy != behaviour.CandySettings.CandyType || Random.value * 100 <= behaviour.CandySettings.Weight) | ||
{ | ||
scp330.RemoveComponent<CandyBehaviour>(); | ||
return; | ||
} | ||
|
||
AddOrTrack(scp330); | ||
behaviour.TrackedCandies.Add(0); | ||
return; | ||
} | ||
|
||
if (ev.Candy != behaviour.CandySettings.CandyType || Random.value * 100 <= behaviour.CandySettings.Weight) | ||
return; | ||
|
||
behaviour.TrackedCandies.Add(scp330.Candies.Count); | ||
} | ||
|
||
/// <inheritdoc cref="Exiled.Events.Handlers.Player.OnItemAdded"/> | ||
internal void OnInternalItemAdded(ItemAddedEventArgs ev) | ||
{ | ||
if (IsTracked(ev.Pickup) && ev.Item.Is(out Scp330 scp330)) | ||
{ | ||
if (scp330.TryGetComponent(out CandyBehaviour behaviour)) | ||
{ | ||
behaviour.TrackedCandies.Add(scp330.Candies.Count - 1); | ||
} | ||
else | ||
{ | ||
behaviour = scp330.AddComponent<CandyBehaviour>(); | ||
behaviour.TrackedCandies.Add(0); | ||
} | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void SubscribeEvents() | ||
{ | ||
base.SubscribeEvents(); | ||
|
||
Exiled.Events.Handlers.Scp330.InteractingScp330 += OnInteractingScp330; | ||
Exiled.Events.Handlers.Player.ItemAdded += OnInternalItemAdded; | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void UnsubscribeEvents() | ||
{ | ||
base.UnsubscribeEvents(); | ||
|
||
Exiled.Events.Handlers.Scp330.InteractingScp330 += OnInteractingScp330; | ||
Exiled.Events.Handlers.Player.ItemAdded -= OnInternalItemAdded; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters