working some more on enemy movement, they stilldon't always want to move to the sides when they reach the front so still some more work to do. also updated the aiming a bit to clamp the arc for firing and drawing, rewrote the draw arc function

This commit is contained in:
2026-06-07 21:14:46 -04:00
parent 12b565715a
commit 3c9a51bd6a
10 changed files with 144 additions and 39 deletions
+38 -10
View File
@@ -29,7 +29,8 @@ public partial class Tower : Sprite2D
{
if (_arcEnd != GetGlobalMousePosition())
{
DrawArc(_attackSpawn.GlobalPosition, GetGlobalMousePosition());
float arcAngle = CalculateLaunchAngle(_attackSpawn.GlobalPosition, GetGlobalMousePosition());
DrawArc(arcAngle, _attackSpawn.GlobalPosition, GetGlobalMousePosition());
}
if (Input.IsActionJustPressed("rightClick"))
{
@@ -39,7 +40,7 @@ public partial class Tower : Sprite2D
else if (Input.IsActionJustPressed("leftClick"))
{
_aimOffset = CalculateLaunchAngle(_attackSpawn.GlobalPosition, GetGlobalMousePosition());
_aimOffset = ToLaunchVector(CalculateLaunchAngle(_attackSpawn.GlobalPosition, GetGlobalMousePosition()));
_commander.ShootCurrentAttack(_aimOffset);
@@ -58,7 +59,7 @@ public partial class Tower : Sprite2D
}
}
public Vector2 CalculateLaunchAngle(Vector2 START, Vector2 END, int MAX_ITERATIONS = 20)
public float CalculateLaunchAngle(Vector2 START, Vector2 END, int MAX_ITERATIONS = 20)
{
Vector2 offset = END - START;
float baseAngle = offset.Angle();
@@ -95,8 +96,9 @@ public partial class Tower : Sprite2D
bestAngle = testAngle;
}
bestAngle = (float)Mathf.Clamp(bestAngle, Math.PI, Math.PI * 2);
return Vector2.FromAngle(bestAngle) * _launchSpeed;
return bestAngle;
}
public void ClearArc()
@@ -105,16 +107,40 @@ public partial class Tower : Sprite2D
path.Curve.ClearPoints();
}
public void DrawArc(Vector2 START, Vector2 END, int MAX_ITERATIONS = 20)
public void DrawArc(float ANGLE, Vector2 START, Vector2 END, int MAX_ITERATIONS = 20)
{
CalculateLaunchAngle(START, END);
float speed = _launchSpeed, gravity = Globals._gravity, drag = Globals._drag, delta = 1.0f / 60.0f;
Vector2 velocity = new Vector2(Mathf.Cos(ANGLE), Mathf.Sin(ANGLE)) * speed;
Vector2 position = START;
float directionSign = Mathf.Sign(END.X - START.X);
float maxFlightTime = 6.0f;
int totalSteps = (int)(maxFlightTime / delta);
List<Vector2> arc = [START];
for (int step = 0; step < totalSteps; step++)
{
velocity.Y += gravity * delta;
velocity -= velocity * drag * delta;
position += velocity * delta;
arc.Add(position);
bool reachedTargetX = (directionSign >= 0) ? (position.X >= END.X) : (position.X <= END.X);
if (reachedTargetX)
{
break;
}
}
Path2D path = _commander._attack.GetNode<Path2D>("PredictedPath");
path.Curve.ClearPoints();
for (int i = 0; i < _arc.Count; i++)
for (int i = 0; i < arc.Count; i++)
{
path.Curve.AddPoint(_arc[i]);
path.Curve.AddPoint(arc[i]);
}
}
@@ -127,14 +153,12 @@ public partial class Tower : Sprite2D
float directionSign = Mathf.Sign(TARGET.X - START.X);
float maxFlightTime = 6.0f;
int totalSteps = (int)(maxFlightTime / delta);
_arc = [START];
for (int step = 0; step < totalSteps; step++)
{
velocity.Y += gravity * delta;
velocity -= velocity * drag * delta;
position += velocity * delta;
_arc.Add(position);
bool reachedTargetX = (directionSign >= 0) ? (position.X >= TARGET.X) : (position.X <= TARGET.X);
@@ -144,7 +168,6 @@ public partial class Tower : Sprite2D
}
}
_arcEnd = _arc[^1];
return (position.Y > TARGET.Y) ? 99999f : -99999f;
}
@@ -156,6 +179,11 @@ public partial class Tower : Sprite2D
}
}
public Vector2 ToLaunchVector(float ANGLE)
{
return Vector2.FromAngle(ANGLE) * _launchSpeed;
}
public void OnMouseEntered(){
_hovered = true;
}