continuing to set up map and pegs
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user