Files
Babushka/scripts/CSharp/Common/Fight/FighterVisual.cs
T

93 lines
3.2 KiB
C#
Raw Normal View History

2025-09-30 16:23:05 +02:00
using System;
2025-09-30 17:36:28 +02:00
using System.Threading.Tasks;
2025-09-30 16:23:05 +02:00
using Babushka.scripts.CSharp.Common.Fight.ActionDetails;
2025-09-30 19:00:52 +02:00
using Babushka.scripts.CSharp.Common.Fight.UI;
2025-09-30 16:23:05 +02:00
using Godot;
using Godot.Collections;
2025-07-10 03:38:48 +02:00
namespace Babushka.scripts.CSharp.Common.Fight;
2025-09-21 14:54:55 +02:00
public partial class FighterVisual : Node2D
2025-07-10 03:38:48 +02:00
{
2025-09-30 16:23:05 +02:00
#region Shortcuts
2025-07-10 03:38:48 +02:00
2025-09-30 16:23:05 +02:00
private FightWorld.FightHappeningData HappeningData =>
FightWorld.Instance.fightHappeningData ?? throw new NoFightHappeningException();
2025-07-10 03:38:48 +02:00
2025-09-30 16:23:05 +02:00
#endregion
2025-07-10 03:38:48 +02:00
2025-09-30 19:00:52 +02:00
[ExportCategory("References")]
[Export] private Node2D _visualParent = null!;
[Export] private Node2D _targetSelectionParent = null!;
[Export] public FighterHealthBarVisual healthBarVisual = null!;
2025-07-10 03:38:48 +02:00
2025-07-10 23:32:16 +02:00
2025-09-30 16:23:05 +02:00
private FightWorld.Fighter _boundFighter;
2025-07-10 03:38:48 +02:00
2025-09-30 16:23:05 +02:00
public void Initialize(FightWorld.Fighter fighter)
2025-07-10 03:38:48 +02:00
{
2025-09-30 16:23:05 +02:00
_boundFighter = fighter;
UpdateVisuals();
2025-07-10 03:38:48 +02:00
}
public void UpdateVisuals()
2025-07-10 03:38:48 +02:00
{
// fighter visuals should always look to the right in the scene.
// This function flips the sprites horizontally, when the fighter is an enemy.
_visualParent.Scale = new Vector2(
_boundFighter.IsInFormation(HappeningData.enemyFighterFormation) ? -1 : 1,
_boundFighter.IsDead() ? .3f : 1);
2025-07-10 03:38:48 +02:00
2025-09-30 19:00:52 +02:00
healthBarVisual.UpdateHealth(_boundFighter.GetHealth(), _boundFighter.maxHealth);
}
2025-09-30 16:23:05 +02:00
public void SetTargetSelectionActive(bool value)
2025-07-10 03:38:48 +02:00
{
2025-09-30 16:23:05 +02:00
_targetSelectionParent.Visible = value;
_targetSelectionParent.ProcessMode = value ? ProcessModeEnum.Inherit : ProcessModeEnum.Disabled;
2025-09-21 14:54:55 +02:00
}
2025-09-30 16:23:05 +02:00
// listen from inside
public void ClickedTarget()
2025-09-21 14:54:55 +02:00
{
2025-09-30 16:23:05 +02:00
if (HappeningData.actionStaging!.CurrentDetail() is not TargetSelectActionDetail targetDetail)
throw new InvalidOperationException("No target selection needed right now");
2025-09-21 14:54:55 +02:00
2025-09-30 16:23:05 +02:00
targetDetail.SetTarget(_boundFighter);
FightHappening.Instance.DetailFilled();
2025-07-10 03:38:48 +02:00
}
2025-09-30 19:00:52 +02:00
2025-09-30 16:23:05 +02:00
// Animations
2025-09-30 17:36:28 +02:00
public async Task AnimatePosToTarget(FighterVisual targetVisual, double duration = 0.15)
2025-07-10 03:38:48 +02:00
{
var tween = GetTree().CreateTween();
2025-09-30 17:36:28 +02:00
tween.TweenProperty(_visualParent, "global_position", targetVisual.GlobalPosition, 0.15);
await ToSignal(tween, "finished");
2025-07-10 03:38:48 +02:00
}
2025-09-30 17:36:28 +02:00
public async Task AnimatePosToBase(double duration = 0.7)
2025-07-10 03:38:48 +02:00
{
var tween = GetTree().CreateTween();
2025-09-30 17:36:28 +02:00
tween.TweenProperty(_visualParent, "position", new Vector2(0, 0), 0.15);
await ToSignal(tween, "finished");
2025-07-10 03:38:48 +02:00
}
2025-09-30 17:36:28 +02:00
public async Task AnimateHit()
2025-07-10 23:32:16 +02:00
{
var tween = GetTree().CreateTween();
2025-09-30 17:36:28 +02:00
tween.TweenProperty(this, "scale", new Vector2(1.4f, 0.6f), 0.15);
2025-07-10 23:32:16 +02:00
tween.TweenProperty(this, "scale", new Vector2(1, 1), 0.4)
.SetTrans(Tween.TransitionType.Cubic).SetEase(Tween.EaseType.Out);
2025-09-30 17:36:28 +02:00
await ToSignal(tween, "finished");
2025-07-10 23:32:16 +02:00
}
2025-09-30 19:00:52 +02:00
2025-11-03 20:21:30 +01:00
// Keep for reference for new Heal animation
2025-09-30 17:36:28 +02:00
//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);
//}
2025-09-30 16:23:05 +02:00
}