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 shouldMove = false;
  12. private bool moveToPosition = false;
  13. private int movedSquares = 0;
  14. public void SetStartPosition(Vector3 startPos) {
  15. startPosition = startPos;
  16. }
  17. public void SetTargetPosition(Vector3 targetPos) {
  18. targetPosition = targetPos;
  19. moveToPosition = true;
  20. }
  21. public void SetTargetCreature(Creature target) {
  22. targetCreature = target;
  23. moveToPosition = false;
  24. }
  25. public void SetCreatureToMove(Creature toMove) {
  26. this.toMove = toMove;
  27. }
  28. /*
  29. * If someone is within 5 feet, should you stop and attack?
  30. If not, attack of opportunity?
  31. * Try not to move within 5 feet of non target, and still closing in to target (prio)
  32. * När toMove är innom 5 foot av target, avbryt move och attakera direkt?
  33. eller flytta efter till end of movement, och då attackera, även om target fortsätter att flytta sig.
  34. */
  35. public IEnumerator PerformAction() {
  36. Tilemap tileMap = GameManagerScript.GetInstance().TileMap;
  37. if (moveToPosition) {
  38. Vector3 distance = tileMap.WorldToCell(targetPosition - startPosition);
  39. int safety = 0;
  40. while (!distance.Equals(Vector3.zero)) {
  41. safety++;
  42. if (distance.x != 0 || distance.y != 0) {
  43. Vector3 movement = toMove.transform.position;
  44. if (distance.x > 0) {
  45. movement.x += 1;
  46. distance.x -= 1;
  47. } else if (distance.x < 0) {
  48. movement.x -= 1;
  49. distance.x += 1;
  50. }
  51. if (distance.y > 0) {
  52. movement.y += 1;
  53. distance.y -= 1;
  54. } else if (distance.y < 0) {
  55. movement.y -= 1;
  56. distance.y += 1;
  57. }
  58. checkIfOccupoied(movement);
  59. toMove.transform.position = movement;
  60. movedSquares++;
  61. if (toMove.movementRate <= movedSquares * 5) { // this one have moved all that they can for one action
  62. Debug.Log(toMove.name + " moved there entire " + toMove.movementRate + " distance, stopping");
  63. break;
  64. }
  65. yield return new WaitForSeconds(1);
  66. }
  67. if (safety > 120) {
  68. throw new System.Exception("LOOPED 120 times");
  69. }
  70. }
  71. } else { // move to creature
  72. Vector3 distance = tileMap.WorldToCell(targetCreature.transform.position - startPosition);
  73. int safety = 0;
  74. while (!distance.Equals(Vector3.zero)) {
  75. safety++;
  76. if (distance.x != 0 || distance.y != 0) { // Move diagonally
  77. Vector3 movement = toMove.transform.position;
  78. if (distance.x > 0) {
  79. movement.x += 1;
  80. distance.x -= 1;
  81. } else if (distance.x < 0) {
  82. movement.x -= 1;
  83. distance.x += 1;
  84. }
  85. if (distance.y > 0) {
  86. movement.y += 1;
  87. distance.y -= 1;
  88. } else if (distance.y < 0) {
  89. movement.y -= 1;
  90. distance.y += 1;
  91. }
  92. toMove.transform.position = movement;
  93. movedSquares++;
  94. if (toMove.movementRate <= movedSquares * 5) { // this one have moved all that they can for one action
  95. Debug.Log(toMove.name + " moved there entire " + toMove.movementRate + " distance, stopping");
  96. break;
  97. }
  98. yield return new WaitForSeconds(1);
  99. distance = tileMap.WorldToCell(targetCreature.transform.position - toMove.transform.position);
  100. Debug.Log(toMove.name + " move to " + targetPosition + "(" + tileMap.WorldToCell(targetPosition) + ") from " + startPosition + "(" + tileMap.WorldToCell(startPosition) + " by moving " + distance);
  101. }
  102. if (safety > 1000) {
  103. throw new System.Exception("LOOPED 1000 times");
  104. }
  105. }
  106. }
  107. yield return null;
  108. }
  109. private void checkIfOccupoied(Vector3 movement) {
  110. }
  111. /* public IEnumerator PerformAction() {
  112. if (startPosition == null || (targetPosition == null && targetCreature == null) || toMove == null) {
  113. Debug.Log("Move action not setup correctly, missing startPosition " + startPosition + " or target position " + targetPosition + " or gameObject " + toMove);
  114. throw new System.Exception("Failed MoveAction");
  115. }
  116. if (targetCreature != null) {
  117. targetPosition = targetCreature.CurrentPos;
  118. }
  119. if (movedSquares >= toMove.movementRate || startPosition.Equals(targetPosition)) {
  120. yield break;
  121. }
  122. UnityEngine.Tilemaps.Tilemap tileMap = GameManagerScript.GetInstance().TileMap;
  123. Vector3Int startPositionAdjusted = tileMap.WorldToCell(startPosition);
  124. Vector3Int targetPositionAdjusted = tileMap.WorldToCell(targetPosition);
  125. Vector3Int distanceToTarget = (startPositionAdjusted - targetPositionAdjusted);
  126. Vector3Int finalPos = tileMap.WorldToCell(startPositionAdjusted - distanceToTarget);
  127. Debug.Log("Start position " + startPosition + " Adjusted: " + startPositionAdjusted + " final pos " + finalPos + " distance " + (-(startPositionAdjusted - targetPositionAdjusted)));
  128. float time = 30 / 6f;
  129. float eleapsedTime = 0f;
  130. while (eleapsedTime < time) {
  131. if (targetCreature != null) {
  132. targetPosition = targetCreature.CurrentPos;
  133. targetPositionAdjusted = tileMap.WorldToCell(targetPosition);
  134. distanceToTarget = (startPositionAdjusted - targetPositionAdjusted);
  135. finalPos = tileMap.WorldToCell(startPositionAdjusted - distanceToTarget);
  136. }
  137. toMove.transform.position = Vector3.Lerp(tileMap.GetCellCenterWorld(startPositionAdjusted), tileMap.GetCellCenterWorld(finalPos), (eleapsedTime / time));
  138. eleapsedTime += Time.deltaTime;
  139. yield return null;
  140. }
  141. } */
  142. }