changing map size

This commit is contained in:
2026-06-04 17:29:47 -04:00
parent 1e928bacb0
commit 0cbc6cbfc7
7 changed files with 124 additions and 654 deletions
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 628 B

After

Width:  |  Height:  |  Size: 735 B

+1
View File
@@ -4,5 +4,6 @@ using System;
public partial class Globals : Node public partial class Globals : Node
{ {
public static Random _rng = new(); public static Random _rng = new();
public static float _gravity = (float)ProjectSettings.GetSetting("physics/2d/default_gravity"), _drag = (float)ProjectSettings.GetSetting("physics/2d/default_linear_damp");
} }
+79 -610
View File
File diff suppressed because one or more lines are too long
+37 -37
View File
@@ -6,8 +6,7 @@ using System.Diagnostics;
public partial class Tower : Sprite2D public partial class Tower : Sprite2D
{ {
public bool _hovered, _aiming; public bool _hovered, _aiming;
public int _launchForce = 1000, _arcIterations = 50; public int _launchSpeed = 1000, _arcIterations = 50;
public float _gravity, _drag;
public Vector2 _aimOffset, _arcEnd; public Vector2 _aimOffset, _arcEnd;
public List<Vector2> _arc = new(); public List<Vector2> _arc = new();
public Marker2D _offset, _attackSpawn; public Marker2D _offset, _attackSpawn;
@@ -16,8 +15,6 @@ public partial class Tower : Sprite2D
public override void _Ready() public override void _Ready()
{ {
base._Ready(); base._Ready();
_gravity = (float)ProjectSettings.GetSetting("physics/2d/default_gravity");
_drag = (float)ProjectSettings.GetSetting("physics/2d/default_linear_damp");
_area = GetNode<Area2D>("Area"); _area = GetNode<Area2D>("Area");
_offset = GetNode<Marker2D>("Offset"); _offset = GetNode<Marker2D>("Offset");
@@ -32,8 +29,7 @@ public partial class Tower : Sprite2D
{ {
if (_arcEnd != GetGlobalMousePosition()) if (_arcEnd != GetGlobalMousePosition())
{ {
CalculateLaunchAngle(_attackSpawn.GlobalPosition, GetGlobalMousePosition(), _launchForce, _gravity, _drag); DrawArc(_attackSpawn.GlobalPosition, GetGlobalMousePosition());
DrawArc();
} }
if (Input.IsActionJustPressed("rightClick")) if (Input.IsActionJustPressed("rightClick"))
{ {
@@ -43,9 +39,11 @@ public partial class Tower : Sprite2D
else if (Input.IsActionJustPressed("leftClick")) else if (Input.IsActionJustPressed("leftClick"))
{ {
_aimOffset = CalculateLaunchAngle(_attackSpawn.GlobalPosition, GetGlobalMousePosition(), _launchForce, _gravity, _drag); _aimOffset = CalculateLaunchAngle(_attackSpawn.GlobalPosition, GetGlobalMousePosition());
_commander.ShootCurrentAttack(_aimOffset); _commander.ShootCurrentAttack(_aimOffset);
ClearArc();
_aiming = false; _aiming = false;
} }
} }
@@ -60,29 +58,25 @@ public partial class Tower : Sprite2D
} }
} }
public Vector2 CalculateLaunchAngle(Vector2 startPos, Vector2 targetPos, float launchSpeed, float gravity, float drag, int maxIterations = 20) public Vector2 CalculateLaunchAngle(Vector2 START, Vector2 END, int MAX_ITERATIONS = 20)
{ {
Vector2 toTarget = targetPos - startPos; Vector2 offset = END - START;
float baseAngle = toTarget.Angle(); // Directly pointing at the target float baseAngle = offset.Angle();
// Pure vertical angle going straight up (-Y is up in Godot)
float straightUpAngle = -Mathf.Pi / 2.0f; float straightUpAngle = -Mathf.Pi / 2.0f;
// We use a normalized 0.0 to 1.0 search range to avoid radian sign confusion float lowAngle = 0.0f;
float lowT = 0.0f; // 0% = pointing straight at target float highAngle = 1.0f;
float highT = 1.0f; // 100% = pointing straight up
float bestAngle = baseAngle; float bestAngle = baseAngle;
float delta = 1.0f / 60.0f; // Matches standard physics step
for (int i = 0; i < maxIterations; i++) for (int i = 0; i < MAX_ITERATIONS; i++)
{ {
float testT = (lowT + highT) / 2.0f; float averageAngle = (lowAngle + highAngle) / 2.0f;
// Interpolate smoothly between the base angle and straight up float testAngle = Mathf.LerpAngle(baseAngle, straightUpAngle, averageAngle);
float testAngle = Mathf.LerpAngle(baseAngle, straightUpAngle, testT);
float error = EvaluateSimulationError(testAngle, startPos, targetPos, launchSpeed, gravity, drag, delta); float error = EvaluateSimulationError(testAngle, START, END);
if (Mathf.Abs(error) < 0.2f) if (Mathf.Abs(error) < 0.2f)
{ {
@@ -90,28 +84,34 @@ public partial class Tower : Sprite2D
break; break;
} }
// error > 0 means the simulated projectile landed below the target (+Y is down)
if (error > 0) if (error > 0)
{ {
// It fell short or low. We need to loft it higher up (move toward 1.0) lowAngle = averageAngle;
lowT = testT;
} }
else else
{ {
// It flew over the target. We need to lower the arc (move toward 0.0) highAngle = averageAngle;
highT = testT;
} }
bestAngle = testAngle; bestAngle = testAngle;
} }
return Vector2.FromAngle(bestAngle) * _launchForce; return Vector2.FromAngle(bestAngle) * _launchSpeed;
} }
public void DrawArc() public void ClearArc()
{ {
Path2D path = _commander._attack.GetNode<Path2D>("PredictedPath"); Path2D path = _commander._attack.GetNode<Path2D>("PredictedPath");
path.Curve.ClearPoints(); path.Curve.ClearPoints();
}
public void DrawArc(Vector2 START, Vector2 END, int MAX_ITERATIONS = 20)
{
CalculateLaunchAngle(START, END);
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]);
@@ -119,15 +119,16 @@ public partial class Tower : Sprite2D
GD.Print(path.Curve.PointCount); GD.Print(path.Curve.PointCount);
} }
private float EvaluateSimulationError(float angle, Vector2 start, Vector2 target, float speed, float gravity, float drag, float delta) private float EvaluateSimulationError(float ANGLE, Vector2 START, Vector2 TARGET)
{ {
Vector2 velocity = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) * speed; float speed = _launchSpeed, gravity = Globals._gravity, drag = Globals._drag, delta = 1.0f / 60.0f;
Vector2 position = start; Vector2 velocity = new Vector2(Mathf.Cos(ANGLE), Mathf.Sin(ANGLE)) * speed;
Vector2 position = START;
float directionSign = Mathf.Sign(target.X - start.X); float directionSign = Mathf.Sign(TARGET.X - START.X);
float maxFlightTime = 6.0f; float maxFlightTime = 6.0f;
int totalSteps = (int)(maxFlightTime / delta); int totalSteps = (int)(maxFlightTime / delta);
_arc = [start]; _arc = [START];
for (int step = 0; step < totalSteps; step++) for (int step = 0; step < totalSteps; step++)
{ {
@@ -136,17 +137,16 @@ public partial class Tower : Sprite2D
position += velocity * delta; position += velocity * delta;
_arc.Add(position); _arc.Add(position);
// Check if our projectile crossed the target line on the X axis bool reachedTargetX = (directionSign >= 0) ? (position.X >= TARGET.X) : (position.X <= TARGET.X);
bool reachedTargetX = (directionSign >= 0) ? (position.X >= target.X) : (position.X <= target.X);
if (reachedTargetX) if (reachedTargetX)
{ {
return position.Y - target.Y; return position.Y - TARGET.Y;
} }
} }
_arcEnd = _arc[^1]; _arcEnd = _arc[^1];
// If it ran out of time or hit a wall before crossing target.X, penalize it heavily return (position.Y > TARGET.Y) ? 99999f : -99999f;
return (position.Y > target.Y) ? 99999f : -99999f;
} }
public void StartTurn() public void StartTurn()
+2 -2
View File
@@ -7,7 +7,7 @@
bounce = 0.25 bounce = 0.25
[sub_resource type="CircleShape2D" id="CircleShape2D_7yfhp"] [sub_resource type="CircleShape2D" id="CircleShape2D_7yfhp"]
radius = 8.0 radius = 12.5
[sub_resource type="Curve2D" id="Curve2D_63pi1"] [sub_resource type="Curve2D" id="Curve2D_63pi1"]
@@ -22,7 +22,7 @@ script = ExtResource("1_63pi1")
shape = SubResource("CircleShape2D_7yfhp") shape = SubResource("CircleShape2D_7yfhp")
[node name="Sprite2D" type="Sprite2D" parent="." unique_id=1583277900] [node name="Sprite2D" type="Sprite2D" parent="." unique_id=1583277900]
scale = Vector2(0.32, 0.32) scale = Vector2(0.5, 0.5)
texture = ExtResource("2_hqc8w") texture = ExtResource("2_hqc8w")
[node name="PredictedPath" type="Path2D" parent="." unique_id=1505290715] [node name="PredictedPath" type="Path2D" parent="." unique_id=1505290715]
+2 -2
View File
@@ -7,7 +7,7 @@
bounce = 0.5 bounce = 0.5
[sub_resource type="CircleShape2D" id="CircleShape2D_4gyqm"] [sub_resource type="CircleShape2D" id="CircleShape2D_4gyqm"]
radius = 8.0 radius = 12.5
[node name="Enemy" type="StaticBody2D" unique_id=1417697759] [node name="Enemy" type="StaticBody2D" unique_id=1417697759]
physics_material_override = SubResource("PhysicsMaterial_7k104") physics_material_override = SubResource("PhysicsMaterial_7k104")
@@ -17,5 +17,5 @@ script = ExtResource("1_4gyqm")
shape = SubResource("CircleShape2D_4gyqm") shape = SubResource("CircleShape2D_4gyqm")
[node name="Sprite2D" type="Sprite2D" parent="." unique_id=1941012605] [node name="Sprite2D" type="Sprite2D" parent="." unique_id=1941012605]
scale = Vector2(0.32, 0.32) scale = Vector2(0.5, 0.5)
texture = ExtResource("1_7k104") texture = ExtResource("1_7k104")
+3 -3
View File
@@ -1,15 +1,15 @@
[gd_scene format=3 uid="uid://dcp7p6al4i0b7"] [gd_scene format=3 uid="uid://dcp7p6al4i0b7"]
[ext_resource type="Script" uid="uid://cg1m762ed04kv" path="res://Main.cs" id="1_ig7tw"] [ext_resource type="Script" uid="uid://cg1m762ed04kv" path="res://Main.cs" id="1_ig7tw"]
[ext_resource type="PackedScene" uid="uid://la8pwcc0tjuu" path="res://PlayArea.tscn" id="3_h2yge"] [ext_resource type="PackedScene" path="res://PlayArea.tscn" id="2_1bvp3"]
[ext_resource type="PackedScene" uid="uid://c6b188d2a20eq" path="res://enemy_controller.tscn" id="4_1bvp3"] [ext_resource type="PackedScene" uid="uid://c6b188d2a20eq" path="res://enemy_controller.tscn" id="4_1bvp3"]
[ext_resource type="PackedScene" uid="uid://b7kvx7p0b2086" path="res://player_controller.tscn" id="4_lquwl"] [ext_resource type="PackedScene" uid="uid://b7kvx7p0b2086" path="res://player_controller.tscn" id="4_lquwl"]
[node name="Main" type="Node" unique_id=535208469] [node name="Main" type="Node" unique_id=535208469]
script = ExtResource("1_ig7tw") script = ExtResource("1_ig7tw")
[node name="PlayArea" parent="." unique_id=1123610167 instance=ExtResource("3_h2yge")] [node name="PlayArea" parent="." unique_id=1123610167 instance=ExtResource("2_1bvp3")]
position = Vector2(960, 600) position = Vector2(960, 510)
[node name="EnemyController" parent="." unique_id=1894449838 instance=ExtResource("4_1bvp3")] [node name="EnemyController" parent="." unique_id=1894449838 instance=ExtResource("4_1bvp3")]