continuing to set up map and pegs

This commit is contained in:
2026-07-02 22:50:21 -04:00
parent 0231c9d136
commit e6355167e3
13 changed files with 93 additions and 50 deletions
+32 -29
View File
@@ -6,19 +6,12 @@ using System.Linq;
public partial class Map : TileMapLayer
{
public PackedScene _mapCellScene = GD.Load<PackedScene>("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<Vector2I> _leftmostColumn = new(), _rightmostColumn = new(), _topRow = new(), _bottomRow = new();
public AStarGrid2D _astar = new();
public Dictionary<Vector2I, Peg> _addressOccupants = new();
public List<Vector2I> _cells
{
get
{
return [.. GetUsedCells()];
}
}
public Dictionary<Vector2I, MapCell> _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<Vector2I> 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<MapCell>();
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<Vector2I> rowCells = [.. _cells.Where(c => c.X == COLUMN_TO_CHECK)];
List<Vector2I> 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<Vector2I> rowCells = [.. _cells.Where(c => c.Y == ROW_TO_CHECK)];
List<Vector2I> 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);
}
}
+1 -1
View File
@@ -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"]
+8
View File
@@ -0,0 +1,8 @@
using Godot;
using System;
public partial class MapCell : Node2D
{
public Vector2I _address;
public MapCellOccupant _occupant;
}
+1
View File
@@ -0,0 +1 @@
uid://b8fx1wwyj30bg
+15
View File
@@ -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
}
}
+1
View File
@@ -0,0 +1 @@
uid://b3dlktsgeteuw
+17 -13
View File
@@ -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<Vector2I> _path = new();
public PegController _pegController;
public Map _map;
public List<PegAction> _actions;
public int _distanceToGoal
{
@@ -24,6 +24,7 @@ public partial class Peg : HoverableNode
{
base._Ready();
_actions = [.. GetNode<Node2D>("Actions").GetChildren().Cast<PegAction>()];
_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<Peg> GetEnemies()
{
return [.. _pegController._pegs.Where(p => p._disposition == -_disposition).OrderBy(p => (p._address - _address).Length())];
}
public virtual List<Vector2I> GetVisibleCells()
public virtual Dictionary<Vector2I, MapCell> 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<Peg> GetVisibleEnemies()
public virtual Dictionary<Vector2I, MapCell> GetVisibleCells()
{
List<Vector2I> 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<Vector2I, MapCell> GetVisibleEnemies()
{
Dictionary<Vector2I, MapCell> visible = GetVisibleCells();
Dictionary<Vector2I, MapCell> enemies = GetEnemies();
return enemies.Keys.Intersect(visible.Keys).ToDictionary(e => e, e => enemies[e]);
}
public Vector2 GetPositionFromAddress()
+1 -1
View File
@@ -7,7 +7,7 @@ public partial class FriendlyPeg : Peg
public override void _Ready()
{
base._Ready();
_disposition = 1;
_disposition = Disposition.FRIENDLY;
}
}
+2 -1
View File
@@ -10,7 +10,8 @@ public partial class HostilePeg : Peg
public override void _Ready()
{
base._Ready();
_disposition = -1;
_disposition = Disposition.FRIENDLY;
}
}
+2 -1
View File
@@ -7,7 +7,8 @@ public partial class NeutralPeg : Peg
public override void _Ready()
{
base._Ready();
_disposition = 0;
_disposition = Disposition.NEUTRAL;
}
}
-4
View File
@@ -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;
+3
View File
@@ -0,0 +1,3 @@
[gd_scene format=3 uid="uid://bh28pqqlppt3a"]
[node name="MapCell" type="Node2D" unique_id=953237004]
+10
View File
@@ -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]