changing map size
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 628 B After Width: | Height: | Size: 735 B |
@@ -4,5 +4,6 @@ using System;
|
||||
public partial class Globals : Node
|
||||
{
|
||||
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
File diff suppressed because one or more lines are too long
@@ -6,8 +6,7 @@ using System.Diagnostics;
|
||||
public partial class Tower : Sprite2D
|
||||
{
|
||||
public bool _hovered, _aiming;
|
||||
public int _launchForce = 1000, _arcIterations = 50;
|
||||
public float _gravity, _drag;
|
||||
public int _launchSpeed = 1000, _arcIterations = 50;
|
||||
public Vector2 _aimOffset, _arcEnd;
|
||||
public List<Vector2> _arc = new();
|
||||
public Marker2D _offset, _attackSpawn;
|
||||
@@ -16,8 +15,6 @@ public partial class Tower : Sprite2D
|
||||
public override void _Ready()
|
||||
{
|
||||
base._Ready();
|
||||
_gravity = (float)ProjectSettings.GetSetting("physics/2d/default_gravity");
|
||||
_drag = (float)ProjectSettings.GetSetting("physics/2d/default_linear_damp");
|
||||
|
||||
_area = GetNode<Area2D>("Area");
|
||||
_offset = GetNode<Marker2D>("Offset");
|
||||
@@ -32,8 +29,7 @@ public partial class Tower : Sprite2D
|
||||
{
|
||||
if (_arcEnd != GetGlobalMousePosition())
|
||||
{
|
||||
CalculateLaunchAngle(_attackSpawn.GlobalPosition, GetGlobalMousePosition(), _launchForce, _gravity, _drag);
|
||||
DrawArc();
|
||||
DrawArc(_attackSpawn.GlobalPosition, GetGlobalMousePosition());
|
||||
}
|
||||
if (Input.IsActionJustPressed("rightClick"))
|
||||
{
|
||||
@@ -43,9 +39,11 @@ public partial class Tower : Sprite2D
|
||||
else if (Input.IsActionJustPressed("leftClick"))
|
||||
{
|
||||
|
||||
_aimOffset = CalculateLaunchAngle(_attackSpawn.GlobalPosition, GetGlobalMousePosition(), _launchForce, _gravity, _drag);
|
||||
_aimOffset = CalculateLaunchAngle(_attackSpawn.GlobalPosition, GetGlobalMousePosition());
|
||||
|
||||
_commander.ShootCurrentAttack(_aimOffset);
|
||||
|
||||
ClearArc();
|
||||
_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;
|
||||
float baseAngle = toTarget.Angle(); // Directly pointing at the target
|
||||
Vector2 offset = END - START;
|
||||
float baseAngle = offset.Angle();
|
||||
|
||||
// Pure vertical angle going straight up (-Y is up in Godot)
|
||||
float straightUpAngle = -Mathf.Pi / 2.0f;
|
||||
|
||||
// We use a normalized 0.0 to 1.0 search range to avoid radian sign confusion
|
||||
float lowT = 0.0f; // 0% = pointing straight at target
|
||||
float highT = 1.0f; // 100% = pointing straight up
|
||||
float lowAngle = 0.0f;
|
||||
float highAngle = 1.0f;
|
||||
|
||||
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, testT);
|
||||
float testAngle = Mathf.LerpAngle(baseAngle, straightUpAngle, averageAngle);
|
||||
|
||||
float error = EvaluateSimulationError(testAngle, startPos, targetPos, launchSpeed, gravity, drag, delta);
|
||||
float error = EvaluateSimulationError(testAngle, START, END);
|
||||
|
||||
if (Mathf.Abs(error) < 0.2f)
|
||||
{
|
||||
@@ -90,28 +84,34 @@ public partial class Tower : Sprite2D
|
||||
break;
|
||||
}
|
||||
|
||||
// error > 0 means the simulated projectile landed below the target (+Y is down)
|
||||
if (error > 0)
|
||||
{
|
||||
// It fell short or low. We need to loft it higher up (move toward 1.0)
|
||||
lowT = testT;
|
||||
lowAngle = averageAngle;
|
||||
}
|
||||
else
|
||||
{
|
||||
// It flew over the target. We need to lower the arc (move toward 0.0)
|
||||
highT = testT;
|
||||
highAngle = averageAngle;
|
||||
}
|
||||
|
||||
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");
|
||||
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++)
|
||||
{
|
||||
path.Curve.AddPoint(_arc[i]);
|
||||
@@ -119,15 +119,16 @@ public partial class Tower : Sprite2D
|
||||
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;
|
||||
Vector2 position = start;
|
||||
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(target.X - start.X);
|
||||
float directionSign = Mathf.Sign(TARGET.X - START.X);
|
||||
float maxFlightTime = 6.0f;
|
||||
int totalSteps = (int)(maxFlightTime / delta);
|
||||
_arc = [start];
|
||||
_arc = [START];
|
||||
|
||||
for (int step = 0; step < totalSteps; step++)
|
||||
{
|
||||
@@ -136,17 +137,16 @@ public partial class Tower : Sprite2D
|
||||
position += velocity * delta;
|
||||
_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)
|
||||
{
|
||||
return position.Y - target.Y;
|
||||
return position.Y - TARGET.Y;
|
||||
}
|
||||
}
|
||||
|
||||
_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()
|
||||
|
||||
+2
-2
@@ -7,7 +7,7 @@
|
||||
bounce = 0.25
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_7yfhp"]
|
||||
radius = 8.0
|
||||
radius = 12.5
|
||||
|
||||
[sub_resource type="Curve2D" id="Curve2D_63pi1"]
|
||||
|
||||
@@ -22,7 +22,7 @@ script = ExtResource("1_63pi1")
|
||||
shape = SubResource("CircleShape2D_7yfhp")
|
||||
|
||||
[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")
|
||||
|
||||
[node name="PredictedPath" type="Path2D" parent="." unique_id=1505290715]
|
||||
|
||||
+2
-2
@@ -7,7 +7,7 @@
|
||||
bounce = 0.5
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_4gyqm"]
|
||||
radius = 8.0
|
||||
radius = 12.5
|
||||
|
||||
[node name="Enemy" type="StaticBody2D" unique_id=1417697759]
|
||||
physics_material_override = SubResource("PhysicsMaterial_7k104")
|
||||
@@ -17,5 +17,5 @@ script = ExtResource("1_4gyqm")
|
||||
shape = SubResource("CircleShape2D_4gyqm")
|
||||
|
||||
[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")
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
[gd_scene format=3 uid="uid://dcp7p6al4i0b7"]
|
||||
|
||||
[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://b7kvx7p0b2086" path="res://player_controller.tscn" id="4_lquwl"]
|
||||
|
||||
[node name="Main" type="Node" unique_id=535208469]
|
||||
script = ExtResource("1_ig7tw")
|
||||
|
||||
[node name="PlayArea" parent="." unique_id=1123610167 instance=ExtResource("3_h2yge")]
|
||||
position = Vector2(960, 600)
|
||||
[node name="PlayArea" parent="." unique_id=1123610167 instance=ExtResource("2_1bvp3")]
|
||||
position = Vector2(960, 510)
|
||||
|
||||
[node name="EnemyController" parent="." unique_id=1894449838 instance=ExtResource("4_1bvp3")]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user