From bfcd48e6573043361141d949d1124a357b821e58 Mon Sep 17 00:00:00 2001 From: cojoedmo Date: Sun, 5 Jul 2026 02:51:09 -0400 Subject: [PATCH] think i finally solved movement going into enemy territory --- Map.cs | 2 +- Peg.cs | 21 +++------------------ Pegs/Actions/BasicMovement.cs | 4 +++- Pegs/Actions/ShootShortbow.cs | 7 +------ Pegs/Actions/SwingShortsword.cs | 8 +------- Pegs/Actions/ThrustSpear.cs | 2 +- Pegs/HostilePeg.cs | 2 +- Pegs/PegAction.cs | 5 +++-- PlayArea.cs | 1 - 9 files changed, 14 insertions(+), 38 deletions(-) diff --git a/Map.cs b/Map.cs index 46d26e0..c395111 100644 --- a/Map.cs +++ b/Map.cs @@ -115,7 +115,7 @@ public partial class Map : TileMapLayer public int GetCellDisposition(Vector2I ADDRESS) { - return (int)GetCellTileData(ADDRESS).GetCustomData("disposition"); + return (int)GetCellTileData(ADDRESS).GetCustomData(_dispositionString); } public MapCellOccupant GetOccupant(Vector2I CELL_TO_CHECK) diff --git a/Peg.cs b/Peg.cs index a578f22..0a4928a 100644 --- a/Peg.cs +++ b/Peg.cs @@ -29,16 +29,7 @@ public partial class Peg : MapCellOccupant } public virtual PegAction SelectAction() { - PegAction action = null; - for (int i = 0; i < _actions.Count; i++) - { - if (_actions[i].MeetsCriteria(this)) - { - action = _actions[i]; - break; - } - } - return action; + return _actions.FirstOrDefault(a => a.MeetsCriteria(this), null); } public virtual void CounterAct(Commander COMMANDER) @@ -73,14 +64,8 @@ public partial class Peg : MapCellOccupant public virtual MapCell Goal() { - Map map = _pegController._playArea._map; - Dictionary visible = GetVisibleCells(); - Dictionary unoccupied = visible.Where(c => !map._astar.IsPointSolid(c.Key)).ToDictionary(); - Dictionary enemyTerritory = map._cells.Where(c => _map.GetCellDisposition(c.Value._address) == -(int)_disposition).ToDictionary(); - Dictionary closest = enemyTerritory.OrderByDescending(c => c.Value._address.Y).ThenByDescending(c => Math.Abs(c.Value._address.X - _address.X)).ToDictionary(); - - // Dictionary closest = unoccupied.OrderByDescending(c => c.Value._address.Y * -(int)_disposition).ThenByDescending(c => Math.Abs(c.Value._address.X - _address.X)).ToDictionary(); - // Dictionary closest = furthest.Where(c => _map.GetCellDisposition(c.Value._address) == -(int)_disposition).ToDictionary(); + Dictionary enemyTerritory = _map._cells.Where(c => _map.GetCellDisposition(c.Value._address) == (_disposition == Disposition.FRIENDLY ? -1 : 1)).ToDictionary(); + Dictionary closest = enemyTerritory.OrderByDescending(c => c.Value._address.Y).ThenBy(c => Math.Abs(c.Value._address.X - _address.X)).ToDictionary(); return closest.ElementAt(0).Value; } diff --git a/Pegs/Actions/BasicMovement.cs b/Pegs/Actions/BasicMovement.cs index ee4d942..ac68eb2 100644 --- a/Pegs/Actions/BasicMovement.cs +++ b/Pegs/Actions/BasicMovement.cs @@ -48,7 +48,9 @@ public partial class BasicMovement : PegAction public override bool MeetsCriteria(Peg PEG) { List bestPath = PEG.GetBestPath(true); - return base.MeetsCriteria(PEG) && bestPath.Count > 0; + return base.MeetsCriteria(PEG) + && bestPath.Count > 0 + && PEG._map.GetCellDisposition(bestPath[0]._address) != -(int)PEG._disposition; } diff --git a/Pegs/Actions/ShootShortbow.cs b/Pegs/Actions/ShootShortbow.cs index 6fd00f1..e7cb1ba 100644 --- a/Pegs/Actions/ShootShortbow.cs +++ b/Pegs/Actions/ShootShortbow.cs @@ -25,16 +25,11 @@ public partial class ShootShortbow : PegAction subtween.TweenProperty(_image, "global_position", target.GlobalPosition, 0.5f); subtween.TweenCallback(Callable.From(() => { - ((Peg)target._occupant)._pegController._playerController.ChangeHealth(-2, this); + PEG._pegController._playerController.ChangeHealth(-2, this); Position = Vector2.Zero; Visible = false; })); return subtween; } - - public override MapCell Target(Peg PEG) - { - return PEG.Goal(); - } } diff --git a/Pegs/Actions/SwingShortsword.cs b/Pegs/Actions/SwingShortsword.cs index 6e37a04..36f2541 100644 --- a/Pegs/Actions/SwingShortsword.cs +++ b/Pegs/Actions/SwingShortsword.cs @@ -26,16 +26,10 @@ public partial class SwingShortsword : PegAction subtween.TweenProperty(_image, "global_position", target.GlobalPosition, 0.5f); subtween.TweenCallback(Callable.From(() => { - ((Peg)target._occupant)._pegController._playerController.ChangeHealth(-2, this); + PEG._pegController._playerController.ChangeHealth(-2, this); _image.Position = Vector2.Zero; _image.Visible = false; })); return subtween; } - - public override MapCell Target(Peg PEG) - { - return PEG.Goal(); - } - } diff --git a/Pegs/Actions/ThrustSpear.cs b/Pegs/Actions/ThrustSpear.cs index 2bb4d28..159fa51 100644 --- a/Pegs/Actions/ThrustSpear.cs +++ b/Pegs/Actions/ThrustSpear.cs @@ -28,7 +28,7 @@ public partial class ThrustSpear : PegAction subtween.TweenProperty(_image, "global_position", target.GlobalPosition, 0.5f); subtween.TweenCallback(Callable.From(() => { - ((Peg)target._occupant).ChangeHealth(-1); + ((Peg)target?._occupant)?.ChangeHealth(-1); _image.Position = Vector2.Zero; _image.Visible = false; })); diff --git a/Pegs/HostilePeg.cs b/Pegs/HostilePeg.cs index 8805fae..4057ad7 100644 --- a/Pegs/HostilePeg.cs +++ b/Pegs/HostilePeg.cs @@ -11,7 +11,7 @@ public partial class HostilePeg : Peg { base._Ready(); - _disposition = Disposition.FRIENDLY; + _disposition = Disposition.HOSTILE; } } diff --git a/Pegs/PegAction.cs b/Pegs/PegAction.cs index e13d1e4..aae2789 100644 --- a/Pegs/PegAction.cs +++ b/Pegs/PegAction.cs @@ -25,9 +25,10 @@ public partial class PegAction : Node2D public virtual bool MeetsCriteria(Peg PEG) { + MapCell target = Target(PEG); return PEG._staminaRemaining >= _cost && _usesRemaining > 0 - && (PEG._address - Target(PEG)._address).Length() <= _range; + && (PEG._address - target._address).Length() <= _range; } public virtual void Reset() @@ -37,6 +38,6 @@ public partial class PegAction : Node2D public virtual MapCell Target(Peg PEG) { - return PEG._cell; + return PEG.Goal(); } } diff --git a/PlayArea.cs b/PlayArea.cs index 2ce7121..3540b89 100644 --- a/PlayArea.cs +++ b/PlayArea.cs @@ -36,7 +36,6 @@ public partial class PlayArea : Node2D MapCell c = _map._cells.ElementAt(i).Value; if (c._occupant != null) { - GD.Print(c._occupant); occupiedSpaces.SetCell(c._address, 0, new Vector2I(4,0)); } else if (_map._astar.IsPointSolid(c._address))