100 lines
3.5 KiB
C#
100 lines
3.5 KiB
C#
using System.Collections.Generic;
|
|
using Fruitomation.Game.Items;
|
|
using JetBrains.Annotations;
|
|
using UnityEngine.Scripting;
|
|
using Fruitomation.Global;
|
|
using UnityEngine;
|
|
using System.Linq;
|
|
|
|
namespace Fruitomation.Game
|
|
{
|
|
public class PresserBuilding : Building
|
|
{
|
|
[Header("Presser Specific")]
|
|
[SerializeField] private Animator PresserAnimator;
|
|
[SerializeField] private Collider2D TopCollider;
|
|
[SerializeField] private Collider2D BottomCollider;
|
|
[SerializeField] private TriggerDetector EffectTrigger;
|
|
|
|
private readonly HashSet<GameObject> CurrentContainedObjects = new();
|
|
|
|
private void Awake()
|
|
{
|
|
EffectTrigger.SetAction(other => CurrentContainedObjects.Add(other.gameObject),
|
|
TriggerType.Enter
|
|
);
|
|
|
|
EffectTrigger.SetAction(other => CurrentContainedObjects.Remove(other.gameObject),
|
|
TriggerType.Exit
|
|
);
|
|
|
|
EffectTrigger.SetAction(other =>
|
|
{
|
|
if (other.transform.parent.TryGetComponent(out Rigidbody2D body))
|
|
{
|
|
body.AddForce(Vector3.down * 5f, ForceMode2D.Force);
|
|
}
|
|
}, TriggerType.Stay);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (GameStateController.Is(GameState.Simulation))
|
|
{
|
|
if (PresserAnimator.speed == 0)
|
|
{
|
|
PresserAnimator.Play(0, 0, 0f); // Play from beginning
|
|
PresserAnimator.speed = 0.2f; // Playing
|
|
}
|
|
}
|
|
|
|
else
|
|
{
|
|
if (PresserAnimator.speed != 0)
|
|
{
|
|
PresserAnimator.Play(0, 0, 0f); // Jump back to default frame
|
|
PresserAnimator.speed = 0; // Paused
|
|
}
|
|
}
|
|
}
|
|
|
|
[Preserve, UsedImplicitly] public void OpenTop() => TopCollider.enabled = false;
|
|
[Preserve, UsedImplicitly] public void CloseTop() => TopCollider.enabled = true;
|
|
[Preserve, UsedImplicitly] public void OpenBottom() => BottomCollider.enabled = false;
|
|
[Preserve, UsedImplicitly] public void CloseBottom() => BottomCollider.enabled = true;
|
|
|
|
[Preserve, UsedImplicitly] public void Press()
|
|
{
|
|
GameObject[] gameObjects = CurrentContainedObjects.ToArray();
|
|
foreach (GameObject go in gameObjects)
|
|
{
|
|
bool isItem = go.transform.parent.TryGetComponent(out ItemBehaviour item);
|
|
if (!isItem)
|
|
{
|
|
return;
|
|
}
|
|
|
|
item.CurrentType = item.CurrentType switch
|
|
{
|
|
ItemType.Apple => UpgradeManager.Is(BasicUpgrade.AppleSlices)
|
|
? ItemType.AppleSlices
|
|
: ItemType.Apple,
|
|
|
|
ItemType.Grape => UpgradeManager.Is(BasicUpgrade.GrapeJuice)
|
|
? ItemType.GrapeJuice
|
|
: ItemType.Grape,
|
|
|
|
ItemType.Kiwi => UpgradeManager.Is(BasicUpgrade.KiwiPresser)
|
|
? ItemType.KiwiJuice
|
|
: ItemType.Kiwi,
|
|
|
|
ItemType.PitayaSkin => UpgradeManager.Is(BasicUpgrade.PitayaFoodDye)
|
|
? ItemType.PitayaFoodDye
|
|
: ItemType.PitayaSkin,
|
|
|
|
var _ => item.CurrentType // Default
|
|
};
|
|
}
|
|
}
|
|
}
|
|
} |