You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Babushka/scripts/CSharp/Common/Fight/Fighter.cs

182 lines
4.2 KiB

using Godot;
namespace Babushka.scripts.CSharp.Common.Fight;
public partial class Fighter : Node2D
{
[Export] public string name;
[Export] public int maxHealth;
[Export] public int attackStrength;
[Export] public int maxActions = 1;
[ExportCategory("References")]
[Export] private Node2D _attackButtons;
[Export] private Node2D _targetButtons;
[Export] private Node2D _targetMarker;
[Export] private Label _healthText;
[Export] private Node2D _visualSprite;
[Signal] public delegate void DamageTakenEventHandler();
[Signal] public delegate void AttackingEventHandler();
[Signal] public delegate void DyingEventHandler();
[Signal] public delegate void HealedEventHandler();
private int _health;
private int _actions;
public FightInstance fightInstance;
public int Health
{
get => _health;
set
{
_health = value;
if (_health <= 0)
{
_health = 0;
Die();
}
if (_health > maxHealth)
{
_health = maxHealth;
}
}
}
private void Die()
{
_visualSprite.Scale = new Vector2(1, 0.3f);
EmitSignalDying();
}
public override void _Ready()
{
Health = maxHealth;
UpdateHealthVisual();
ResetActions();
}
public void Attack()
{
fightInstance.SelectAttack(this);
}
public void HideAttackButton()
{
_attackButtons.Hide();
}
public void ShowAttackButton()
{
_attackButtons.Show();
}
public void HideTargetButtons()
{
_targetButtons.Hide();
}
public void ShowTargetButtons()
{
_targetButtons.Show();
}
public void TargetMouseEvent(Node viewport, InputEvent inputEvent, int shapeIdx)
{
if (inputEvent.IsPressed())
ClickedTarget();
}
public void AttackMouseEvent(Node viewport, InputEvent inputEvent, int shapeIdx)
{
if (inputEvent.IsPressed())
ClickedAttack();
}
public void HealMouseEvent(Node viewport, InputEvent inputEvent, int shapeIdx)
{
if (inputEvent.IsPressed())
ClickedHeal();
}
private void ClickedAttack()
{
fightInstance.SelectAttack(this);
}
private void ClickedHeal()
{
fightInstance.SelectHeal(this);
}
private void ClickedTarget()
{
fightInstance.SelectTargetAndAttack(this);
}
public void StartHoverTarget()
{
_targetMarker.Visible = true;
}
public void EndHoverTarget()
{
_targetMarker.Visible = false;
}
public void UpdateHealthVisual()
{
_healthText.Text = $"{Health}/{maxHealth}";
}
public void AttackAnimation(FightAttack attack)
{
EmitSignalAttacking();
var tween = GetTree().CreateTween();
tween.TweenProperty(this, "global_position", attack.target.GlobalPosition, 0.15);
tween.TweenCallback(Callable.From(() => attack.target?.HitAnimation(attack)));
tween.TweenProperty(this, "position", new Vector2(0, 0), 0.7)
.SetTrans(Tween.TransitionType.Cubic).SetEase(Tween.EaseType.Out);
}
private void HitAnimation(FightAttack attack)
{
EmitSignalDamageTaken();
var tween = GetTree().CreateTween();
tween.TweenProperty(this, "scale", new Vector2(1.4f, 0.6f), 0.15);
tween.TweenProperty(this, "scale", new Vector2(1, 1), 0.4)
.SetTrans(Tween.TransitionType.Cubic).SetEase(Tween.EaseType.Out);
}
public bool IsDead()
{
return Health <= 0;
}
public void ResetActions()
{
_actions = maxActions;
}
public bool HasActionsLeft()
{
return _actions > 0;
}
public void DecrementActions()
{
_actions--;
}
public void HealAnimation()
{
EmitSignalHealed();
var tween = GetTree().CreateTween();
tween.TweenProperty(this, "scale", new Vector2(0.6f, 1.4f), 0.15);
tween.TweenProperty(this, "scale", new Vector2(1, 1), 0.4)
.SetTrans(Tween.TransitionType.Cubic).SetEase(Tween.EaseType.Out);
}
}