Files
Fruitomation/Assets/Scripts/Game/Buildings/Automation/PresserBuilding.cs
2026-04-27 20:19:53 +01:00

89 lines
3.0 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 => ItemType.AppleJuice,
ItemType.Grape => ItemType.GrapeJuice,
ItemType.Kiwi => ItemType.KiwiJuice,
ItemType.PitayaSkin => ItemType.PitayaFoodDye,
var _ => item.CurrentType // Default
};
}
}
}
}