using Godot; using System; using System.Collections.Generic; using System.Linq; public partial class Spikes : Area2D { public enum State { Waiting, Opening, Closing } [Export] public bool Enabled = false; [Export] public float SpikesTimeout = 2f; [Export] public float StartOffset = 0f; private AnimatedSprite2D _sprite; private State _state = State.Waiting; private float _timeSinceState; private readonly List _bodiesOnButton = new List(); // Called when the node enters the scene tree for the first time. public override void _Ready() { _sprite = (AnimatedSprite2D)FindChild("AnimatedSprite2D"); _timeSinceState -= StartOffset; } // Called every frame. 'delta' is the elapsed time since the previous frame. public override void _Process(double delta) { } public override void _PhysicsProcess(double delta) { if (Enabled) { switch (_state) { case State.Waiting: _sprite.Play("default"); _timeSinceState += (float)delta; if (_timeSinceState > SpikesTimeout) { _state = State.Opening; _timeSinceState = 0; } break; case State.Opening: _sprite.Play("activated"); KillBodiesOnButton(); break; case State.Closing: _sprite.PlayBackwards("activated"); KillBodiesOnButton(); break; } } } private void KillBodiesOnButton() { foreach (var body in _bodiesOnButton) { switch (body) { case LivingArmor armor: armor.Kill(this); break; case Wretched wretched: wretched.Kill(this); break; case Player player: player.Kill(this); break; default: return; } } } private void _OnEntered(Node2D body) { // if (_state is State.Waiting) // { _bodiesOnButton.Add(body); // } // switch (body) // { // case Wretched wretched: // wretched.Kill(this); // break; // case Player player: // player.Kill(this); // break; // default: // return; // } } private void _OnExited(Node2D body) { _bodiesOnButton.Remove(body); } private void _WhenOpened() { switch (_state) { case State.Waiting: break; case State.Opening: _state = State.Closing; break; case State.Closing: _state = State.Waiting; break; } } private void On() { Enabled = true; _timeSinceState = -StartOffset; } private void Off() { _sprite.Stop(); Enabled = false; _state = State.Waiting; } }