Guest User

Untitled

a guest
Nov 23rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. package org.apollo.game.model;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.Deque;
  5. import java.util.Queue;
  6.  
  7. /**
  8. * A queue of {@link Direction}s which a {@link Character} will follow.
  9. * @author Graham Edgecombe
  10. */
  11. public final class WalkingQueue {
  12.  
  13. /**
  14. * The maximum size of the queue. If any additional steps are added, they
  15. * are discarded.
  16. */
  17. private static final int MAXIMUM_SIZE = 128;
  18.  
  19. /**
  20. * Represents a single point in the queue.
  21. * @author Graham Edgecombe
  22. */
  23. private static final class Point {
  24.  
  25. /**
  26. * The point's position.
  27. */
  28. private final Position position;
  29.  
  30. /**
  31. * The direction to walk to this point.
  32. */
  33. private final Direction direction;
  34.  
  35. /**
  36. * Creates a point.
  37. * @param position The position.
  38. * @param direction The direction.
  39. */
  40. public Point(Position position, Direction direction) {
  41. this.position = position;
  42. this.direction = direction;
  43. }
  44.  
  45. @Override
  46. public String toString() {
  47. return Point.class.getName() + " [direction=" + direction + ", position=" + position + "]";
  48. }
  49.  
  50. }
  51.  
  52. /**
  53. * The character whose walking queue this is.
  54. */
  55. private Character character;
  56.  
  57. /**
  58. * The queue of directions.
  59. */
  60. private Deque<Point> points = new ArrayDeque<Point>();
  61.  
  62. /**
  63. * The old queue of directions.
  64. */
  65. private Deque<Point> oldPoints = new ArrayDeque<Point>();
  66.  
  67. /**
  68. * Flag indicating if this queue (only) should be ran.
  69. */
  70. private boolean runningQueue;
  71.  
  72. /**
  73. * Creates a walking queue for the specified character.
  74. * @param character The character.
  75. */
  76. public WalkingQueue(Character character) {
  77. this.character = character;
  78. }
  79.  
  80. /**
  81. * Called every pulse, updates the queue.
  82. */
  83. public void pulse() {
  84. Position position = character.getPosition();
  85.  
  86. Direction first = Direction.NONE;
  87. Direction second = Direction.NONE;
  88.  
  89. Point next = points.poll();
  90. if (next != null) {
  91. first = next.direction;
  92. position = next.position;
  93.  
  94. if (runningQueue /* or run toggled AND enough energy */) {
  95. next = points.poll();
  96. if (next != null) {
  97. second = next.direction;
  98. position = next.position;
  99. }
  100. }
  101. }
  102.  
  103. character.setDirections(first, second);
  104. character.setPosition(position);
  105. }
  106.  
  107. /**
  108. * Sets the running queue flag.
  109. * @param running The running queue flag.
  110. */
  111. public void setRunningQueue(boolean running) {
  112. this.runningQueue = running;
  113. }
  114.  
  115. /**
  116. * Adds the first step to the queue, attempting to connect the server and
  117. * client position by looking at the previous queue.
  118. * @param clientConnectionPosition The first step.
  119. * @return {@code true} if the queues could be connected correctly,
  120. * {@code false} if not.
  121. */
  122. public boolean addFirstStep(Position clientConnectionPosition) {
  123. Position serverPosition = character.getPosition();
  124.  
  125. int deltaX = clientConnectionPosition.getX() - serverPosition.getX();
  126. int deltaY = clientConnectionPosition.getY() - serverPosition.getY();
  127.  
  128. if (Direction.isConnectable(deltaX, deltaY)) {
  129. points.clear();
  130. oldPoints.clear();
  131.  
  132. addStep(clientConnectionPosition);
  133. return true;
  134. }
  135.  
  136. Queue<Position> travelBackQueue = new ArrayDeque<Position>();
  137.  
  138. Point oldPoint;
  139. while ((oldPoint = oldPoints.pollLast()) != null) {
  140. Position oldPosition = oldPoint.position;
  141.  
  142. deltaX = oldPosition.getX() - serverPosition.getX();
  143. deltaY = oldPosition.getX() - serverPosition.getY();
  144.  
  145. travelBackQueue.add(oldPosition);
  146.  
  147. if (Direction.isConnectable(deltaX, deltaY)) {
  148. points.clear();
  149. oldPoints.clear();
  150.  
  151. for (Position travelBackPosition : travelBackQueue) {
  152. addStep(travelBackPosition);
  153. }
  154.  
  155. addStep(clientConnectionPosition);
  156. return true;
  157. }
  158. }
  159.  
  160. oldPoints.clear();
  161. return false;
  162. }
  163.  
  164. /**
  165. * Adds a step to the queue.
  166. * @param step The step to add.
  167. */
  168. public void addStep(Position step) {
  169. Point last = getLast();
  170.  
  171. int x = step.getX();
  172. int y = step.getY();
  173.  
  174. int deltaX = x - last.position.getX();
  175. int deltaY = y - last.position.getY();
  176.  
  177. int max = Math.max(Math.abs(deltaX), Math.abs(deltaY));
  178.  
  179. for (int i = 0; i < max; i++) {
  180. if (deltaX < 0) {
  181. deltaX++;
  182. } else if (deltaX > 0) {
  183. deltaX--;
  184. }
  185.  
  186. if (deltaY < 0) {
  187. deltaY++;
  188. } else if (deltaY > 0) {
  189. deltaY--;
  190. }
  191.  
  192. addStep(x - deltaX, y - deltaY);
  193. }
  194. }
  195.  
  196. /**
  197. * Adds a step.
  198. * @param x The x coordinate of this step.
  199. * @param y The y coordinate of this step.
  200. */
  201. private void addStep(int x, int y) {
  202. if (points.size() >= MAXIMUM_SIZE) {
  203. return;
  204. }
  205.  
  206. Point last = getLast();
  207.  
  208. int deltaX = x - last.position.getX();
  209. int deltaY = y - last.position.getY();
  210.  
  211. Direction direction = Direction.fromDeltas(deltaX, deltaY);
  212.  
  213. if (direction != Direction.NONE) {
  214. Point p = new Point(new Position(x, y), direction);
  215. points.add(p);
  216. oldPoints.add(p);
  217. }
  218. }
  219.  
  220. /**
  221. * Gets the last point.
  222. * @return The last point.
  223. */
  224. private Point getLast() {
  225. Point last = points.peekLast();
  226. if (last == null) {
  227. return new Point(character.getPosition(), Direction.NONE);
  228. }
  229. return last;
  230. }
  231.  
  232. /**
  233. * Clears the walking queue.
  234. */
  235. public void clear() {
  236. points.clear();
  237. oldPoints.clear();
  238. }
  239.  
  240. /**
  241. * Gets the size of the queue.
  242. * @return The size of the queue.
  243. */
  244. public int size() {
  245. return points.size();
  246. }
  247.  
  248. }
Add Comment
Please, Sign In to add comment