mostly fixed movement but there's still a problem with enemies in the top left cell (0,0) being set to not solid

This commit is contained in:
2026-06-09 18:11:06 -04:00
parent e209861cb3
commit 4722cf882b
7 changed files with 114 additions and 40 deletions
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 735 B

After

Width:  |  Height:  |  Size: 3.0 KiB

+15 -1
View File
@@ -7,6 +7,8 @@ public partial class Enemy : StaticBody2D
{
[Signal]
public delegate void DeathEventHandler(Enemy THIS);
[Signal]
public delegate void ClickedEventHandler(Enemy THIS);
public bool _hovered = false, _track = false;
public int _damage = 1, _health = 2, _speed, _speedRemaining, _visibilityRange = 4;
public Vector2I _address, _range = Vector2I.Up;
@@ -18,6 +20,18 @@ public partial class Enemy : StaticBody2D
{
base._Ready();
}
public override void _Process(double delta)
{
base._Process(delta);
if (_hovered)
{
if (Input.IsActionJustPressed("leftClick"))
{
EmitSignal(SignalName.Clicked, this);
}
}
}
public override void _PhysicsProcess(double delta)
{
base._PhysicsProcess(delta);
@@ -47,6 +61,6 @@ public partial class Enemy : StaticBody2D
_hovered = true;
}
public void OnMouseExited(){
_hovered = true;
_hovered = false;
}
}
+47 -9
View File
@@ -17,6 +17,7 @@ public partial class EnemyController : TurnController
{
Enemy newEnemy = _enemyScene.Instantiate<Enemy>();
newEnemy.Death += RemoveEnemy;
newEnemy.Clicked += HandleEnemyClick;
newEnemy._speed = Globals._rng.Next(2,4+1);
newEnemy.Modulate = new Color(newEnemy._speed == 2 ? "#FF0000" : newEnemy._speed == 3 ? "#00FF00" : "#0000FF");
@@ -34,6 +35,7 @@ public partial class EnemyController : TurnController
{
Enemy newEnemy = _enemyScene.Instantiate<Enemy>();
newEnemy.Death += RemoveEnemy;
newEnemy.Clicked += HandleEnemyClick;
newEnemy._speed = Globals._rng.Next(2,4+1);
newEnemy._speed = 3;
@@ -47,11 +49,6 @@ public partial class EnemyController : TurnController
}
// public List<Vector2I> GetEnemyPath(Enemy ENEMY, Vector2I PATH)
// {
// }
public Vector2I GetBestGoal(Enemy ENEMY, Vector2I GOAL)
{
Vector2I goal = GOAL;
@@ -88,6 +85,27 @@ public partial class EnemyController : TurnController
return goal;
}
public void HandleEnemyClick(Enemy ENEMY)
{
GD.Print(ENEMY._address, _playArea._map._astar.IsPointSolid(ENEMY._address));
if (ENEMY._speedRemaining <= 0){
return;
}
TileMapLayer pathLayer = _playArea.GetNode<TileMapLayer>("PathLayer");
Vector2I goal = GetBestGoal(ENEMY, new(ENEMY._address.X, Math.Max(ENEMY._address.Y - ENEMY._visibilityRange, _playArea._map._minY)));
if (ENEMY._address == Vector2I.Zero)
{
GD.Print(0,goal);
}
List<Vector2I> newPath = [.. _playArea._map.GetPath(ENEMY._address, goal)];
pathLayer.GetUsedCells().ToList().ForEach(c => pathLayer.SetCell(c,0,Vector2I.Down*4));
for (int i = 0; i < newPath.Count; i++)
{
pathLayer.SetCell(newPath[i],0,Vector2I.One);
}
}
public void Initiate()
{
List<Vector2I> positions = [.. _playArea.GetNode<TileMapLayer>("InitialPositions").GetUsedCells()];
@@ -105,7 +123,15 @@ public partial class EnemyController : TurnController
if (enemy._speedRemaining > 0)
{
Vector2I goal = GetBestGoal(enemy, new(enemy._address.X, Math.Max(enemy._address.Y - enemy._visibilityRange, _playArea._map._minY)));
if (enemy._address == Vector2I.Zero)
{
GD.Print(1,goal);
}
enemy._path = _playArea._map.GetPath(enemy._address, goal);
if (enemy._path.Count <= 0)
{
enemy._speedRemaining = 0;
}
}
}
@@ -127,12 +153,24 @@ public partial class EnemyController : TurnController
}
if (enemy._address.Y <= _playArea._map._minY)
{
enemy._speedRemaining = 0;
continue;
}
Vector2I goal = GetBestGoal(enemy, new(enemy._address.X, Math.Max(enemy._address.Y - enemy._visibilityRange, _playArea._map._minY)));
if (enemy._address == Vector2I.Zero)
{
GD.Print(2,goal);
}
List<Vector2I> path = _playArea._map.GetPath(enemy._address, goal);
if (path.Count == 0)
{
continue;
}
Vector2I cell = path[0];
if (enemy._address == Vector2I.Zero)
{
GD.Print(3, enemy._address);
}
_playArea._map.SetCellEnemy(cell, enemy);
enemy._path.Add(cell);
enemy._speedRemaining--;
@@ -166,7 +204,7 @@ public partial class EnemyController : TurnController
}
tween.TweenCallback(Callable.From(() => EmitSignal(SignalName.TurnDone)));
_playArea._map.HighlightCells();
_playArea.HighlightCells();
}
public void RemoveEnemy(Enemy ENEMY_TO_REMOVE)
@@ -178,7 +216,7 @@ public partial class EnemyController : TurnController
public List<Enemy> SortEnemies(List<Enemy> ENEMIES)
{
return [.. ENEMIES.Where(e => e._speedRemaining > 0 && _playArea._map.GetPath(e._address, new Vector2I(e._address.X, Math.Max(e._address.Y - e._visibilityRange, _playArea._map._minY))).Count > 0).OrderByDescending(e => e._path.Count).ThenBy(e => e._address.Y).ThenBy(e => Math.Abs(e._address.X - _playArea._map._maxX / 2))];
return [.. ENEMIES.Where(e => e._speedRemaining > 0).OrderByDescending(e => e._path.Count).ThenBy(e => e._address.Y).ThenBy(e => Math.Abs(e._address.X - _playArea._map._maxX / 2))];
}
public override void StartTurn()
@@ -191,7 +229,7 @@ public partial class EnemyController : TurnController
public void SetEnemy(Enemy ENEMY, Vector2I CELL)
{
if (CELL == new Vector2I(5, 7))
if (CELL == new Vector2I(16, 14))
{
ENEMY._track = true;
}
+19 -30
View File
@@ -73,25 +73,6 @@ public partial class Map : TileMapLayer
return GetOccupant(CELL_TO_CHECK) != null;
}
public void HighlightCells()
{
_cells.ForEach(c =>
{
if (HasOccupant(c))
{
SetCell(c, 0, new Vector2I(4,1));
}
else if (_astar.IsPointSolid(c))
{
}
else
{
SetCell(c, 0, new Vector2I(0, (c.X + c.Y) % 2 == 0 ? 0 : 1));
}
});
}
public bool IsCellSolid(Vector2I CELL_TO_CHECK)
{
bool hasOccupant = HasOccupant(CELL_TO_CHECK);
@@ -105,7 +86,7 @@ public partial class Map : TileMapLayer
{
List<Vector2I> rowCells = [.. _cells.Where(c => c.Y == ROW_TO_CHECK)];
return rowCells.All(c => HasOccupant(c));
return rowCells.All(c => _astar.IsPointSolid(c));
}
public void SetCellEnemy(Vector2I ADDRESS, Enemy ENEMY)
@@ -135,20 +116,28 @@ public partial class Map : TileMapLayer
EvaluateSolidCells();
}
public List<Vector2I> GetPath(Vector2I FROM, Vector2I TO, bool INCLUDE_FROM = false, bool SHOW_PATH = false)
public List<Vector2I> GetPath(Vector2I FROM, Vector2I TO, bool INCLUDE_FROM = false)
{
_astar.SetPointSolid(FROM, false);
if (FROM == Vector2I.Zero)
{
GD.Print(4,_astar.IsPointSolid(FROM));
}
if (TO == Vector2I.Zero)
{
GD.Print(5,_astar.IsPointSolid(FROM));
}
List<Vector2I> pathTaken = [.. _astar.GetIdPath(FROM, TO, true)];
_astar.SetPointSolid(FROM, true);
// if (SHOW_PATH)
// {
// for (int i = 0; i < pathTaken.Count; i++)
// {
// Vector2I cell = pathTaken[i];
// SetCell(cell, _mainSource, _pathTakenAtlasCoordinates);
// }
// }
if (FROM == Vector2I.Zero)
{
GD.Print(6,_astar.IsPointSolid(FROM));
}
if (TO == Vector2I.Zero)
{
GD.Print(7,_astar.IsPointSolid(FROM));
}
if (!INCLUDE_FROM)
{
pathTaken.Remove(FROM);
+20
View File
@@ -30,4 +30,24 @@ public partial class PlayArea : Node2D
_bucket.Move();
}
public void HighlightCells()
{
TileMapLayer occupiedSpaces = GetNode<TileMapLayer>("OccupiedSpaces");
_map._cells.ForEach(c =>
{
if (_map.HasOccupant(c))
{
occupiedSpaces.SetCell(c, 0, new Vector2I(4,0));
}
else if (_map._astar.IsPointSolid(c))
{
}
else
{
occupiedSpaces.SetCell(c, 0, Vector2I.Down*4);
}
});
}
}
+4
View File
@@ -10,6 +10,7 @@ bounce = 0.5
radius = 12.5
[node name="Enemy" type="StaticBody2D" unique_id=1417697759]
input_pickable = true
physics_material_override = SubResource("PhysicsMaterial_7k104")
script = ExtResource("1_4gyqm")
@@ -19,3 +20,6 @@ shape = SubResource("CircleShape2D_4gyqm")
[node name="Sprite2D" type="Sprite2D" parent="." unique_id=1941012605]
scale = Vector2(0.5, 0.5)
texture = ExtResource("1_7k104")
[connection signal="mouse_entered" from="." to="." method="OnMouseEntered"]
[connection signal="mouse_exited" from="." to="." method="OnMouseExited"]
+9
View File
File diff suppressed because one or more lines are too long