Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.37 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Security.Cryptography.X509Certificates;
  6. using UnityEngine;
  7. using Debug = UnityEngine.Debug;
  8.  
  9. public class ControllerAI : MonoBehaviour
  10. {
  11. private Attributes attributes;
  12. private GameState gameState;
  13. private GridManager gridManager;
  14. private GameObject objective;
  15. private Coroutine currentMovement;
  16. private int moveCounter;
  17.  
  18. public GameObject thisObj;
  19. public GameObject objCell;
  20. public List<GameObject> thingsinRange;
  21. public List<GameObject> enemiesFound;
  22. public GameObject theClosestObj;
  23.  
  24. void Start()
  25. {
  26. attributes = gameObject.GetComponent<Attributes>();
  27. gameState = FindObjectOfType<GameState>();
  28. gridManager = FindObjectOfType<GridManager>();
  29. objCell = GetCellOfObj(gameObject);
  30. objCell.GetComponent<Cell>().AddToResidents(gameObject);
  31. }
  32.  
  33. void Update()
  34. {
  35. //If the game turn counter is greater than the object's turn counter take turn
  36. if (gameState.turnCounter > moveCounter)
  37. {
  38. TakeTurn();
  39. moveCounter += attributes.moveRate;
  40. }
  41. }
  42.  
  43. void StartMovement(IEnumerator movement)
  44. {
  45. if (currentMovement != null)
  46. StopCoroutine(currentMovement);
  47. currentMovement = StartCoroutine(movement);
  48. }
  49.  
  50. public void TakeTurn()
  51. {
  52. //If the unit does not have an objective, get one.
  53. if (theClosestObj == null)
  54. {
  55. //Find all things in sight range
  56. //Create list of enemy types and a list of real enemies.
  57. //Get Cells of enemies
  58. //Find Closest Enemy -- this may or may not need to be expanded to incorporate items, gold, etc.
  59. //Find Closest enemy does not factor in path, simply grid cells. Will need to do path finding some time.
  60. //Objective is, in this case, the cell with which the enemy, item, gold, is in.
  61.  
  62. thisObj = gameObject;
  63. thingsinRange = FindThingsInRange(thisObj);
  64. enemiesFound = DetermineEnemies(thingsinRange);
  65. theClosestObj = ClosestObj(enemiesFound, thisObj);
  66. }
  67.  
  68. if (GetDistance(theClosestObj) > attributes.attackRange)
  69. {
  70. //replace with Get Cell of Position?
  71. Vector2 moveDirection = FindDirection(GetCellOfObj(gameObject), GetCellOfObj(theClosestObj));
  72.  
  73. int x = (int) moveDirection.x;
  74. int y = (int) moveDirection.y;
  75.  
  76. GameObject endCell = gridManager.gridCells[x, y];
  77. if (!endCell.GetComponent<Cell>().isBlocker && !endCell.GetComponent<Cell>().hasCharacter)
  78. {
  79. StartMovement(Move.MoveObj(gameObject, objCell, endCell));
  80. }
  81.  
  82. }
  83. else
  84. {
  85. StartMovement(Attack.AttackObj(gameObject, theClosestObj));
  86. }
  87. }
  88.  
  89. public GameObject GetCellOfObj(GameObject obj)
  90. {
  91. int x = (int) obj.transform.position.x;
  92. int y = (int) obj.transform.position.y;
  93.  
  94. GameObject onCell = gridManager.gridCells[x, y];
  95.  
  96. return onCell;
  97. }
  98.  
  99. public List<GameObject> FindThingsInRange(GameObject obj)
  100. {
  101. //Define my aggroRange
  102. int sightRange = attributes.sightRange;
  103.  
  104. List<GameObject> residentsInRange = new List<GameObject>();
  105.  
  106. foreach (GameObject cell in GetNeighbors(obj, sightRange))
  107. {
  108. Cell cellConfig = cell.GetComponent<Cell>();
  109.  
  110. foreach (GameObject resident in cellConfig.residents)
  111. {
  112. residentsInRange.Add(resident);
  113. }
  114. }
  115. return residentsInRange;
  116. }
  117.  
  118. public List<GameObject> DetermineEnemies(List<GameObject> things)
  119. {
  120. //This dictionary should probably go into GameState
  121. //Friendly characters only attack evil + chaotic.
  122. //Evil characters only attack friendly + chaotic.
  123. //Chaotic characters attack everyone.
  124. Dictionary<string, List<string>> enemiesOf = new Dictionary<string, List<string>>();
  125.  
  126. //This is such an error prone method of defining faction -- would love to have a dropdown menu for the attributes SO.
  127. List<string> enemyOfFriendly = new List<string>();
  128. enemyOfFriendly.Add("evil");
  129. enemyOfFriendly.Add("chaotic");
  130.  
  131. enemiesOf.Add("friendly", enemyOfFriendly);
  132.  
  133. List<string> enemyOfEvil = new List<string>();
  134. enemyOfEvil.Add("friendly");
  135. enemyOfEvil.Add("chaotic");
  136.  
  137. enemiesOf.Add("evil", enemyOfEvil);
  138.  
  139. List<string> enemyOfChaotic = new List<string>();
  140. enemyOfChaotic.Add("friendly");
  141. enemyOfChaotic.Add("evil");
  142. enemyOfChaotic.Add("chaotic");
  143.  
  144. enemiesOf.Add("chaotic", enemyOfChaotic);
  145.  
  146. //Get the list of enemy types based of this objects faction, from the dictionary.
  147. //Compare the list of things, do any of the things match any of the types from the dictionary?
  148. //Add to list of enemies.
  149. List<GameObject> enemies = new List<GameObject>();
  150.  
  151. for (int i = 0; i < things.Count; i++)
  152. {
  153. Attributes thingsAttributes = things[i].GetComponent<Attributes>();
  154. foreach (string factions in enemiesOf[attributes.faction])
  155.  
  156. if (factions == thingsAttributes.faction)
  157. {
  158. enemies.Add(things[i]);
  159. }
  160. }
  161. return enemies;
  162. }
  163.  
  164. //Find the closest enemy from the enemies list.
  165. //Take each enemy in the list, get the cell, then get the distance.
  166. public GameObject ClosestObj(List<GameObject> objList, GameObject focusObj)
  167. {
  168. List<float> distances = new List<float>();
  169.  
  170. GameObject closestObj = null;
  171.  
  172. foreach (GameObject obj in objList)
  173. {
  174. GameObject curObj = focusObj;
  175. int distance = GetDistance(obj);
  176. distances.Add(distance);
  177.  
  178. if (distance <= distances.Min())
  179. {
  180. closestObj = obj;
  181. }
  182. }
  183. return closestObj;
  184. }
  185.  
  186. public Vector2 FindDirection(GameObject startCell, GameObject endCell)
  187. {
  188. //Getting a next best direction will require pathfinding (should I go up around the wall or down towards the door?)
  189.  
  190. int startX = (int) startCell.transform.position.x;
  191. int startY = (int) startCell.transform.position.y;
  192.  
  193. int endX = (int) endCell.transform.position.x;
  194. int endY = (int) endCell.transform.position.y;
  195.  
  196. //Which axis do we want to move on?
  197. int distX = Mathf.Abs(startX - endX);
  198. int distY = Mathf.Abs(startY - endY);
  199.  
  200. Vector2 direction = new Vector2(0, 0);
  201.  
  202. if (distX >= distY)
  203. {
  204. //Move Horizontal
  205. //Which way, horizontally?
  206. if (startX >= endX)
  207. {
  208. //Move Left
  209. direction = new Vector2(startX - 1, startY);
  210. }
  211. else
  212. {
  213. //Move Right
  214. direction = new Vector2(startX + 1, startY);
  215. }
  216. }
  217. else
  218. {
  219. //Move Vertical
  220. if (startY >= endY)
  221. {
  222. //Move Down
  223. direction = new Vector2(startX, startY - 1);
  224. }
  225. else
  226. {
  227. //Move Up
  228. direction = new Vector2(startX, startY + 1);
  229. }
  230. }
  231. return direction;
  232. }
  233.  
  234.  
  235.  
  236. public int GetDistance(GameObject objA)
  237. {
  238. int objAX = (int) objA.transform.position.x;
  239. int objAY = (int) objA.transform.position.y;
  240.  
  241. int objBX = (int) gameObject.transform.position.x;
  242. int objBY = (int) gameObject.transform.position.y;
  243.  
  244. int distance = Mathf.Abs(objAX - objBX) + Mathf.Abs(objAY - objBY);
  245. return distance;
  246. }
  247.  
  248. public GameObject GetResidentOnCell(GameObject cellObj)
  249. {
  250. Cell cell = cellObj.GetComponent<Cell>();
  251.  
  252. GameObject resident = null;
  253.  
  254. foreach (GameObject cellResident in cell.residents)
  255. {
  256. resident = cellResident;
  257. }
  258. return resident;
  259. }
  260.  
  261.  
  262. public List<GameObject> GetNeighbors(GameObject obj, int range)
  263. {
  264. int row = (int) obj.transform.position.x;
  265. int col = (int) obj.transform.position.y;
  266.  
  267. List<GameObject> neighbors = new List<GameObject>();
  268. for (int x = row - range; x < row + range; x++)
  269. {
  270. if (x < 0 || x > gridManager.gridCells.GetLength(0))
  271. {
  272. continue;
  273. }
  274. for (int y = col - range; y < col + range; y++)
  275. {
  276. if (y < 0 || y > gridManager.gridCells.GetLength(1))
  277. {
  278. continue;
  279. }
  280. //Manhattan distance to check range from obj
  281. if (Mathf.Abs(row - x) + Mathf.Abs(col - y) < range)
  282. {
  283. neighbors.Add(gridManager.gridCells[x, y]);
  284. }
  285. }
  286. }
  287.  
  288. return neighbors;
  289. }
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement