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

122 lines
3.9 KiB

using System;
using System.Threading.Tasks;
using Babushka.scripts.CSharp.Common.Fight.ActionDetails;
using Godot;
using Godot.Collections;
namespace Babushka.scripts.CSharp.Common.Fight;
[Tool]
public partial class FighterVisual : Node2D
{
#region Shortcuts
private FightWorld.FightHappeningData HappeningData =>
FightWorld.Instance.fightHappeningData ?? throw new NoFightHappeningException();
#endregion
[ExportCategory("References")]
[Export] private Node2D _visualParent;
[Export] private Node2D _targetSelectionParent;
[Signal]
public delegate void DamageTakenEventHandler();
[Signal]
public delegate void AttackingEventHandler();
[Signal]
public delegate void DyingEventHandler();
[Signal]
public delegate void HealedEventHandler();
private FightWorld.Fighter _boundFighter;
public void Initialize(FightWorld.Fighter fighter)
{
_boundFighter = fighter;
UpdateMirrorState();
}
/// <summary>
/// fighter visuals should always look to the right in the scene.
/// This function flips the sprites horizontally, when the fighter is an enemy.
/// </summary>
private void UpdateMirrorState()
{
_visualParent.Scale = new Vector2(_boundFighter.isEnemy ? -1 : 1, 1);
}
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);
//}
}