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; UpdateMirrorState(); UpdateHealthBar(); } /// /// fighter visuals should always look to the right in the scene. /// This function flips the sprites horizontally, when the fighter is an enemy. /// private void UpdateMirrorState() { _visualParent.Scale = new Vector2(_boundFighter.isEnemy ? -1 : 1, 1); } public void UpdateHealthBar() { 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 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); //} 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"); } //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 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); //} }