basic enemy movement
This commit is contained in:
parent
46d2b7bc95
commit
fd8778e9f2
6 changed files with 377 additions and 15 deletions
104
Assets/Scripts/EnemyMovement.cs
Normal file
104
Assets/Scripts/EnemyMovement.cs
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class EnemyMovement : MonoBehaviour
|
||||
{
|
||||
#region Statication
|
||||
|
||||
public static EnemyMovement instance;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (instance != null && instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
instance = this;
|
||||
}
|
||||
|
||||
#endregion
|
||||
private Queue<TileObject> allMovableTiles = new();
|
||||
private Queue<TileObject> currentMovableTiles = new();
|
||||
private Dictionary<TileObject, TileObject> currentPaths = new();
|
||||
|
||||
public void PathfindToTarget(TileObject target, Enemy selectedEntity)
|
||||
{
|
||||
allMovableTiles.Clear();
|
||||
currentMovableTiles.Clear();
|
||||
currentPaths.Clear();
|
||||
GetTilesInRange(selectedEntity);
|
||||
TileObject requestedTile = GetPathToTarget(target, selectedEntity);
|
||||
selectedEntity.currentTile = requestedTile;
|
||||
selectedEntity.transform.position = requestedTile.transform.position;
|
||||
}
|
||||
private void GetTilesInRange(Enemy selectedEntity)
|
||||
{
|
||||
foreach (TileObject tileObject in selectedEntity.currentTile.neighbors)
|
||||
{
|
||||
if (!tileObject.blocked && !tileObject.hasUnit)
|
||||
{
|
||||
currentMovableTiles.Enqueue(tileObject);
|
||||
allMovableTiles.Enqueue(tileObject);
|
||||
currentPaths.Add(tileObject, selectedEntity.currentTile);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < selectedEntity.maxMovement - 1; i++)
|
||||
{
|
||||
foreach (TileObject tileObject in currentMovableTiles.ToArray())
|
||||
{
|
||||
foreach (TileObject neighboringTiles in tileObject.neighbors)
|
||||
{
|
||||
if (!neighboringTiles.blocked && !neighboringTiles.hasUnit)
|
||||
{
|
||||
currentMovableTiles.Enqueue(neighboringTiles);
|
||||
currentPaths.TryAdd(neighboringTiles, tileObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (TileObject tileObject in allMovableTiles.ToArray())
|
||||
{
|
||||
foreach (TileObject neighboringTiles in tileObject.neighbors)
|
||||
{
|
||||
if (!neighboringTiles.blocked && !neighboringTiles.hasUnit)
|
||||
{
|
||||
allMovableTiles.Enqueue(neighboringTiles);
|
||||
currentPaths.TryAdd(neighboringTiles, tileObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private TileObject GetPathToTarget(TileObject target, Enemy selectedEntity)
|
||||
{
|
||||
TileObject currentTile = target;
|
||||
List<TileObject> pathToTarget = new();
|
||||
TileObject pathTileInRange = null;
|
||||
while (currentTile != selectedEntity.currentTile)
|
||||
{
|
||||
pathToTarget.Add(currentTile);
|
||||
currentTile = currentPaths[currentTile];
|
||||
}
|
||||
|
||||
for (int i = 0; i < pathToTarget.Count; i++)
|
||||
{
|
||||
if (currentMovableTiles.Contains(pathToTarget[i]))
|
||||
{
|
||||
pathTileInRange = pathToTarget[i];
|
||||
return pathTileInRange;
|
||||
}
|
||||
}
|
||||
List<TileObject> pathToRange = new();
|
||||
/*if (pathTileInRange != null)
|
||||
{
|
||||
while (pathTileInRange != selectedEntity.currentTile)
|
||||
{
|
||||
pathToTarget.Add(pathTileInRange);
|
||||
pathTileInRange = currentPaths[pathTileInRange];
|
||||
}
|
||||
return pathToRange;
|
||||
}*/
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue