finished reowrking grid maps, polished astar pathfinding, started work on aiming functions --todo: change launch speed to constant, change aim function to take into account the angle it would need to launch at to hit mouse position like in peggle, add raycasting
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public partial class Map : TileMapLayer
|
||||
{
|
||||
public int _minX, _maxX, _minY, _maxY, _mainSource = 0;
|
||||
public string _isSolidString = "is_solid";
|
||||
public Vector2 _cellSize, _sizeInPixels, _sizeInCells;
|
||||
public Vector2I _pathTakenAtlasCoordinates = new Vector2I(4, 0);
|
||||
public List<Vector2I> _leftmostColumn, _rightmostColumn, _topRow, _bottomRow;
|
||||
public AStarGrid2D _astar = new();
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
base._Ready();
|
||||
_cellSize = TileSet.TileSize;
|
||||
List<Vector2I> cells = GetUsedCells().ToList();
|
||||
_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)];
|
||||
|
||||
_sizeInCells = new Vector2(_topRow.Count, _leftmostColumn.Count);
|
||||
_sizeInPixels = _sizeInCells * _cellSize;
|
||||
|
||||
SetupAstar();
|
||||
}
|
||||
|
||||
public Vector2 GetCellPositionFromAddress(Vector2I CELL_ADDRESS)
|
||||
{
|
||||
return GlobalPosition + CELL_ADDRESS * _cellSize + _cellSize / 2;
|
||||
}
|
||||
|
||||
public bool IsCellSolid(Vector2I CELL_TO_CHECK)
|
||||
{
|
||||
return (bool)GetCellTileData(CELL_TO_CHECK).GetCustomData(_isSolidString);
|
||||
}
|
||||
|
||||
public void SetupAstar()
|
||||
{
|
||||
_astar.Region = new Rect2I(_minX, _minY, _topRow.Count, _leftmostColumn.Count);
|
||||
_astar.CellSize = _cellSize;
|
||||
_astar.DefaultComputeHeuristic = AStarGrid2D.Heuristic.Manhattan;
|
||||
_astar.DiagonalMode = AStarGrid2D.DiagonalModeEnum.Never;
|
||||
_astar.Update();
|
||||
List<Vector2I> cells = [.. GetUsedCells()];
|
||||
for (int i = 0; i < cells.Count; i++)
|
||||
{
|
||||
_astar.SetPointSolid(cells[i], IsCellSolid(cells[i]));
|
||||
}
|
||||
}
|
||||
|
||||
public List<Vector2I> GetPath(Vector2I FROM, Vector2I TO, bool INCLUDE_FROM = false, bool SHOW_PATH = false)
|
||||
{
|
||||
List<Vector2I> pathTaken = [.. _astar.GetIdPath(FROM, TO)];
|
||||
if (SHOW_PATH)
|
||||
{
|
||||
for (int i = 0; i < pathTaken.Count; i++)
|
||||
{
|
||||
Vector2I cell = pathTaken[i];
|
||||
SetCell(cell, _mainSource, _pathTakenAtlasCoordinates);
|
||||
}
|
||||
}
|
||||
if (!INCLUDE_FROM)
|
||||
{
|
||||
pathTaken.Remove(FROM);
|
||||
}
|
||||
return pathTaken;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user