MoveAction.cs 6.8 KB

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