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/FighterVisual.cs

108 lines
3.6 KiB

using System;
using System.Threading.Tasks;
using Babushka.scripts.CSharp.Common.Fight.ActionDetails;
using Babushka.scripts.CSharp.Common.Fight.UI;
using Godot;
using Godot.Collections;
namespace Babushka.scripts.CSharp.Common.Fight;
public partial class FighterVisual : Node2D
{
#region Shortcuts
private FightWorld.FightHappeningData HappeningData =>
FightWorld.Instance.fightHappeningData ?? throw new NoFightHappeningException();
#endregion
[ExportCategory("References")]
[Export] private Node2D _visualParent = null!;
[Export] private Node2D _targetSelectionParent = null!;
[Export] private Node2D _squashParent = null!;
[Export] public FighterHealthBarVisual healthBarVisual = null!;
[Export] private FighterDamageIndicatorVisual _fighterDamageIndicatorVisual;
private FightWorld.Fighter _boundFighter;
public void Initialize(FightWorld.Fighter fighter)
{
_boundFighter = fighter;
UpdateVisuals();
}
public void UpdateVisuals()
{
// 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);
UpdateHealthBarVisuals();
}
private void UpdateHealthBarVisuals()
{
healthBarVisual.UpdateHealth(_boundFighter.Health, _boundFighter.maxHealth);
}
public void SetTargetSelectionActive(bool value)
{
_targetSelectionParent.Visible = value;
_targetSelectionParent.ProcessMode = value ? ProcessModeEnum.Inherit : ProcessModeEnum.Disabled;
}
// listen from inside
public void ClickedTarget()
{
if (HappeningData.actionStaging!.CurrentDetail() is not TargetSelectActionDetail targetDetail)
throw new InvalidOperationException("No target selection needed right now");
targetDetail.SetTarget(_boundFighter);
FightHappening.Instance.DetailFilled();
}
// Animations
public async Task AnimatePosToTarget(FighterVisual targetVisual, double duration = 0.15)
{
var tween = GetTree().CreateTween();
tween.TweenProperty(_visualParent, "global_position", targetVisual.GlobalPosition, 0.15);
await ToSignal(tween, "finished");
}
public async Task AnimatePosToBase(double duration = 0.7)
{
var tween = GetTree().CreateTween();
tween.TweenProperty(_visualParent, "position", new Vector2(0, 0), 0.15);
await ToSignal(tween, "finished");
}
public async Task AnimateHit()
{
UpdateHealthBarVisuals();
var tween = GetTree().CreateTween();
tween.TweenProperty(_squashParent, "scale", new Vector2(1.4f, 0.6f), 0.15);
tween.TweenProperty(_squashParent, "scale", new Vector2(1, 1), 0.4)
.SetTrans(Tween.TransitionType.Cubic).SetEase(Tween.EaseType.Out);
await ToSignal(tween, "finished");
}
public async Task AnimateHeal()
{
UpdateHealthBarVisuals();
var tween = GetTree().CreateTween();
tween.TweenProperty(_squashParent, "scale", new Vector2(0.6f, 1.4f), 0.15);
tween.TweenProperty(_squashParent, "scale", new Vector2(1, 1), 0.4)
.SetTrans(Tween.TransitionType.Cubic).SetEase(Tween.EaseType.Out);
await ToSignal(tween, "finished");
}
public void SpawnDamageIndicatorNumber(string text)
{
_fighterDamageIndicatorVisual.SpawnFlyingNumber(text);
}
}