diff --git a/Map.cs b/Map.cs index 5bb09c7..1a69248 100644 --- a/Map.cs +++ b/Map.cs @@ -6,19 +6,12 @@ using System.Linq; public partial class Map : TileMapLayer { + public PackedScene _mapCellScene = GD.Load("res://map_cell.tscn"); public int _minX, _maxX, _minY, _maxY, _mainSource = 0; - public string _isSolidString = "is_solid"; + public string _isSolidString = "is_solid", _dispositionString = "disposition"; public Vector2 _cellSize, _sizeInPixels, _sizeInCells; - public List _leftmostColumn = new(), _rightmostColumn = new(), _topRow = new(), _bottomRow = new(); public AStarGrid2D _astar = new(); - public Dictionary _addressOccupants = new(); - public List _cells - { - get - { - return [.. GetUsedCells()]; - } - } + public Dictionary _cells = new(), _leftmostColumn = new(), _rightmostColumn = new(), _topRow = new(), _bottomRow = new(); public int _firstOpenRow { get @@ -78,16 +71,26 @@ public partial class Map : TileMapLayer public override void _Ready() { base._Ready(); + List usedCells = [.. GetUsedCells()]; _cellSize = TileSet.TileSize; - _cells.ForEach(c => _addressOccupants[c] = null); - _minX = _cells.Min(c => c.X); - _maxX = _cells.Max(c => c.X); - _minY = _cells.Min(c => c.Y); - _maxY = _cells.Max(c => c.Y); - _leftmostColumn = [.. _cells.Where(c => c.X == _minX)]; - _rightmostColumn = [.. _cells.Where(c => c.X == _maxX)]; - _topRow = [.. _cells.Where(c => c.Y == _minY)]; - _bottomRow = [.. _cells.Where(c => c.Y == _maxY)]; + for (int i = 0; i < usedCells.Count; i++) + { + MapCell newMapCell = _mapCellScene.Instantiate(); + Vector2I usedCell = usedCells[i]; + newMapCell._address = usedCell; + newMapCell.GlobalPosition = GetCellPositionFromAddress(newMapCell._address); + newMapCell._occupant = null; + _cells[usedCell] = newMapCell; + AddChild(newMapCell); + } + _minX = _cells.Min(c => c.Value._address.X); + _maxX = _cells.Max(c => c.Value._address.X); + _minY = _cells.Min(c => c.Value._address.Y); + _maxY = _cells.Max(c => c.Value._address.Y); + _leftmostColumn = _cells.Where(c => c.Value._address.X == _minX).ToDictionary(); + _rightmostColumn = _cells.Where(c => c.Value._address.X == _maxX).ToDictionary(); + _topRow = _cells.Where(c => c.Value._address.Y == _minY).ToDictionary(); + _bottomRow = _cells.Where(c => c.Value._address.Y == _maxY).ToDictionary(); _sizeInCells = new Vector2(_topRow.Count, _leftmostColumn.Count); _sizeInPixels = _sizeInCells * _cellSize; @@ -111,9 +114,9 @@ public partial class Map : TileMapLayer return GlobalPosition + CELL_ADDRESS * _cellSize + _cellSize / 2; } - public Peg GetOccupant(Vector2I CELL_TO_CHECK) + public MapCellOccupant GetOccupant(Vector2I CELL_TO_CHECK) { - return _addressOccupants[CELL_TO_CHECK]; + return _cells[CELL_TO_CHECK]._occupant; } public bool HasOccupant(Vector2I CELL_TO_CHECK) @@ -132,16 +135,16 @@ public partial class Map : TileMapLayer public bool IsColumnnFull(int COLUMN_TO_CHECK) { - List rowCells = [.. _cells.Where(c => c.X == COLUMN_TO_CHECK)]; + List rowCells = [.. _cells.Where(c => c.Value._address.X == COLUMN_TO_CHECK).ToDictionary().Keys]; - return rowCells.All(c => _astar.IsPointSolid(c)); + return rowCells.All(_astar.IsPointSolid); } public bool IsRowFull(int ROW_TO_CHECK) { - List rowCells = [.. _cells.Where(c => c.Y == ROW_TO_CHECK)]; + List rowCells = [.. _cells.Where(c => c.Value._address.Y == ROW_TO_CHECK).ToDictionary().Keys]; - return rowCells.All(c => _astar.IsPointSolid(c)); + return rowCells.All(_astar.IsPointSolid); } public void SetCellPeg(Vector2I ADDRESS, Peg PEG) @@ -150,12 +153,12 @@ public partial class Map : TileMapLayer { if (PEG._address != -Vector2I.One) { - _addressOccupants[PEG._address] = null; + _cells[PEG._address]._occupant = null; SetCellSolid(PEG._address); } PEG._address = ADDRESS; } - _addressOccupants[ADDRESS] = PEG; + _cells[ADDRESS]._occupant = PEG; SetCellSolid(ADDRESS); } @@ -174,7 +177,7 @@ public partial class Map : TileMapLayer _astar.Update(); for (int i = 0; i < _cells.Count; i++) { - _astar.SetPointWeightScale(_cells[i], Math.Abs(_cells[i].Y * 2)); + _astar.SetPointWeightScale(_cells.ElementAt(i).Value._address, Math.Abs(_cells.ElementAt(i).Value._address.Y * 2)); } EvaluateSolidCells(); } @@ -197,7 +200,7 @@ public partial class Map : TileMapLayer { for (int i = 0; i < _cells.Count; i++) { - SetCellSolid(_cells[i]); + SetCellSolid(_cells.ElementAt(i).Value._address); } } diff --git a/Map.tscn b/Map.tscn index 2ff8cb1..d92afa6 100644 --- a/Map.tscn +++ b/Map.tscn @@ -1,4 +1,4 @@ -[gd_scene format=3 uid="uid://mjinvqj25wha"] +[gd_scene format=3 uid="uid://dqknim3lldrmv"] [ext_resource type="Texture2D" uid="uid://cf554xlykq1o4" path="res://Art/tile_set.png" id="1_gfjgm"] [ext_resource type="Script" uid="uid://d2ri1sa0jwtqm" path="res://Map.cs" id="2_3vi7k"] diff --git a/MapCell.cs b/MapCell.cs new file mode 100644 index 0000000..b25d5da --- /dev/null +++ b/MapCell.cs @@ -0,0 +1,8 @@ +using Godot; +using System; + +public partial class MapCell : Node2D +{ + public Vector2I _address; + public MapCellOccupant _occupant; +} diff --git a/MapCell.cs.uid b/MapCell.cs.uid new file mode 100644 index 0000000..d6e08e5 --- /dev/null +++ b/MapCell.cs.uid @@ -0,0 +1 @@ +uid://b8fx1wwyj30bg diff --git a/MapCellOccupant.cs b/MapCellOccupant.cs new file mode 100644 index 0000000..5976e0c --- /dev/null +++ b/MapCellOccupant.cs @@ -0,0 +1,15 @@ +using Godot; +using System; + +public partial class MapCellOccupant : HoverableNode +{ + public Vector2I _address; + public Disposition _disposition = Disposition.NONE; + public enum Disposition + { + NONE = -99, + FRIENDLY = 1, + NEUTRAL = 0, + HOSTILE = -1 + } +} diff --git a/MapCellOccupant.cs.uid b/MapCellOccupant.cs.uid new file mode 100644 index 0000000..aca4934 --- /dev/null +++ b/MapCellOccupant.cs.uid @@ -0,0 +1 @@ +uid://b3dlktsgeteuw diff --git a/Peg.cs b/Peg.cs index 86ecbdb..7bfe497 100644 --- a/Peg.cs +++ b/Peg.cs @@ -3,14 +3,14 @@ using System; using System.Collections.Generic; using System.Linq; -public partial class Peg : HoverableNode +public partial class Peg : MapCellOccupant { [Signal] public delegate void DeathEventHandler(Peg THIS); - public int _id, _health = 2, _healthMax = 2, _stamina, _staminaRemaining, _movement = 0, _disposition, _visibility; - public Vector2I _address; + public int _id, _health = 2, _healthMax = 2, _stamina, _staminaRemaining, _movement = 0, _visibility; public List _path = new(); public PegController _pegController; + public Map _map; public List _actions; public int _distanceToGoal { @@ -24,6 +24,7 @@ public partial class Peg : HoverableNode { base._Ready(); _actions = [.. GetNode("Actions").GetChildren().Cast()]; + _map = _pegController._playArea._map; } public virtual PegAction SelectAction() { @@ -61,21 +62,24 @@ public partial class Peg : HoverableNode return _pegController._playArea._map.GetPath(_address, Goal(), false, PARTIAL); } - public virtual List GetEnemies() - { - return [.. _pegController._pegs.Where(p => p._disposition == -_disposition).OrderBy(p => (p._address - _address).Length())]; - } - - public virtual List GetVisibleCells() + public virtual Dictionary GetEnemies() { Map map = _pegController._playArea._map; - return [.. map._cells.Where(c => (c - _address).Length() <= _visibility)]; + return map._cells.Where(c => (int)c.Value._occupant._disposition == -(int)_disposition).ToDictionary(); } - public virtual List GetVisibleEnemies() + public virtual Dictionary GetVisibleCells() { - List visible = GetVisibleCells(); - return [.. GetEnemies().Where(e => visible.Contains(e._address))]; + Map map = _pegController._playArea._map; + return map._cells.Where(c => (c.Value._address - _address).Length() <= _visibility).ToDictionary(); + } + + public virtual Dictionary GetVisibleEnemies() + { + Dictionary visible = GetVisibleCells(); + Dictionary enemies = GetEnemies(); + + return enemies.Keys.Intersect(visible.Keys).ToDictionary(e => e, e => enemies[e]); } public Vector2 GetPositionFromAddress() diff --git a/Pegs/FriendlyPeg.cs b/Pegs/FriendlyPeg.cs index ca01707..dfcac30 100644 --- a/Pegs/FriendlyPeg.cs +++ b/Pegs/FriendlyPeg.cs @@ -7,7 +7,7 @@ public partial class FriendlyPeg : Peg public override void _Ready() { base._Ready(); - _disposition = 1; + _disposition = Disposition.FRIENDLY; } } diff --git a/Pegs/HostilePeg.cs b/Pegs/HostilePeg.cs index 562bc58..8805fae 100644 --- a/Pegs/HostilePeg.cs +++ b/Pegs/HostilePeg.cs @@ -10,7 +10,8 @@ public partial class HostilePeg : Peg public override void _Ready() { base._Ready(); - _disposition = -1; + + _disposition = Disposition.FRIENDLY; } } diff --git a/Pegs/NeutralPeg.cs b/Pegs/NeutralPeg.cs index 4f73acc..d39072b 100644 --- a/Pegs/NeutralPeg.cs +++ b/Pegs/NeutralPeg.cs @@ -7,7 +7,8 @@ public partial class NeutralPeg : Peg public override void _Ready() { base._Ready(); - _disposition = 0; + + _disposition = Disposition.NEUTRAL; } } diff --git a/Pegs/PegAction.cs b/Pegs/PegAction.cs index 44067f1..e197af0 100644 --- a/Pegs/PegAction.cs +++ b/Pegs/PegAction.cs @@ -25,10 +25,6 @@ public partial class PegAction : Node2D public virtual bool MeetsCriteria(Peg PEG) { - if (PEG.GetType().ToString() == "Spearman") - { - GD.Print(Name,": ",PEG._staminaRemaining >= _cost, "&&", _usesRemaining > 0, "&&", (PEG._address - Target(PEG)).Length() <= _range); - } return PEG._staminaRemaining >= _cost && _usesRemaining > 0 && (PEG._address - Target(PEG)).Length() <= _range; diff --git a/map_cell.tscn b/map_cell.tscn new file mode 100644 index 0000000..b0c6d30 --- /dev/null +++ b/map_cell.tscn @@ -0,0 +1,3 @@ +[gd_scene format=3 uid="uid://bh28pqqlppt3a"] + +[node name="MapCell" type="Node2D" unique_id=953237004] diff --git a/map_cell_occupant.tscn b/map_cell_occupant.tscn new file mode 100644 index 0000000..b0f0b83 --- /dev/null +++ b/map_cell_occupant.tscn @@ -0,0 +1,10 @@ +[gd_scene format=3 uid="uid://d4nyjj5740ww5"] + +[ext_resource type="Script" uid="uid://b3dlktsgeteuw" path="res://MapCellOccupant.cs" id="1_2wshj"] + +[node name="MapCellOccupant" type="Node2D" unique_id=946672764] +script = ExtResource("1_2wshj") + +[node name="HoverBounds" type="Area2D" parent="." unique_id=783465962] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="HoverBounds" unique_id=585114419]