1bit-game-jam/scripts/entities/Spikes.cs

111 lines
1.8 KiB
C#

using Godot;
using System;
public partial class Spikes : Area2D
{
public enum State
{
Waiting,
Opening,
Closing
}
[Export] public bool Enabled = false;
[Export] public float SpikesTimeout = 1f;
[Export] public float StartOffset = 0f;
private AnimatedSprite2D _sprite;
private State _state = State.Waiting;
private float _timeSinceState;
// 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.Stop();
_timeSinceState += (float)delta;
if (_timeSinceState > SpikesTimeout)
{
_state = State.Opening;
_timeSinceState = 0;
}
break;
case State.Opening:
_sprite.Play("default");
break;
case State.Closing:
_sprite.PlayBackwards("default");
break;
}
}
}
private void _OnEntered(Node2D body)
{
if (_state is State.Waiting)
return;
switch (body)
{
case Wretched wretched:
wretched.Kill(this);
break;
case Player player:
player.Kill(this);
break;
default:
return;
}
}
private void _OnExited(Node2D body)
{
// Replace with function 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;
}
}