cirnogodot/Scripts/AttackPatterns/MovementPattern.cs

73 lines
2.3 KiB
C#
Raw Normal View History

2025-02-05 19:41:49 +01:00
using Cirno.Scripts.Actors;
using Cirno.Scripts.Resources;
using Godot;
namespace Cirno.Scripts.AttackPatterns;
[GlobalClass]
2025-06-03 10:11:09 +02:00
[Tool]
2025-02-05 19:41:49 +01:00
public partial class MovementPattern : AttackPattern
{
2025-03-15 17:17:30 +01:00
[Export] public Vector2 relativeTargetPosition;
[Export] public float moveDuration = 2f;
[Export] public Tween.TransitionType transitionType = Tween.TransitionType.Linear;
[Export] public Tween.EaseType easeType = Tween.EaseType.InOut;
[Export] public AttackPattern shootingPattern;
2025-02-05 19:41:49 +01:00
2025-03-15 17:17:30 +01:00
public override IPatternMachine MakeMachine(Node2D parent)
{
return new MovementPatternMachine(this, parent);
}
2025-02-05 19:41:49 +01:00
2025-03-15 17:17:30 +01:00
public class MovementPatternMachine(MovementPattern pattern, Node2D parent) : IPatternMachine
{
public Node2D Parent => parent;
public MovementPattern Pattern { get; } = pattern;
private IPatternMachine _machine;
private Tween tween;
private bool isComplete = false;
2025-03-15 11:44:30 +01:00
2025-03-15 17:17:30 +01:00
protected IScriptHost Boss;
2025-03-15 11:44:30 +01:00
2025-03-15 17:17:30 +01:00
public void Start()
{
if (parent is not IScriptHost boss)
return;
2025-03-15 11:44:30 +01:00
2025-03-15 17:17:30 +01:00
Boss = boss;
tween = parent.CreateTween();
isComplete = false;
2025-02-05 19:41:49 +01:00
2025-03-15 17:17:30 +01:00
Vector2 targetPosition = (Boss?.HomePosition ?? parent.GlobalPosition) + Pattern.relativeTargetPosition;
2025-03-06 22:09:11 +01:00
2025-03-15 17:17:30 +01:00
tween.TweenProperty(Parent, "position", targetPosition, Pattern.moveDuration)
.SetTrans(Pattern.transitionType)
.SetEase(Pattern.easeType)
.Finished += () => isComplete = true;
2025-02-05 19:41:49 +01:00
2025-03-15 17:17:30 +01:00
if (Pattern.shootingPattern != null && !Pattern.WaitForCompletion)
{
_machine = Pattern.shootingPattern.MakeMachine(parent);
_machine.Start();
}
2025-02-05 19:41:49 +01:00
}
2025-03-15 17:17:30 +01:00
public void UpdatePattern(double delta)
2025-02-05 19:41:49 +01:00
{
2025-03-15 17:17:30 +01:00
if (_machine is not null && !Pattern.WaitForCompletion)
{
_machine.UpdatePattern(delta);
//shootingPattern.UpdatePattern(delta);
}
2025-02-05 19:41:49 +01:00
}
2025-03-15 17:17:30 +01:00
public bool IsComplete()
{
if (Pattern.WaitForCompletion && _machine is not null)
return isComplete && _machine.IsComplete();
return isComplete;
}
2025-02-05 19:41:49 +01:00
}
}