using Godot; using System; public partial class Boss : CharacterBody2D { [Signal] public delegate void KilledEventHandler(); [Export] public int MaxHp = 6; public enum State { Default, Injured } private AnimatedSprite2D _sprite; private State _state = State.Default; private int _currentHp; private float _injureTimeout = Constants.BossInjureTimeout; // Called when the node enters the scene tree for the first time. public override void _Ready() { _sprite = (AnimatedSprite2D)FindChild("AnimatedSprite2D"); _currentHp = MaxHp; } // Called every frame. 'delta' is the elapsed time since the previous frame. public override void _Process(double delta) { } public override void _PhysicsProcess(double delta) { _injureTimeout -= (float)delta; switch (_state) { case State.Default: _sprite.Play("default"); break; case State.Injured: _sprite.Play("injured"); if (_injureTimeout < 0) { _state = State.Default; } break; } if (_currentHp <= 0) { EmitSignal(SignalName.Killed); } } private void _OnAttack(Node2D body) { if (body is LivingArmor armor) { _currentHp -= 1; _state = State.Injured; _injureTimeout = Constants.BossInjureTimeout; armor.Kill(this); if (_currentHp <= 0) GD.Print($"{this.Name} was killed."); } } }