MoveAction.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. }