using System; 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 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; } switch (item.CurrentType) { case ItemType.Apple: item.CurrentType = UpgradeManager.Is(BasicUpgrade.AppleJuice) ? ItemType.AppleJuice : ItemType.Apple; break; case ItemType.Grape: item.CurrentType = UpgradeManager.Is(BasicUpgrade.GrapeJuice) ? ItemType.GrapeJuice : ItemType.Grape; break; case ItemType.BananaSkin: item.CurrentType = UpgradeManager.Is(BasicUpgrade.BananaBacon) ? ItemType.BananaBacon : ItemType.BananaSkin; break; case ItemType.Kiwi: item.CurrentType = UpgradeManager.Is(BasicUpgrade.KiwiPresser) // BYPRODUCT NEEDED ? ItemType.KiwiJuice : ItemType.Kiwi; FruitSpawner.SpawnItem(ItemType.KiwiSeeds, transform.position); break; case ItemType.Mango: item.CurrentType = UpgradeManager.Is(BasicUpgrade.MangoJuice) ? ItemType.MangoJuice : ItemType.Mango; break; case ItemType.PitayaSkin: item.CurrentType = UpgradeManager.Is(BasicUpgrade.PitayaFoodDye) ? ItemType.PitayaFoodDye : ItemType.PitayaSkin; break; default: item.CurrentType = item.CurrentType; break; } } } } }