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] public FighterHealthBarVisual healthBarVisual = null!; 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); healthBarVisual.UpdateHealth(_boundFighter.GetHealth(), _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() { 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); await ToSignal(tween, "finished"); } // Keep for reference for new Heal animation //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); //} }