1bit-game-jam/scripts/enemies/LivingArmor.cs

157 lines
2.6 KiB
C#

using Godot;
using System;
public partial class LivingArmor : CharacterBody2D
{
public enum State
{
Waiting,
Moving,
Attack
}
public enum SideFace
{
Left,
Up,
Right,
Down
}
[Export] public SideFace Facing = SideFace.Down;
[Export] public float MovingSpeed = 16f;
public State CurrentState
{
get => _state;
private set
{
_state = value;
_timeSinceState = 0;
switch (_state)
{
case State.Waiting:
break;
case State.Moving:
break;
case State.Attack:
break;
}
}
}
private State _state;
private float _timeSinceState;
private AnimatedSprite2D _sprite;
private bool _isLitUp;
public override void _Ready()
{
_sprite = (AnimatedSprite2D)FindChild("AnimatedSprite2D");
CurrentState = State.Waiting;
var animationName = "side_walk";
switch (Facing)
{
case SideFace.Left:
_sprite.FlipH = true;
break;
case SideFace.Right:
break;
case SideFace.Up:
animationName = "up_walk";
break;
case SideFace.Down:
animationName = "down_walk";
break;
}
_sprite.Play(animationName);
_sprite.Stop();
}
public override void _Process(double delta)
{
}
public override void _PhysicsProcess(double delta)
{
_timeSinceState += (float)delta;
switch (_state)
{
case State.Attack:
break;
case State.Waiting:
_sprite.Stop();
break;
case State.Moving:
var direction = (Player.Instance.Position - Position).Normalized();
Velocity = direction * MovingSpeed;
var animationName = "side_walk";
if (Velocity.Y > 0.001f)
animationName = "down_walk";
else if (Velocity.Y < 0.001f)
animationName = "up_walk";
if (Mathf.Abs(Velocity.X) >= Mathf.Abs(Velocity.Y))
animationName = "side_walk";
_sprite.FlipH = Velocity.X < 0.001f && animationName == "side_walk";
_sprite.Play(animationName);
MoveAndSlide();
//MoveAndCollide(direction);
break;
}
CheckIfLitUp();
}
private void _OnLightEntered(Area2D area)
{
if (area.GetParentOrNull<GameCamera>() is null)
return;
_isLitUp = true;
}
private void _OnPlayerCollision(Node2D body)
{
if (body is not Player player)
return;
if (CurrentState is State.Waiting)
return;
player.Kill(this);
}
private void _OnLightExited(Area2D area)
{
if (area.GetParentOrNull<GameCamera>() is null)
return;
_isLitUp = false;
}
private void _OnPlayerCollisionExited(Node2D body)
{
}
void CheckIfLitUp()
{
if (!_isLitUp)
{
CurrentState = State.Waiting;
return;
}
if (CurrentState is State.Moving or State.Attack)
return;
CurrentState = State.Moving;
}
}