/android/Android(LGame-0.3.2&LAE-1.1)/LGame-0.3.2(OpenGLES)/src/core/org/loon/framework/android/game/action/map/AStarFinderPool.java

http://loon-simple.googlecode.com/ · Java · 123 lines · 82 code · 21 blank · 20 comment · 8 complexity · 5a12a31684a7361d1e0a80deecfed170 MD5 · raw file

  1. package org.loon.framework.android.game.action.map;
  2. import java.util.Iterator;
  3. import java.util.LinkedList;
  4. import org.loon.framework.android.game.core.geom.Vector2f;
  5. /**
  6. * Copyright 2008 - 2010
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  9. * use this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  17. * License for the specific language governing permissions and limitations under
  18. * the License.
  19. *
  20. * @project loonframework
  21. * @author chenpeng
  22. * @email??šceponline@yahoo.com.cn
  23. * @version 0.1
  24. */
  25. public class AStarFinderPool implements Runnable {
  26. private Field2D field;
  27. private Thread pathfinderThread;
  28. private boolean running;
  29. private TaskQueue pathQueue = new TaskQueue();
  30. public AStarFinderPool(int[][] maps) {
  31. this(new Field2D(maps));
  32. }
  33. public AStarFinderPool(Field2D field) {
  34. this.field = field;
  35. this.running = true;
  36. this.pathfinderThread = new Thread(this);
  37. this.pathfinderThread.start();
  38. }
  39. public void run() {
  40. while (running) {
  41. try {
  42. Thread.sleep(1000000);
  43. } catch (InterruptedException ex) {
  44. }
  45. emptyPathQueue();
  46. }
  47. }
  48. private void emptyPathQueue() {
  49. AStarFinder task;
  50. for (; (task = pathQueue.poll()) != null;) {
  51. task.run();
  52. }
  53. }
  54. public void stop() {
  55. running = true;
  56. pathfinderThread.interrupt();
  57. }
  58. public void search(int startx, int starty, int endx, int endy,
  59. boolean flying, boolean flag, AStarFinderListener callback) {
  60. AStarFinder pathfinderTask = new AStarFinder(field, startx, starty,
  61. endx, endy, flying, flag, callback);
  62. AStarFinder existing = pathQueue.contains(pathfinderTask);
  63. if (existing != null) {
  64. existing.update(pathfinderTask);
  65. } else {
  66. pathQueue.add(pathfinderTask);
  67. }
  68. pathfinderThread.interrupt();
  69. }
  70. public void search(int startx, int starty, int endx, int endy,
  71. boolean flying, AStarFinderListener callback) {
  72. search(startx, starty, endx, endy, flying, false, callback);
  73. }
  74. public LinkedList<Vector2f> search(int startX, int startY, int endX,
  75. int endY, boolean flying, boolean flag) {
  76. return new AStarFinder(field, startX, startY, endX, endY, flying, flag)
  77. .findPath();
  78. }
  79. public LinkedList<Vector2f> search(int startX, int startY, int endX,
  80. int endY, boolean flying) {
  81. return new AStarFinder(field, startX, startY, endX, endY, flying, false)
  82. .findPath();
  83. }
  84. class TaskQueue {
  85. private LinkedList<AStarFinder> queue = new LinkedList<AStarFinder>();
  86. public synchronized AStarFinder contains(AStarFinder element) {
  87. for (Iterator<AStarFinder> it = queue.iterator(); it.hasNext();) {
  88. AStarFinder af = it.next();
  89. if (af.equals(element)) {
  90. return af;
  91. }
  92. }
  93. return null;
  94. }
  95. public synchronized AStarFinder poll() {
  96. return queue.poll();
  97. }
  98. public synchronized void add(AStarFinder t) {
  99. queue.add(t);
  100. }
  101. }
  102. }