Untitled
unknown
plain_text
2 years ago
4.8 kB
7
Indexable
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; public class EnemyAI : MonoBehaviour { Movement movement; PathfindingManager pathfinding; int currentIndex; [SerializeField] float moveTime = 0.8f; [SerializeField] int enemyDamage = 40; GameObject parentObject; Transform parentTransform; GridManager gridManager; bool isMoving; List<Cell> currentAvialablePath = new List<Cell>(); List<Cell> currentNeighbours = new List<Cell>(); List<Cell> cellsToMove = new List<Cell>(); private void Awake() { //StartCoroutine(MoveToNextCell()); gridManager = GridManager.Instance; movement = new Movement(); pathfinding = new PathfindingManager(); } public int EnemyDamage() { return enemyDamage; } void Start() { StartCoroutine(MoveToNextCell()); } private IEnumerator MoveToNextCell() { //if (!movement.isMoving) //{ // parentTransform = transform.parent; ////if (parentTransform != null) //{ // parentObject = parentTransform.gameObject; //} //Cell parentCell = parentObject.GetComponent<Cell>(); //currentIndex = gridManager.cells.IndexOf(parentCell); //currentAvialablePath = pathfinding.FindPath(parentCell, gridManager.cells[Player.playerCellIndex], gridManager.cells); // //foreach (var item in currentAvialablePath) // //{ // // item.setColor(Color.cyan); // //} // yield return new WaitForSeconds(0.2f); // currentNeighbours = pathfinding.GetNeighbors(parentCell); // if (currentNeighbours != null && currentAvialablePath != null) // { // cellsToMove = currentNeighbours.Intersect(currentAvialablePath).ToList(); // } // //List<Cell> cellsToMove = currentNeighbours?.Intersect(currentAvialablePath)?.ToList(); // if (cellsToMove != null && cellsToMove.Count > 0) // { // foreach (var item in cellsToMove) // { // currentAvialablePath.Clear(); // currentNeighbours.Clear(); // StartCoroutine(movement.MoveToNExtCell(moveTime, this.gameObject, item)); // } // cellsToMove.Clear(); // } //} //yield return new WaitForSeconds(0.2f); //StartCoroutine(MoveToNextCell()); while (true) { //if (!movement.isMoving) { parentTransform = transform.parent; if (parentTransform != null) { parentObject = parentTransform.gameObject; } Cell parentCell = parentObject.GetComponent<Cell>(); currentIndex = gridManager.cells.IndexOf(parentCell); Debug.Log("current Index is" + currentIndex); currentAvialablePath = pathfinding.FindPath(parentCell, gridManager.cells[Player.playerCellIndex], gridManager.cells); foreach (var item in currentAvialablePath) { item.setColor(Color.cyan); } currentNeighbours = pathfinding.GetNeighbors(parentCell); if (currentNeighbours != null && currentAvialablePath != null) { cellsToMove = currentNeighbours.Intersect(currentAvialablePath).ToList(); } if (cellsToMove != null && cellsToMove.Count > 0) { foreach (Cell item in cellsToMove) { item.setColor(Color.yellow); } if (!movement.isMoving) { StartCoroutine(movement.MoveToNExtCell(moveTime, this.gameObject, cellsToMove[0])); //foreach (var item in cellsToMove) //{ // StartCoroutine(movement.MoveToNExtCell(moveTime, this.gameObject, item)); // yield return new WaitForSeconds(moveTime); //} currentAvialablePath.Clear(); currentNeighbours.Clear(); cellsToMove.Clear(); parentCell = null; } } } yield return null; } //void Update() //{ // if (!movement.isMoving && !isMoving) // { // StartCoroutine(MoveToNextCell()); // } //} } }
Editor is loading...