1bit-game-jam/scripts/enemies/Claw.cs
2023-08-19 15:23:08 +03:00

90 lines
1.6 KiB
C#

using Godot;
using System;
public partial class Claw : CharacterBody2D
{
[Export] public float MovingSpeed = 32f;
public enum State
{
Waiting,
Moving,
Prepare,
Attack
}
private State _state = State.Waiting;
private float _stateTimeout = 0;
private bool _isPlayerNearBy = false;
private AnimatedSprite2D _sprite;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_sprite = (AnimatedSprite2D)FindChild("AnimatedSprite2D");
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
public override void _PhysicsProcess(double delta)
{
_stateTimeout -= (float)delta;
switch (_state)
{
case State.Waiting:
break;
case State.Moving:
var direction = (Player.Instance.Position - Position).Normalized();
Velocity = direction * MovingSpeed;
_sprite.FlipH = Velocity.X < 0.001f;
_sprite.Play("default");
MoveAndSlide();
break;
case State.Prepare:
_sprite.Play("prepare");
if (_stateTimeout < 0)
{
_state = State.Attack;
_stateTimeout = 0.25f;
}
break;
case State.Attack:
_sprite.Play("attack");
if (_stateTimeout < 0)
{
_state = State.Moving;
if (_isPlayerNearBy)
Player.Instance.Kill(this);
}
break;
}
}
private void _OnEntered(Node2D body)
{
if (body is Player)
{
_state = State.Prepare;
_isPlayerNearBy = true;
_stateTimeout = 0.5f;
}
}
private void _OnExited(Node2D body)
{
_isPlayerNearBy = false;
}
public void Enable()
{
GD.Print("Boss enabled");
_state = State.Moving;
}
}