using Godot; using GodotStateCharts; using Lamar.IoC.Instances; using LandTrains.scripts.Enemy; using LandTrains.scripts.Foundation; using System; public partial class MortarBugView : Node3D { public MortarBugView() { Curve = new Curve3D(); Distance = 0; } internal Curve3D Curve { get; set; } internal Path3D Path3D { get; set; } internal PathFollow3D PathFollow3D { get; set; } internal AnimationPlayer AnimationPlayer { get; set; } internal StateChart StateChart { get; set; } public CompoundState CompoundState { get; private set; } internal LandTrains.scripts.Enemy.MortarBugController Bug { get; set; } internal float Distance { get; set; } internal float CurveLength { get; set; } internal Node3D Visuals { get; set; } internal DiscreteVector3 LastTargetArea { get; set; } // Called when the node enters the scene tree for the first time. public override void _Ready() { Bug.Die += Bug_Die; StateChart = StateChart.Of(GetNode("StateChart")); CompoundState = CompoundState.Of(GetNode("StateChart//CompoundState")); AnimationPlayer = GetNode("AnimationPlayer"); Visuals = GetNode("Path3D//PathFollow3D//visuals"); Path3D = GetNode("Path3D"); Path3D.Curve = Curve; PathFollow3D = Path3D.GetNode("PathFollow3D"); PathFollow3D.ProgressRatio = 0; AnimationPlayer.AnimationFinished += P_AnimationFinished; } private void Bug_Die(object sender, EventArgs e) { StateChart.SendEvent("ToDie"); } private void Attack() { UpdateTarget(); StateChart.SendEvent("ToAttack"); } private void UpdateTarget() { Curve.RemovePoint(Curve.PointCount - 1); Curve.AddPoint(PathFollow3D.Position); var p = Bug.Target.Area - Position; Curve.AddPoint(p); CurveLength = Curve.GetBakedLength(); } public override void _Process(double delta) { } internal void _on_spawn_state_entered() { AnimationPlayer.Play("spawn"); } internal void _on_attack_state_entered() { Distance += Convert.ToSingle(.0008f); PathFollow3D.Progress += Distance; AnimationPlayer.Play("attack"); } private void P_AnimationFinished(StringName animName) { if (animName == "spawn") { StateChart.SendEvent("ToMove"); } if (animName == "die") { QueueFree(); } if (animName == "attack") { StateChart.SendEvent("ToMove"); } } internal void _on_move_state_entered() { var c = new Curve3D(); c.AddPoint(Visuals.Position); c.AddPoint(LastTargetArea - Position); Distance = 0; Curve = c; CurveLength = Curve.GetBakedLength(); Path3D.Curve = c; AnimationPlayer.Play("start-move"); } internal void _on_die_state_entered() { AnimationPlayer.Play("die"); } internal void _on_move_state_processing(float delta) { Bug.Area = (LandTrains.scripts.Foundation.DiscreteVector3)Visuals.GlobalPosition; if (Bug.Target != null && Bug.Target.Area != LastTargetArea) { LastTargetArea = Bug.Target.Area; UpdateTarget(); } Bug.Processing(delta); if (Bug.StartAttack) { Attack(); Bug.StartAttack = false; } if (Distance > CurveLength) return; Distance += Convert.ToSingle(delta * Bug.Speed); PathFollow3D.Progress = Distance; } }