MoveAction.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Tilemaps;
  6. public class MoveAction : IAction {
  7. Vector3 startPosition;
  8. Vector3 targetPosition;
  9. private Creature toMove;
  10. private Creature targetCreature;
  11. private bool moveToPosition = false;
  12. List<Vector3> highlightedTiles;
  13. private int movedSquares = 0;
  14. private bool actionPerformed;
  15. private LineRenderer LR;
  16. public bool ActionPerformed { get => actionPerformed; set => actionPerformed = value; }
  17. bool hilightEnbled = true;
  18. public bool HilightEnabled { get => hilightEnbled; set => hilightEnbled = value; }
  19. public void SetStartPosition(Vector3 startPos) {
  20. startPosition = startPos;
  21. }
  22. public void SetTargetPosition(Vector3 targetPos) {
  23. targetPosition = targetPos;
  24. moveToPosition = true;
  25. }
  26. public void SetTargetCreature(Creature target) {
  27. targetCreature = target;
  28. moveToPosition = false;
  29. }
  30. public void SetCreatureToMove(Creature toMove) {
  31. this.toMove = toMove;
  32. }
  33. /*
  34. * If someone is within 5 feet, should you stop and attack?
  35. If not, attack of opportunity?
  36. * Try not to move within 5 feet of non target, and still closing in to target (prio)
  37. * När toMove är innom 5 foot av target, avbryt move och attakera direkt?
  38. eller flytta efter till end of movement, och då attackera, även om target fortsätter att flytta sig.
  39. */
  40. public IEnumerator PerformAction() {
  41. Tilemap tileMap = GameManagerScript.GetInstance().TileMap;
  42. if (moveToPosition) {
  43. Vector3 distance = tileMap.WorldToCell(targetPosition - startPosition);
  44. int safety = 0;
  45. while (!distance.Equals(Vector3.zero)) {
  46. safety++;
  47. if (distance.x != 0 || distance.y != 0) {
  48. Vector3 movement = toMove.transform.position;
  49. if (distance.x > 0) {
  50. movement.x += 1;
  51. distance.x -= 1;
  52. } else if (distance.x < 0) {
  53. movement.x -= 1;
  54. distance.x += 1;
  55. }
  56. if (distance.y > 0) {
  57. movement.y += 1;
  58. distance.y -= 1;
  59. } else if (distance.y < 0) {
  60. movement.y -= 1;
  61. distance.y += 1;
  62. }
  63. Creature tileBlockedBy = TileOccupiedBy(movement);
  64. if (tileBlockedBy != null) {
  65. Debug.Log("Tile blocked by: " + tileBlockedBy.name);
  66. IAction actionWhenBlocked = toMove.ActionWhenBlocked(tileBlockedBy);
  67. if (actionWhenBlocked != null) {
  68. movedSquares += 2; // consume two move, and set and use second action to decision
  69. toMove.SetSecondAction(actionWhenBlocked);
  70. yield return actionWhenBlocked.PerformAction();
  71. }
  72. } else {
  73. toMove.transform.position = movement;
  74. movedSquares++;
  75. }
  76. if (toMove.movementRate <= movedSquares * 5) { // this one have moved all that they can for one action
  77. Debug.Log(toMove.name + " moved there entire " + toMove.movementRate + " distance, stopping");
  78. ActionPerformed = true;
  79. break;
  80. }
  81. yield return new WaitForSeconds(1);
  82. }
  83. if (safety > 120) {
  84. throw new System.Exception("LOOPED 120 times");
  85. }
  86. }
  87. } else { // move to creature
  88. Vector3 distance = tileMap.WorldToCell(targetCreature.transform.position - startPosition);
  89. int safety = 0;
  90. while (!distance.Equals(Vector3.zero)) {
  91. safety++;
  92. if (distance.x != 0 || distance.y != 0) { // Move diagonally
  93. Vector3 movement = toMove.transform.position;
  94. if (distance.x > 0) {
  95. movement.x += 1;
  96. distance.x -= 1;
  97. } else if (distance.x < 0) {
  98. movement.x -= 1;
  99. distance.x += 1;
  100. }
  101. if (distance.y > 0) {
  102. movement.y += 1;
  103. distance.y -= 1;
  104. } else if (distance.y < 0) {
  105. movement.y -= 1;
  106. distance.y += 1;
  107. }
  108. Creature tileBlockedBy = TileOccupiedBy(movement);
  109. if (tileBlockedBy != null) {
  110. Debug.Log(toMove.name + "s Tile blocked by: " + tileBlockedBy.name);
  111. IAction action = toMove.ActionWhenBlocked(tileBlockedBy);
  112. toMove.SetSecondAction(action);
  113. movedSquares += 2; // consume two move, and set and use second action to decision
  114. yield return toMove.GetSecondAction().PerformAction();
  115. } else {
  116. toMove.transform.position = movement;
  117. movedSquares++;
  118. }
  119. if (toMove.movementRate <= movedSquares * 5) { // this one have moved all that they can for one action
  120. Debug.Log(toMove.name + " moved there entire " + toMove.movementRate + " distance, stopping");
  121. break;
  122. }
  123. yield return new WaitForSeconds(1);
  124. distance = tileMap.WorldToCell(targetCreature.transform.position - toMove.transform.position);
  125. Debug.Log(toMove.name + " move to " + targetPosition + "(" + tileMap.WorldToCell(targetPosition) + ") from " + startPosition + "(" + tileMap.WorldToCell(startPosition) + " by moving " + distance);
  126. }
  127. if (safety > 1000) {
  128. throw new System.Exception("LOOPED 1000 times");
  129. }
  130. }
  131. }
  132. actionPerformed = true;
  133. yield return null;
  134. }
  135. private Creature TileOccupiedBy(Vector3 nextSprite) {
  136. List<Creature> creatures = GameManagerScript.GetInstance().GetCombatants();
  137. Tilemap tileMap = GameManagerScript.GetInstance().TileMap;
  138. Vector3Int cellToCheck = tileMap.WorldToCell(nextSprite);
  139. Creature returnValue = null;
  140. foreach (Creature creature in creatures) {
  141. if (cellToCheck.Equals(tileMap.WorldToCell(creature.transform.position))) {
  142. returnValue = creature;
  143. }
  144. }
  145. return returnValue;
  146. }
  147. }