From 336b72e4cbd7377a0316a655c02544c7039e3ecd Mon Sep 17 00:00:00 2001 From: cojoedmo Date: Fri, 3 Jul 2026 02:16:29 -0400 Subject: [PATCH] continuing to switch to MapCell orientation --- Main.cs | 2 ++ Map.cs | 24 +++++++++++++++--------- MapCellOccupant.cs | 1 + Peg.cs | 13 +++++++------ PegController.cs | 26 ++++++++++++++------------ Pegs/Actions/BasicMovement.cs | 4 ++-- Pegs/Actions/ShootShortbow.cs | 17 ++++++----------- Pegs/Actions/SwingShortsword.cs | 17 ++++++----------- Pegs/Actions/ThrustSpear.cs | 13 +++++-------- Pegs/FriendlyPegs/Spearman.cs | 10 +++++----- Pegs/PegAction.cs | 6 +++--- PlayArea.cs | 28 ++++++++++++++-------------- TurnController.cs | 2 +- 13 files changed, 81 insertions(+), 82 deletions(-) diff --git a/Main.cs b/Main.cs index 67e2674..7dbe414 100644 --- a/Main.cs +++ b/Main.cs @@ -23,6 +23,8 @@ public partial class Main : Node _playerController._playArea = _playArea; _pegController._playArea = _playArea; + _playerController._map = _playArea._map; + _pegController._map = _playArea._map; _playerController.TurnDone += ChangeTurn; _playerController.Death += EndGame; diff --git a/Map.cs b/Map.cs index 1a69248..9e5d45d 100644 --- a/Map.cs +++ b/Map.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Dynamic; using System.Linq; +using System.Reflection.Metadata.Ecma335; 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)); } + public Vector2 GetCellPosition(MapCell CELL) + { + return CELL.GlobalPosition; + } + public Vector2 GetCellPositionFromAddress(Vector2I CELL_ADDRESS) { return GlobalPosition + CELL_ADDRESS * _cellSize + _cellSize / 2; @@ -147,24 +153,24 @@ public partial class Map : TileMapLayer return rowCells.All(_astar.IsPointSolid); } - public void SetCellPeg(Vector2I ADDRESS, Peg PEG) + public void SetCellPeg(MapCell CELL, Peg PEG) { if (PEG != null) { if (PEG._address != -Vector2I.One) { - _cells[PEG._address]._occupant = null; - SetCellSolid(PEG._address); + CELL._occupant = null; + SetCellSolid(_cells[PEG._address]); } - PEG._address = ADDRESS; + PEG._address = CELL._address; } - _cells[ADDRESS]._occupant = PEG; - SetCellSolid(ADDRESS); + CELL._occupant = PEG; + 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); } @@ -200,7 +206,7 @@ public partial class Map : TileMapLayer { for (int i = 0; i < _cells.Count; i++) { - SetCellSolid(_cells.ElementAt(i).Value._address); + SetCellSolid(_cells.ElementAt(i).Value); } } diff --git a/MapCellOccupant.cs b/MapCellOccupant.cs index 5976e0c..4cdbac0 100644 --- a/MapCellOccupant.cs +++ b/MapCellOccupant.cs @@ -4,6 +4,7 @@ using System; public partial class MapCellOccupant : HoverableNode { public Vector2I _address; + public MapCell _cell; public Disposition _disposition = Disposition.NONE; public enum Disposition { diff --git a/Peg.cs b/Peg.cs index 7bfe497..675b87b 100644 --- a/Peg.cs +++ b/Peg.cs @@ -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 GetEnemies() @@ -87,13 +88,13 @@ public partial class Peg : MapCellOccupant return _pegController._playArea._map.GetCellPositionFromAddress(_address); } - public virtual Vector2I Goal() + public virtual MapCell Goal() { Map map = _pegController._playArea._map; - List visible = GetVisibleCells(); - List unoccupied = [.. visible.Where(c => !map._astar.IsPointSolid(c))]; - List closest = [.. unoccupied.OrderByDescending(c => c.Y * _disposition).ThenByDescending(c => Math.Abs(c.X - _address.X))]; - return closest[0]; + Dictionary visible = GetVisibleCells(); + Dictionary unoccupied = visible.Where(c => !map._astar.IsPointSolid(c.Key)).ToDictionary(); + Dictionary closest = unoccupied.OrderByDescending(c => c.Value._address.Y * (int)_disposition).ThenByDescending(c => Math.Abs(c.Value._address.X - _address.X)).ToDictionary(); + return closest.ElementAt(0).Value; } public virtual void StartTurn() diff --git a/PegController.cs b/PegController.cs index 3be76fd..dbeb2ad 100644 --- a/PegController.cs +++ b/PegController.cs @@ -41,8 +41,8 @@ public partial class PegController : TurnController newFriendlyPeg._pegController = this; - List unoccupied = [.. _playArea._map._bottomRow.Where(c => _pegs.All(e => e._address != c))]; - Vector2I randomCell = unoccupied[Globals._rng.Next(unoccupied.Count)]; + Dictionary unoccupied = _playArea._map._bottomRow.Where(c => c.Value._occupant == null).ToDictionary(); + MapCell randomCell = unoccupied.ElementAt(Globals._rng.Next(unoccupied.Keys.Count)).Value; SetPeg(newFriendlyPeg, randomCell); _pegs.Add(newFriendlyPeg); @@ -62,7 +62,7 @@ public partial class PegController : TurnController newFriendlyPeg._pegController = this; - SetPeg(newFriendlyPeg, POSITIONS[i]); + SetPeg(newFriendlyPeg, _map._cells[POSITIONS[i]]); _pegs.Add(newFriendlyPeg); AddChild(newFriendlyPeg); _pegsCreated++; @@ -80,8 +80,8 @@ public partial class PegController : TurnController newHostilePeg._pegController = this; - List unoccupied = [.. _playArea._map._bottomRow.Where(c => _pegs.All(e => e._address != c))]; - Vector2I randomCell = unoccupied[Globals._rng.Next(unoccupied.Count)]; + Dictionary unoccupied = _map._bottomRow.Where(c => c.Value._occupant == null).ToDictionary(); + MapCell randomCell = unoccupied.ElementAt(Globals._rng.Next(unoccupied.Count)).Value; SetPeg(newHostilePeg, randomCell); _pegs.Add(newHostilePeg); @@ -100,8 +100,9 @@ public partial class PegController : TurnController newHostilePeg.Click += HandlePegClick; newHostilePeg._pegController = this; + newHostilePeg._map = _playArea._map; - SetPeg(newHostilePeg, POSITIONS[i]); + SetPeg(newHostilePeg, _map._cells[POSITIONS[i]]); _pegs.Add(newHostilePeg); AddChild(newHostilePeg); _pegsCreated++; @@ -162,12 +163,12 @@ public partial class PegController : TurnController Map map = _playArea._map; TileMapLayer pathLayer = _playArea.GetNode("PathLayer"); List pl = [.. pathLayer.GetUsedCells()]; - List visible = peg.GetVisibleCells(); + Dictionary visible = peg.GetVisibleCells(); for (int i = 0; i < pl.Count; 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) { _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(); _pegsDestroyed++; } @@ -246,10 +247,11 @@ public partial class PegController : TurnController HandlePegTurn(); } - public void SetPeg(Peg PEG, Vector2I CELL) + public void SetPeg(Peg PEG, MapCell CELL) { - _playArea._map.SetCellPeg(CELL, PEG); - PEG.GlobalPosition = _playArea._map.GetCellPositionFromAddress(CELL); + _map.SetCellPeg(CELL, PEG); + PEG._cell = CELL; + PEG.GlobalPosition = CELL.GlobalPosition; } } diff --git a/Pegs/Actions/BasicMovement.cs b/Pegs/Actions/BasicMovement.cs index 900c4ba..6ef5267 100644 --- a/Pegs/Actions/BasicMovement.cs +++ b/Pegs/Actions/BasicMovement.cs @@ -41,14 +41,14 @@ public partial class BasicMovement : PegAction } Vector2I cell = path[0]; - map.SetCellPeg(cell, PEG); + map.SetCellPeg(map._cells[cell], PEG); PEG._path.Add(cell); } public override bool MeetsCriteria(Peg PEG) { List 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; } diff --git a/Pegs/Actions/ShootShortbow.cs b/Pegs/Actions/ShootShortbow.cs index 3f80b82..6fd00f1 100644 --- a/Pegs/Actions/ShootShortbow.cs +++ b/Pegs/Actions/ShootShortbow.cs @@ -18,28 +18,23 @@ public partial class ShootShortbow : PegAction public override Tween CreateAnimation(Peg PEG) { - Vector2 target = PEG._pegController._playArea._map.GetCellPositionFromAddress(Target(PEG)); + MapCell target = Target(PEG); Tween subtween = CreateTween(); subtween.TweenProperty(_image, "visible", true, 0.0f); - subtween.TweenProperty(_image, "rotation", PEG.GetAngleTo(target), 0.0f); - subtween.TweenProperty(_image, "global_position", target, 0.5f); + subtween.TweenProperty(_image, "rotation", PEG.GetAngleTo(target.GlobalPosition), 0.0f); + subtween.TweenProperty(_image, "global_position", target.GlobalPosition, 0.5f); subtween.TweenCallback(Callable.From(() => { - PEG._pegController._playerController.ChangeHealth(-1, this); + ((Peg)target._occupant)._pegController._playerController.ChangeHealth(-2, this); Position = Vector2.Zero; Visible = false; })); return subtween; } - public override Vector2I Target(Peg PEG) + public override MapCell Target(Peg PEG) { - List closest = [.. PEG.GetVisibleCells().Where(c => (int)PEG._pegController._playArea._map.GetCellTileData(c).GetCustomData("disposition") == -PEG._disposition).OrderBy(c => (c - PEG._address).Length())]; - if (closest.Count == 0) - { - return -Vector2I.One; - } - return closest[0]; // return PEG._pegController._playerController._towers.OrderBy(t => (t.GlobalPosition - GlobalPosition).Length()).ToList()[0].GlobalPosition; + return PEG.Goal(); } } diff --git a/Pegs/Actions/SwingShortsword.cs b/Pegs/Actions/SwingShortsword.cs index 4f5cd7f..6e37a04 100644 --- a/Pegs/Actions/SwingShortsword.cs +++ b/Pegs/Actions/SwingShortsword.cs @@ -18,29 +18,24 @@ public partial class SwingShortsword : PegAction public override Tween CreateAnimation(Peg PEG) { - Vector2 target = PEG._pegController._playArea._map.GetCellPositionFromAddress(Target(PEG)); + MapCell target = Target(PEG); // GD.Print(target); Tween subtween = CreateTween(); subtween.TweenProperty(_image, "visible", true, 0.0f); - subtween.TweenProperty(_image, "rotation", PEG.GetAngleTo(target), 0.0f); - subtween.TweenProperty(_image, "global_position", target, 0.5f); + subtween.TweenProperty(_image, "rotation", PEG.GetAngleTo(target.GlobalPosition), 0.0f); + subtween.TweenProperty(_image, "global_position", target.GlobalPosition, 0.5f); subtween.TweenCallback(Callable.From(() => { - PEG._pegController._playerController.ChangeHealth(-2, this); + ((Peg)target._occupant)._pegController._playerController.ChangeHealth(-2, this); _image.Position = Vector2.Zero; _image.Visible = false; })); return subtween; } - public override Vector2I Target(Peg PEG) + public override MapCell Target(Peg PEG) { - List closest = [.. PEG.GetVisibleCells().Where(c => (int)PEG._pegController._playArea._map.GetCellTileData(c).GetCustomData("disposition") == -PEG._disposition).OrderBy(c => (c - PEG._address).Length())]; - if (closest.Count == 0) - { - return -Vector2I.One; - } - return closest[0]; + return PEG.Goal(); } } diff --git a/Pegs/Actions/ThrustSpear.cs b/Pegs/Actions/ThrustSpear.cs index bc9499d..404fabc 100644 --- a/Pegs/Actions/ThrustSpear.cs +++ b/Pegs/Actions/ThrustSpear.cs @@ -16,25 +16,22 @@ public partial class ThrustSpear : PegAction public override Tween CreateAnimation(Peg PEG) { - GD.Print(Name); - Map map = PEG._pegController._playArea._map; - Peg pegTarget = map._addressOccupants[Target(PEG)]; - Vector2 target = pegTarget.GlobalPosition; + MapCell target = Target(PEG); Tween subtween = CreateTween(); subtween.TweenProperty(_image, "visible", true, 0.0f); - subtween.TweenProperty(_image, "rotation", PEG.GetAngleTo(target), 0.0f); - subtween.TweenProperty(_image, "global_position", target, 0.5f); + subtween.TweenProperty(_image, "rotation", PEG.GetAngleTo(target.GlobalPosition), 0.0f); + subtween.TweenProperty(_image, "global_position", target.GlobalPosition, 0.5f); subtween.TweenCallback(Callable.From(() => { - pegTarget.ChangeHealth(-1); + ((Peg)target._occupant).ChangeHealth(-1); _image.Position = Vector2.Zero; _image.Visible = false; })); return subtween; } - public override Vector2I Target(Peg PEG) + public override MapCell Target(Peg PEG) { return PEG.Goal(); } diff --git a/Pegs/FriendlyPegs/Spearman.cs b/Pegs/FriendlyPegs/Spearman.cs index 0e1146c..3434f0e 100644 --- a/Pegs/FriendlyPegs/Spearman.cs +++ b/Pegs/FriendlyPegs/Spearman.cs @@ -12,15 +12,15 @@ public partial class Spearman : FriendlyPeg _visibility = 4; } - public override Vector2I Goal() + public override MapCell Goal() { Map map = _pegController._playArea._map; - List enemies = [.. _pegController._pegs.Where(p => p._disposition == -_disposition)]; + Dictionary enemies = GetVisibleEnemies(); if (enemies.Count == 0) { - return _address; + return _map._cells[_address]; } - List closest = [.. enemies.OrderBy(e => (e._address - _address).Length()).Select(e => e._address)]; - return closest[0]; + Dictionary closest = enemies.OrderBy(e => (e.Value._occupant._address - _address).Length()).ToDictionary(); + return closest.ElementAt(0).Value; } } diff --git a/Pegs/PegAction.cs b/Pegs/PegAction.cs index e197af0..e13d1e4 100644 --- a/Pegs/PegAction.cs +++ b/Pegs/PegAction.cs @@ -27,7 +27,7 @@ public partial class PegAction : Node2D { return PEG._staminaRemaining >= _cost && _usesRemaining > 0 - && (PEG._address - Target(PEG)).Length() <= _range; + && (PEG._address - Target(PEG)._address).Length() <= _range; } public virtual void Reset() @@ -35,8 +35,8 @@ public partial class PegAction : Node2D _usesRemaining = _usesMax; } - public virtual Vector2I Target(Peg PEG) + public virtual MapCell Target(Peg PEG) { - return PEG._address; + return PEG._cell; } } diff --git a/PlayArea.cs b/PlayArea.cs index 1d3b0f3..c1f51ec 100644 --- a/PlayArea.cs +++ b/PlayArea.cs @@ -32,21 +32,21 @@ public partial class PlayArea : Node2D public void HighlightCells() { TileMapLayer occupiedSpaces = GetNode("OccupiedSpaces"); - _map._cells.ForEach(c => - { - if (_map.HasOccupant(c)) - { - occupiedSpaces.SetCell(c, 0, new Vector2I(4,0)); - } - else if (_map._astar.IsPointSolid(c)) - { + // _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); - } - }); + // } + // else + // { + // occupiedSpaces.SetCell(c, 0, Vector2I.Down*4); + // } + // }); } } diff --git a/TurnController.cs b/TurnController.cs index df1713d..a6754c9 100644 --- a/TurnController.cs +++ b/TurnController.cs @@ -7,7 +7,7 @@ public partial class TurnController : Node2D [Signal] public delegate void TurnDoneEventHandler(); public PlayArea _playArea; - + public Map _map; public virtual void StartTurn() {