Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Security.Cryptography.X509Certificates;
- using UnityEngine;
- using Debug = UnityEngine.Debug;
- public class ControllerAI : MonoBehaviour
- {
- private Attributes attributes;
- private GameState gameState;
- private GridManager gridManager;
- private GameObject objective;
- private Coroutine currentMovement;
- private int moveCounter;
- public GameObject thisObj;
- public GameObject objCell;
- public List<GameObject> thingsinRange;
- public List<GameObject> enemiesFound;
- public GameObject theClosestObj;
- void Start()
- {
- attributes = gameObject.GetComponent<Attributes>();
- gameState = FindObjectOfType<GameState>();
- gridManager = FindObjectOfType<GridManager>();
- objCell = GetCellOfObj(gameObject);
- objCell.GetComponent<Cell>().AddToResidents(gameObject);
- }
- void Update()
- {
- //If the game turn counter is greater than the object's turn counter take turn
- if (gameState.turnCounter > moveCounter)
- {
- TakeTurn();
- moveCounter += attributes.moveRate;
- }
- }
- void StartMovement(IEnumerator movement)
- {
- if (currentMovement != null)
- StopCoroutine(currentMovement);
- currentMovement = StartCoroutine(movement);
- }
- public void TakeTurn()
- {
- //If the unit does not have an objective, get one.
- if (theClosestObj == null)
- {
- //Find all things in sight range
- //Create list of enemy types and a list of real enemies.
- //Get Cells of enemies
- //Find Closest Enemy -- this may or may not need to be expanded to incorporate items, gold, etc.
- //Find Closest enemy does not factor in path, simply grid cells. Will need to do path finding some time.
- //Objective is, in this case, the cell with which the enemy, item, gold, is in.
- thisObj = gameObject;
- thingsinRange = FindThingsInRange(thisObj);
- enemiesFound = DetermineEnemies(thingsinRange);
- theClosestObj = ClosestObj(enemiesFound, thisObj);
- }
- if (GetDistance(theClosestObj) > attributes.attackRange)
- {
- //replace with Get Cell of Position?
- Vector2 moveDirection = FindDirection(GetCellOfObj(gameObject), GetCellOfObj(theClosestObj));
- int x = (int) moveDirection.x;
- int y = (int) moveDirection.y;
- GameObject endCell = gridManager.gridCells[x, y];
- if (!endCell.GetComponent<Cell>().isBlocker && !endCell.GetComponent<Cell>().hasCharacter)
- {
- StartMovement(Move.MoveObj(gameObject, objCell, endCell));
- }
- }
- else
- {
- StartMovement(Attack.AttackObj(gameObject, theClosestObj));
- }
- }
- public GameObject GetCellOfObj(GameObject obj)
- {
- int x = (int) obj.transform.position.x;
- int y = (int) obj.transform.position.y;
- GameObject onCell = gridManager.gridCells[x, y];
- return onCell;
- }
- public List<GameObject> FindThingsInRange(GameObject obj)
- {
- //Define my aggroRange
- int sightRange = attributes.sightRange;
- List<GameObject> residentsInRange = new List<GameObject>();
- foreach (GameObject cell in GetNeighbors(obj, sightRange))
- {
- Cell cellConfig = cell.GetComponent<Cell>();
- foreach (GameObject resident in cellConfig.residents)
- {
- residentsInRange.Add(resident);
- }
- }
- return residentsInRange;
- }
- public List<GameObject> DetermineEnemies(List<GameObject> things)
- {
- //This dictionary should probably go into GameState
- //Friendly characters only attack evil + chaotic.
- //Evil characters only attack friendly + chaotic.
- //Chaotic characters attack everyone.
- Dictionary<string, List<string>> enemiesOf = new Dictionary<string, List<string>>();
- //This is such an error prone method of defining faction -- would love to have a dropdown menu for the attributes SO.
- List<string> enemyOfFriendly = new List<string>();
- enemyOfFriendly.Add("evil");
- enemyOfFriendly.Add("chaotic");
- enemiesOf.Add("friendly", enemyOfFriendly);
- List<string> enemyOfEvil = new List<string>();
- enemyOfEvil.Add("friendly");
- enemyOfEvil.Add("chaotic");
- enemiesOf.Add("evil", enemyOfEvil);
- List<string> enemyOfChaotic = new List<string>();
- enemyOfChaotic.Add("friendly");
- enemyOfChaotic.Add("evil");
- enemyOfChaotic.Add("chaotic");
- enemiesOf.Add("chaotic", enemyOfChaotic);
- //Get the list of enemy types based of this objects faction, from the dictionary.
- //Compare the list of things, do any of the things match any of the types from the dictionary?
- //Add to list of enemies.
- List<GameObject> enemies = new List<GameObject>();
- for (int i = 0; i < things.Count; i++)
- {
- Attributes thingsAttributes = things[i].GetComponent<Attributes>();
- foreach (string factions in enemiesOf[attributes.faction])
- if (factions == thingsAttributes.faction)
- {
- enemies.Add(things[i]);
- }
- }
- return enemies;
- }
- //Find the closest enemy from the enemies list.
- //Take each enemy in the list, get the cell, then get the distance.
- public GameObject ClosestObj(List<GameObject> objList, GameObject focusObj)
- {
- List<float> distances = new List<float>();
- GameObject closestObj = null;
- foreach (GameObject obj in objList)
- {
- GameObject curObj = focusObj;
- int distance = GetDistance(obj);
- distances.Add(distance);
- if (distance <= distances.Min())
- {
- closestObj = obj;
- }
- }
- return closestObj;
- }
- public Vector2 FindDirection(GameObject startCell, GameObject endCell)
- {
- //Getting a next best direction will require pathfinding (should I go up around the wall or down towards the door?)
- int startX = (int) startCell.transform.position.x;
- int startY = (int) startCell.transform.position.y;
- int endX = (int) endCell.transform.position.x;
- int endY = (int) endCell.transform.position.y;
- //Which axis do we want to move on?
- int distX = Mathf.Abs(startX - endX);
- int distY = Mathf.Abs(startY - endY);
- Vector2 direction = new Vector2(0, 0);
- if (distX >= distY)
- {
- //Move Horizontal
- //Which way, horizontally?
- if (startX >= endX)
- {
- //Move Left
- direction = new Vector2(startX - 1, startY);
- }
- else
- {
- //Move Right
- direction = new Vector2(startX + 1, startY);
- }
- }
- else
- {
- //Move Vertical
- if (startY >= endY)
- {
- //Move Down
- direction = new Vector2(startX, startY - 1);
- }
- else
- {
- //Move Up
- direction = new Vector2(startX, startY + 1);
- }
- }
- return direction;
- }
- public int GetDistance(GameObject objA)
- {
- int objAX = (int) objA.transform.position.x;
- int objAY = (int) objA.transform.position.y;
- int objBX = (int) gameObject.transform.position.x;
- int objBY = (int) gameObject.transform.position.y;
- int distance = Mathf.Abs(objAX - objBX) + Mathf.Abs(objAY - objBY);
- return distance;
- }
- public GameObject GetResidentOnCell(GameObject cellObj)
- {
- Cell cell = cellObj.GetComponent<Cell>();
- GameObject resident = null;
- foreach (GameObject cellResident in cell.residents)
- {
- resident = cellResident;
- }
- return resident;
- }
- public List<GameObject> GetNeighbors(GameObject obj, int range)
- {
- int row = (int) obj.transform.position.x;
- int col = (int) obj.transform.position.y;
- List<GameObject> neighbors = new List<GameObject>();
- for (int x = row - range; x < row + range; x++)
- {
- if (x < 0 || x > gridManager.gridCells.GetLength(0))
- {
- continue;
- }
- for (int y = col - range; y < col + range; y++)
- {
- if (y < 0 || y > gridManager.gridCells.GetLength(1))
- {
- continue;
- }
- //Manhattan distance to check range from obj
- if (Mathf.Abs(row - x) + Mathf.Abs(col - y) < range)
- {
- neighbors.Add(gridManager.gridCells[x, y]);
- }
- }
- }
- return neighbors;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement