basic enemy movement
This commit is contained in:
parent
46d2b7bc95
commit
fd8778e9f2
6 changed files with 377 additions and 15 deletions
|
|
@ -11,11 +11,14 @@ public class Enemy : Entity
|
|||
turnSpeed = Random.Range(0, 100);
|
||||
}
|
||||
|
||||
[ContextMenu("ForceTurn")]
|
||||
public void StartTurn()
|
||||
{
|
||||
//attack the nearest player?
|
||||
TurnHandler.instance.UpdateTurns();
|
||||
closestPlayer = FindClosestPlayer();
|
||||
Debug.Log(closestPlayer);
|
||||
EnemyMovement.instance.PathfindToTarget(closestPlayer.currentTile.neighbors[Random.Range(0, closestPlayer.currentTile.neighbors.Count)], this);
|
||||
}
|
||||
|
||||
private PlayerEntity FindClosestPlayer()
|
||||
|
|
|
|||
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;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/EnemyMovement.cs.meta
Normal file
2
Assets/Scripts/EnemyMovement.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 5480895c85041d2e492c0ccbfa17254a
|
||||
|
|
@ -35,7 +35,10 @@ public class PlayerEntityMovement : MonoBehaviour
|
|||
|
||||
public LayerMask playerLayer;
|
||||
|
||||
private List<TileObject> currentMovableTiles = new();
|
||||
private Queue<TileObject> currentMovableTiles = new();
|
||||
private Dictionary<TileObject, TileObject> paths = new();
|
||||
private List<TileObject> currentPath = new();
|
||||
private TileObject requestedTile;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
|
|
@ -52,6 +55,29 @@ public class PlayerEntityMovement : MonoBehaviour
|
|||
}
|
||||
if (isMoving)
|
||||
{
|
||||
TileObject newTile = GridManager.instance.GetTile(mouseGridPos);
|
||||
if (requestedTile != newTile)
|
||||
{
|
||||
requestedTile = newTile;
|
||||
foreach (TileObject tile in currentPath)
|
||||
{
|
||||
tile.sprite.color = Color.green;
|
||||
}
|
||||
currentPath.Clear();
|
||||
if (currentMovableTiles.Contains(requestedTile))
|
||||
{
|
||||
TileObject currentTile = requestedTile;
|
||||
while (currentTile != selectedEntity.currentTile)
|
||||
{
|
||||
currentPath.Add(currentTile);
|
||||
currentTile = paths[currentTile];
|
||||
}
|
||||
foreach (TileObject tile in currentPath)
|
||||
{
|
||||
tile.sprite.color = Color.blue;
|
||||
}
|
||||
}
|
||||
}
|
||||
templateObject.transform.position = mouseGridPos;
|
||||
if (Input.GetMouseButtonDown(0) && debounceTime <= 0 && SelectLocation())
|
||||
{
|
||||
|
|
@ -67,11 +93,13 @@ public class PlayerEntityMovement : MonoBehaviour
|
|||
debounceTime = debounceDuration; //alotta for loops here
|
||||
UncolorGrid();
|
||||
currentMovableTiles.Clear();
|
||||
paths.Clear();
|
||||
foreach (TileObject tileObject in selectedEntity.currentTile.neighbors)
|
||||
{
|
||||
if (!tileObject.blocked && !tileObject.hasUnit)
|
||||
{
|
||||
currentMovableTiles.Add(tileObject);
|
||||
currentMovableTiles.Enqueue(tileObject);
|
||||
paths.Add(tileObject, selectedEntity.currentTile);
|
||||
tileObject.sprite.color = Color.green;
|
||||
}
|
||||
}
|
||||
|
|
@ -83,8 +111,9 @@ public class PlayerEntityMovement : MonoBehaviour
|
|||
{
|
||||
if (!neighboringTiles.blocked && !neighboringTiles.hasUnit)
|
||||
{
|
||||
currentMovableTiles.Add(neighboringTiles);
|
||||
currentMovableTiles.Enqueue(neighboringTiles);
|
||||
neighboringTiles.sprite.color = Color.green;
|
||||
paths.TryAdd(neighboringTiles, tileObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -102,14 +131,13 @@ public class PlayerEntityMovement : MonoBehaviour
|
|||
|
||||
private bool SelectLocation()
|
||||
{
|
||||
TileObject selectedTile = GridManager.instance.GetTile(mouseGridPos);
|
||||
if (!selectedTile || selectedTile.blocked || selectedTile.hasUnit || !currentMovableTiles.Contains(selectedTile))
|
||||
if (!requestedTile || requestedTile.blocked || requestedTile.hasUnit || !currentMovableTiles.Contains(requestedTile))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
selectedEntity.currentTile.hasUnit = false;
|
||||
selectedTile.hasUnit = true;
|
||||
selectedEntity.currentTile = selectedTile;
|
||||
requestedTile.hasUnit = true;
|
||||
selectedEntity.currentTile = requestedTile;
|
||||
selectedEntity.transform.position = mouseGridPos;
|
||||
templateObject.SetActive(false);
|
||||
UncolorGrid();
|
||||
|
|
|
|||
|
|
@ -52,12 +52,7 @@ public class TurnHandler : MonoBehaviour
|
|||
}
|
||||
if (allDone)
|
||||
{
|
||||
foreach (PlayerEntity player in playerEntities)
|
||||
{
|
||||
player.hasMoved = false;
|
||||
player.hasAttacked = false;
|
||||
}
|
||||
//currentGameState = GameState.EnemyTurn;
|
||||
currentGameState = GameState.EnemyTurn;
|
||||
}
|
||||
}
|
||||
else if (currentGameState == GameState.EnemyTurn)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue