continuing to switch to MapCell orientation

This commit is contained in:
2026-07-03 02:16:29 -04:00
parent e6355167e3
commit 336b72e4cb
13 changed files with 81 additions and 82 deletions
+2
View File
@@ -23,6 +23,8 @@ public partial class Main : Node
_playerController._playArea = _playArea; _playerController._playArea = _playArea;
_pegController._playArea = _playArea; _pegController._playArea = _playArea;
_playerController._map = _playArea._map;
_pegController._map = _playArea._map;
_playerController.TurnDone += ChangeTurn; _playerController.TurnDone += ChangeTurn;
_playerController.Death += EndGame; _playerController.Death += EndGame;
+15 -9
View File
@@ -3,6 +3,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Dynamic; using System.Dynamic;
using System.Linq; using System.Linq;
using System.Reflection.Metadata.Ecma335;
public partial class Map : TileMapLayer public partial class Map : TileMapLayer
{ {
@@ -109,6 +110,11 @@ public partial class Map : TileMapLayer
} }
return new Vector2I((int)Math.Floor(POSITION.X / _cellSize.X), (int)Math.Floor(POSITION.Y / _cellSize.Y)); return new Vector2I((int)Math.Floor(POSITION.X / _cellSize.X), (int)Math.Floor(POSITION.Y / _cellSize.Y));
} }
public Vector2 GetCellPosition(MapCell CELL)
{
return CELL.GlobalPosition;
}
public Vector2 GetCellPositionFromAddress(Vector2I CELL_ADDRESS) public Vector2 GetCellPositionFromAddress(Vector2I CELL_ADDRESS)
{ {
return GlobalPosition + CELL_ADDRESS * _cellSize + _cellSize / 2; return GlobalPosition + CELL_ADDRESS * _cellSize + _cellSize / 2;
@@ -147,24 +153,24 @@ public partial class Map : TileMapLayer
return rowCells.All(_astar.IsPointSolid); return rowCells.All(_astar.IsPointSolid);
} }
public void SetCellPeg(Vector2I ADDRESS, Peg PEG) public void SetCellPeg(MapCell CELL, Peg PEG)
{ {
if (PEG != null) if (PEG != null)
{ {
if (PEG._address != -Vector2I.One) if (PEG._address != -Vector2I.One)
{ {
_cells[PEG._address]._occupant = null; CELL._occupant = null;
SetCellSolid(PEG._address); SetCellSolid(_cells[PEG._address]);
} }
PEG._address = ADDRESS; PEG._address = CELL._address;
} }
_cells[ADDRESS]._occupant = PEG; CELL._occupant = PEG;
SetCellSolid(ADDRESS); SetCellSolid(CELL);
} }
public void SetCellSolid(Vector2I ADDRESS) public void SetCellSolid(MapCell CELL)
{ {
_astar.SetPointSolid(ADDRESS, IsCellSolid(ADDRESS)); _astar.SetPointSolid(CELL._address, IsCellSolid(CELL._address));
// _astar.SetPointWeightScale(ADDRESS, IsCellSolid(ADDRESS) ? 1000 : 1); // _astar.SetPointWeightScale(ADDRESS, IsCellSolid(ADDRESS) ? 1000 : 1);
} }
@@ -200,7 +206,7 @@ public partial class Map : TileMapLayer
{ {
for (int i = 0; i < _cells.Count; i++) for (int i = 0; i < _cells.Count; i++)
{ {
SetCellSolid(_cells.ElementAt(i).Value._address); SetCellSolid(_cells.ElementAt(i).Value);
} }
} }
+1
View File
@@ -4,6 +4,7 @@ using System;
public partial class MapCellOccupant : HoverableNode public partial class MapCellOccupant : HoverableNode
{ {
public Vector2I _address; public Vector2I _address;
public MapCell _cell;
public Disposition _disposition = Disposition.NONE; public Disposition _disposition = Disposition.NONE;
public enum Disposition public enum Disposition
{ {
+7 -6
View File
@@ -59,7 +59,8 @@ public partial class Peg : MapCellOccupant
// } // }
// } // }
return _pegController._playArea._map.GetPath(_address, Goal(), false, PARTIAL); MapCell goal = Goal();
return _pegController._playArea._map.GetPath(_address, goal._address, false, PARTIAL);
} }
public virtual Dictionary<Vector2I, MapCell> GetEnemies() public virtual Dictionary<Vector2I, MapCell> GetEnemies()
@@ -87,13 +88,13 @@ public partial class Peg : MapCellOccupant
return _pegController._playArea._map.GetCellPositionFromAddress(_address); return _pegController._playArea._map.GetCellPositionFromAddress(_address);
} }
public virtual Vector2I Goal() public virtual MapCell Goal()
{ {
Map map = _pegController._playArea._map; Map map = _pegController._playArea._map;
List<Vector2I> visible = GetVisibleCells(); Dictionary<Vector2I, MapCell> visible = GetVisibleCells();
List<Vector2I> unoccupied = [.. visible.Where(c => !map._astar.IsPointSolid(c))]; Dictionary<Vector2I, MapCell> unoccupied = visible.Where(c => !map._astar.IsPointSolid(c.Key)).ToDictionary();
List<Vector2I> closest = [.. unoccupied.OrderByDescending(c => c.Y * _disposition).ThenByDescending(c => Math.Abs(c.X - _address.X))]; Dictionary<Vector2I, MapCell> closest = unoccupied.OrderByDescending(c => c.Value._address.Y * (int)_disposition).ThenByDescending(c => Math.Abs(c.Value._address.X - _address.X)).ToDictionary();
return closest[0]; return closest.ElementAt(0).Value;
} }
public virtual void StartTurn() public virtual void StartTurn()
+14 -12
View File
@@ -41,8 +41,8 @@ public partial class PegController : TurnController
newFriendlyPeg._pegController = this; newFriendlyPeg._pegController = this;
List<Vector2I> unoccupied = [.. _playArea._map._bottomRow.Where(c => _pegs.All(e => e._address != c))]; Dictionary<Vector2I, MapCell> unoccupied = _playArea._map._bottomRow.Where(c => c.Value._occupant == null).ToDictionary();
Vector2I randomCell = unoccupied[Globals._rng.Next(unoccupied.Count)]; MapCell randomCell = unoccupied.ElementAt(Globals._rng.Next(unoccupied.Keys.Count)).Value;
SetPeg(newFriendlyPeg, randomCell); SetPeg(newFriendlyPeg, randomCell);
_pegs.Add(newFriendlyPeg); _pegs.Add(newFriendlyPeg);
@@ -62,7 +62,7 @@ public partial class PegController : TurnController
newFriendlyPeg._pegController = this; newFriendlyPeg._pegController = this;
SetPeg(newFriendlyPeg, POSITIONS[i]); SetPeg(newFriendlyPeg, _map._cells[POSITIONS[i]]);
_pegs.Add(newFriendlyPeg); _pegs.Add(newFriendlyPeg);
AddChild(newFriendlyPeg); AddChild(newFriendlyPeg);
_pegsCreated++; _pegsCreated++;
@@ -80,8 +80,8 @@ public partial class PegController : TurnController
newHostilePeg._pegController = this; newHostilePeg._pegController = this;
List<Vector2I> unoccupied = [.. _playArea._map._bottomRow.Where(c => _pegs.All(e => e._address != c))]; Dictionary<Vector2I, MapCell> unoccupied = _map._bottomRow.Where(c => c.Value._occupant == null).ToDictionary();
Vector2I randomCell = unoccupied[Globals._rng.Next(unoccupied.Count)]; MapCell randomCell = unoccupied.ElementAt(Globals._rng.Next(unoccupied.Count)).Value;
SetPeg(newHostilePeg, randomCell); SetPeg(newHostilePeg, randomCell);
_pegs.Add(newHostilePeg); _pegs.Add(newHostilePeg);
@@ -100,8 +100,9 @@ public partial class PegController : TurnController
newHostilePeg.Click += HandlePegClick; newHostilePeg.Click += HandlePegClick;
newHostilePeg._pegController = this; newHostilePeg._pegController = this;
newHostilePeg._map = _playArea._map;
SetPeg(newHostilePeg, POSITIONS[i]); SetPeg(newHostilePeg, _map._cells[POSITIONS[i]]);
_pegs.Add(newHostilePeg); _pegs.Add(newHostilePeg);
AddChild(newHostilePeg); AddChild(newHostilePeg);
_pegsCreated++; _pegsCreated++;
@@ -162,12 +163,12 @@ public partial class PegController : TurnController
Map map = _playArea._map; Map map = _playArea._map;
TileMapLayer pathLayer = _playArea.GetNode<TileMapLayer>("PathLayer"); TileMapLayer pathLayer = _playArea.GetNode<TileMapLayer>("PathLayer");
List<Vector2I> pl = [.. pathLayer.GetUsedCells()]; List<Vector2I> pl = [.. pathLayer.GetUsedCells()];
List<Vector2I> visible = peg.GetVisibleCells(); Dictionary<Vector2I, MapCell> visible = peg.GetVisibleCells();
for (int i = 0; i < pl.Count; i++) for (int i = 0; i < pl.Count; i++)
{ {
Vector2I cell = pl[i]; Vector2I cell = pl[i];
pathLayer.GetUsedCells().ToList().ForEach(c => pathLayer.SetCell(cell,0,Vector2I.Down + Vector2I.Right*(visible.IndexOf(cell) > -1 ? 1 : 4))); pathLayer.GetUsedCells().ToList().ForEach(c => pathLayer.SetCell(cell,0,Vector2I.Down + Vector2I.Right*(visible.ContainsKey(cell) ? 1 : 4)));
} }
} }
@@ -180,7 +181,7 @@ public partial class PegController : TurnController
public void HandlePegRemoval(Peg PEG_TO_REMOVE) public void HandlePegRemoval(Peg PEG_TO_REMOVE)
{ {
_pegs.Remove(PEG_TO_REMOVE); _pegs.Remove(PEG_TO_REMOVE);
_playArea._map.SetCellPeg(PEG_TO_REMOVE._address, null); _playArea._map.SetCellPeg(_map._cells[PEG_TO_REMOVE._address], null);
PEG_TO_REMOVE.QueueFree(); PEG_TO_REMOVE.QueueFree();
_pegsDestroyed++; _pegsDestroyed++;
} }
@@ -246,10 +247,11 @@ public partial class PegController : TurnController
HandlePegTurn(); HandlePegTurn();
} }
public void SetPeg(Peg PEG, Vector2I CELL) public void SetPeg(Peg PEG, MapCell CELL)
{ {
_playArea._map.SetCellPeg(CELL, PEG); _map.SetCellPeg(CELL, PEG);
PEG.GlobalPosition = _playArea._map.GetCellPositionFromAddress(CELL); PEG._cell = CELL;
PEG.GlobalPosition = CELL.GlobalPosition;
} }
} }
+2 -2
View File
@@ -41,14 +41,14 @@ public partial class BasicMovement : PegAction
} }
Vector2I cell = path[0]; Vector2I cell = path[0];
map.SetCellPeg(cell, PEG); map.SetCellPeg(map._cells[cell], PEG);
PEG._path.Add(cell); PEG._path.Add(cell);
} }
public override bool MeetsCriteria(Peg PEG) public override bool MeetsCriteria(Peg PEG)
{ {
List<Vector2I> bestPath = PEG.GetBestPath(true); List<Vector2I> bestPath = PEG.GetBestPath(true);
return base.MeetsCriteria(PEG) && bestPath.Count > 0 && (int)PEG._pegController._playArea._map.GetCellTileData(bestPath[0]).GetCustomData("disposition") != -PEG._disposition; return base.MeetsCriteria(PEG) && bestPath.Count > 0 && (int)PEG._pegController._playArea._map.GetCellTileData(bestPath[0]).GetCustomData("disposition") != -(int)PEG._disposition;
} }
+6 -11
View File
@@ -18,28 +18,23 @@ public partial class ShootShortbow : PegAction
public override Tween CreateAnimation(Peg PEG) public override Tween CreateAnimation(Peg PEG)
{ {
Vector2 target = PEG._pegController._playArea._map.GetCellPositionFromAddress(Target(PEG)); MapCell target = Target(PEG);
Tween subtween = CreateTween(); Tween subtween = CreateTween();
subtween.TweenProperty(_image, "visible", true, 0.0f); subtween.TweenProperty(_image, "visible", true, 0.0f);
subtween.TweenProperty(_image, "rotation", PEG.GetAngleTo(target), 0.0f); subtween.TweenProperty(_image, "rotation", PEG.GetAngleTo(target.GlobalPosition), 0.0f);
subtween.TweenProperty(_image, "global_position", target, 0.5f); subtween.TweenProperty(_image, "global_position", target.GlobalPosition, 0.5f);
subtween.TweenCallback(Callable.From(() => subtween.TweenCallback(Callable.From(() =>
{ {
PEG._pegController._playerController.ChangeHealth(-1, this); ((Peg)target._occupant)._pegController._playerController.ChangeHealth(-2, this);
Position = Vector2.Zero; Position = Vector2.Zero;
Visible = false; Visible = false;
})); }));
return subtween; return subtween;
} }
public override Vector2I Target(Peg PEG) public override MapCell Target(Peg PEG)
{ {
List<Vector2I> closest = [.. PEG.GetVisibleCells().Where(c => (int)PEG._pegController._playArea._map.GetCellTileData(c).GetCustomData("disposition") == -PEG._disposition).OrderBy(c => (c - PEG._address).Length())]; return PEG.Goal();
if (closest.Count == 0)
{
return -Vector2I.One;
}
return closest[0]; // return PEG._pegController._playerController._towers.OrderBy(t => (t.GlobalPosition - GlobalPosition).Length()).ToList()[0].GlobalPosition;
} }
} }
+6 -11
View File
@@ -18,29 +18,24 @@ public partial class SwingShortsword : PegAction
public override Tween CreateAnimation(Peg PEG) public override Tween CreateAnimation(Peg PEG)
{ {
Vector2 target = PEG._pegController._playArea._map.GetCellPositionFromAddress(Target(PEG)); MapCell target = Target(PEG);
// GD.Print(target); // GD.Print(target);
Tween subtween = CreateTween(); Tween subtween = CreateTween();
subtween.TweenProperty(_image, "visible", true, 0.0f); subtween.TweenProperty(_image, "visible", true, 0.0f);
subtween.TweenProperty(_image, "rotation", PEG.GetAngleTo(target), 0.0f); subtween.TweenProperty(_image, "rotation", PEG.GetAngleTo(target.GlobalPosition), 0.0f);
subtween.TweenProperty(_image, "global_position", target, 0.5f); subtween.TweenProperty(_image, "global_position", target.GlobalPosition, 0.5f);
subtween.TweenCallback(Callable.From(() => subtween.TweenCallback(Callable.From(() =>
{ {
PEG._pegController._playerController.ChangeHealth(-2, this); ((Peg)target._occupant)._pegController._playerController.ChangeHealth(-2, this);
_image.Position = Vector2.Zero; _image.Position = Vector2.Zero;
_image.Visible = false; _image.Visible = false;
})); }));
return subtween; return subtween;
} }
public override Vector2I Target(Peg PEG) public override MapCell Target(Peg PEG)
{ {
List<Vector2I> closest = [.. PEG.GetVisibleCells().Where(c => (int)PEG._pegController._playArea._map.GetCellTileData(c).GetCustomData("disposition") == -PEG._disposition).OrderBy(c => (c - PEG._address).Length())]; return PEG.Goal();
if (closest.Count == 0)
{
return -Vector2I.One;
}
return closest[0];
} }
} }
+5 -8
View File
@@ -16,25 +16,22 @@ public partial class ThrustSpear : PegAction
public override Tween CreateAnimation(Peg PEG) public override Tween CreateAnimation(Peg PEG)
{ {
GD.Print(Name); MapCell target = Target(PEG);
Map map = PEG._pegController._playArea._map;
Peg pegTarget = map._addressOccupants[Target(PEG)];
Vector2 target = pegTarget.GlobalPosition;
Tween subtween = CreateTween(); Tween subtween = CreateTween();
subtween.TweenProperty(_image, "visible", true, 0.0f); subtween.TweenProperty(_image, "visible", true, 0.0f);
subtween.TweenProperty(_image, "rotation", PEG.GetAngleTo(target), 0.0f); subtween.TweenProperty(_image, "rotation", PEG.GetAngleTo(target.GlobalPosition), 0.0f);
subtween.TweenProperty(_image, "global_position", target, 0.5f); subtween.TweenProperty(_image, "global_position", target.GlobalPosition, 0.5f);
subtween.TweenCallback(Callable.From(() => subtween.TweenCallback(Callable.From(() =>
{ {
pegTarget.ChangeHealth(-1); ((Peg)target._occupant).ChangeHealth(-1);
_image.Position = Vector2.Zero; _image.Position = Vector2.Zero;
_image.Visible = false; _image.Visible = false;
})); }));
return subtween; return subtween;
} }
public override Vector2I Target(Peg PEG) public override MapCell Target(Peg PEG)
{ {
return PEG.Goal(); return PEG.Goal();
} }
+5 -5
View File
@@ -12,15 +12,15 @@ public partial class Spearman : FriendlyPeg
_visibility = 4; _visibility = 4;
} }
public override Vector2I Goal() public override MapCell Goal()
{ {
Map map = _pegController._playArea._map; Map map = _pegController._playArea._map;
List<Peg> enemies = [.. _pegController._pegs.Where(p => p._disposition == -_disposition)]; Dictionary<Vector2I, MapCell> enemies = GetVisibleEnemies();
if (enemies.Count == 0) if (enemies.Count == 0)
{ {
return _address; return _map._cells[_address];
} }
List<Vector2I> closest = [.. enemies.OrderBy(e => (e._address - _address).Length()).Select(e => e._address)]; Dictionary<Vector2I, MapCell> closest = enemies.OrderBy(e => (e.Value._occupant._address - _address).Length()).ToDictionary();
return closest[0]; return closest.ElementAt(0).Value;
} }
} }
+3 -3
View File
@@ -27,7 +27,7 @@ public partial class PegAction : Node2D
{ {
return PEG._staminaRemaining >= _cost return PEG._staminaRemaining >= _cost
&& _usesRemaining > 0 && _usesRemaining > 0
&& (PEG._address - Target(PEG)).Length() <= _range; && (PEG._address - Target(PEG)._address).Length() <= _range;
} }
public virtual void Reset() public virtual void Reset()
@@ -35,8 +35,8 @@ public partial class PegAction : Node2D
_usesRemaining = _usesMax; _usesRemaining = _usesMax;
} }
public virtual Vector2I Target(Peg PEG) public virtual MapCell Target(Peg PEG)
{ {
return PEG._address; return PEG._cell;
} }
} }
+14 -14
View File
@@ -32,21 +32,21 @@ public partial class PlayArea : Node2D
public void HighlightCells() public void HighlightCells()
{ {
TileMapLayer occupiedSpaces = GetNode<TileMapLayer>("OccupiedSpaces"); TileMapLayer occupiedSpaces = GetNode<TileMapLayer>("OccupiedSpaces");
_map._cells.ForEach(c => // _map._cells.ForEach(c =>
{ // {
if (_map.HasOccupant(c)) // if (_map.HasOccupant(c))
{ // {
occupiedSpaces.SetCell(c, 0, new Vector2I(4,0)); // occupiedSpaces.SetCell(c, 0, new Vector2I(4,0));
} // }
else if (_map._astar.IsPointSolid(c)) // else if (_map._astar.IsPointSolid(c))
{ // {
} // }
else // else
{ // {
occupiedSpaces.SetCell(c, 0, Vector2I.Down*4); // occupiedSpaces.SetCell(c, 0, Vector2I.Down*4);
} // }
}); // });
} }
} }
+1 -1
View File
@@ -7,7 +7,7 @@ public partial class TurnController : Node2D
[Signal] [Signal]
public delegate void TurnDoneEventHandler(); public delegate void TurnDoneEventHandler();
public PlayArea _playArea; public PlayArea _playArea;
public Map _map;
public virtual void StartTurn() public virtual void StartTurn()
{ {