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

256 lines
4.6 KiB
C#

using Godot;
using System;
using System.Collections.Generic;
public partial class LivingArmor : CharacterBody2D
{
[Signal]
public delegate void KilledEventHandler();
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;
//private readonly List<WeakRef> _bodiesInSight = new List<WeakRef>();
private readonly List<Node2D> _bodiesInSight = new List<Node2D>();
private readonly List<Node2D> _bodiesNearBy = new List<Node2D>();
private Node2D _target = null;
private Vector2 _respawnPosition;
public override void _Ready()
{
_sprite = (AnimatedSprite2D)FindChild("AnimatedSprite2D");
_respawnPosition = Position;
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.Pause();
break;
case State.Moving:
foreach (var body in _bodiesNearBy)
{
switch (body)
{
case Wretched wretched:
wretched.Kill(this);
break;
case Player player:
player.Kill(this);
break;
}
}
SearchTarget();
switch (_target)
{
case Player player:
if (!player.Alive)
{
_bodiesInSight.Remove(player);
_target = null;
}
break;
case Wretched wretched:
if (!wretched.IsAlive)
{
_bodiesInSight.Remove(wretched);
_target = null;
}
break;
}
if (_target is null)
{
_state = State.Waiting;
return;
}
var direction = (_target.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 SearchTarget()
{
float targetDistance = -1;
foreach (var body in _bodiesInSight)
{
var distance = (body.Position - Position).Length();
GD.Print($"{body.Name}");
switch (body)
{
case Wretched wretched:
if (targetDistance < 0 || targetDistance > distance)
{
targetDistance = distance;
_target = wretched;
}
break;
case Player player:
if (_target is Wretched)
{
continue;
}
_target = player;
break;
}
}
}
private void _OnLightEntered(Area2D area)
{
if (area.GetParentOrNull<GameCamera>() is null)
return;
_isLitUp = true;
}
private void _OnPlayerCollision(Node2D body)
{
_bodiesNearBy.Add(body);
}
private void _OnLightExited(Area2D area)
{
if (area.GetParentOrNull<GameCamera>() is null)
return;
_isLitUp = false;
}
private void _OnPlayerCollisionExited(Node2D body)
{
_bodiesNearBy.Remove(body);
}
void CheckIfLitUp()
{
if (!_isLitUp)
{
CurrentState = State.Waiting;
return;
}
if (CurrentState is State.Moving or State.Attack)
return;
CurrentState = State.Moving;
}
private void _OnBodyEntered(Node2D body)
{
if (body is not Wretched and not Player)
return;
_bodiesInSight.Add(body);
}
private void _OnBodyExited(Node2D body)
{
if (body is not Wretched and not Player)
return;
if (body == _target)
{
_target = null;
}
_bodiesInSight.Remove(body);
}
public void Kill(Node2D killer)
{
GD.Print($"{this.Name} was killed by {killer.Name}");
EmitSignal(SignalName.Killed);
Position = _respawnPosition;
// QueueFree(); // TODO
}
}