/android/LGame-Android-0.2.95/src/org/loon/framework/android/game/action/Actions.java

http://loon-simple.googlecode.com/ · Java · 201 lines · 158 code · 23 blank · 20 comment · 40 complexity · 5a97f5f2004b3d8206a5d46f95cd43eb MD5 · raw file

  1. package org.loon.framework.android.game.action;
  2. import java.util.ArrayList;
  3. import org.loon.framework.android.game.core.graphics.window.actor.Actor;
  4. import org.loon.framework.android.game.utils.collection.ArrayMap;
  5. /**
  6. * Copyright 2008 - 2011
  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 Actions {
  26. final private ArrayMap actions;
  27. Actions() {
  28. this.actions = new ArrayMap(10);
  29. }
  30. public void addAction(ActionEvent action, Actor actObject, boolean paused) {
  31. synchronized (actions) {
  32. ActionElement element = (ActionElement) actions.get(actObject);
  33. if (element == null) {
  34. element = new ActionElement(actObject, paused);
  35. actions.put(actObject, element);
  36. }
  37. element.actions.add(action);
  38. action.start(actObject);
  39. }
  40. }
  41. private void deleteElement(ActionElement element) {
  42. synchronized (actions) {
  43. element.actions.clear();
  44. actions.remove(element.key);
  45. }
  46. }
  47. public void removeAllActions(Actor actObject) {
  48. if (actObject == null) {
  49. return;
  50. }
  51. synchronized (actions) {
  52. ActionElement element = (ActionElement) actions.get(actObject);
  53. if (element != null) {
  54. element.actions.clear();
  55. deleteElement(element);
  56. }
  57. }
  58. }
  59. private void removeAction(int index, ActionElement element) {
  60. synchronized (actions) {
  61. element.actions.remove(index);
  62. if (element.actionIndex >= index) {
  63. element.actionIndex--;
  64. }
  65. if (element.actions.isEmpty()) {
  66. deleteElement(element);
  67. }
  68. }
  69. }
  70. public int getCount() {
  71. return actions.size();
  72. }
  73. public void removeAction(Object tag, Actor actObject) {
  74. synchronized (actions) {
  75. ActionElement element = (ActionElement) actions.get(actObject);
  76. if (element != null) {
  77. if (element.actions != null) {
  78. int limit = element.actions.size();
  79. for (int i = 0; i < limit; i++) {
  80. ActionEvent a = (ActionEvent) element.actions.get(i);
  81. if (a.getTag() == tag && a.getOriginal() == actObject) {
  82. removeAction(i, element);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. public void removeAction(ActionEvent action) {
  90. if (action == null) {
  91. return;
  92. }
  93. synchronized (actions) {
  94. ActionElement element = (ActionElement) actions.get(action
  95. .getOriginal());
  96. if (element != null) {
  97. int i = element.actions.indexOf(action);
  98. if (i != -1) {
  99. removeAction(i, element);
  100. }
  101. }
  102. }
  103. }
  104. public ActionEvent getAction(Object tag, Actor actObject) {
  105. synchronized (actions) {
  106. ActionElement element = (ActionElement) actions.get(actObject);
  107. if (element != null) {
  108. if (element.actions != null) {
  109. int limit = element.actions.size();
  110. for (int i = 0; i < limit; i++) {
  111. ActionEvent a = (ActionEvent) element.actions.get(i);
  112. if (a.getTag() == tag)
  113. return a;
  114. }
  115. }
  116. }
  117. return null;
  118. }
  119. }
  120. public void update(long elapsedTime) {
  121. synchronized (actions) {
  122. int size = actions.size();
  123. for (int i = size - 1; i > -1; --i) {
  124. ActionElement currentTarget = (ActionElement) actions.get(i);
  125. synchronized (currentTarget) {
  126. if (!currentTarget.paused) {
  127. for (currentTarget.actionIndex = 0; currentTarget.actionIndex < currentTarget.actions
  128. .size(); currentTarget.actionIndex++) {
  129. currentTarget.currentAction = (ActionEvent) currentTarget.actions
  130. .get(currentTarget.actionIndex);
  131. if (!currentTarget.currentAction.isInit) {
  132. currentTarget.currentAction.isInit = true;
  133. currentTarget.currentAction.onLoad();
  134. }
  135. currentTarget.currentAction.step(elapsedTime);
  136. if (currentTarget.currentAction.isComplete()) {
  137. currentTarget.currentAction.stop();
  138. removeAction(currentTarget.currentAction);
  139. }
  140. currentTarget.currentAction = null;
  141. }
  142. }
  143. if (currentTarget.actions.isEmpty()) {
  144. deleteElement(currentTarget);
  145. }
  146. }
  147. }
  148. }
  149. }
  150. public void stop(Actor actObject) {
  151. synchronized (actions) {
  152. ActionElement element = (ActionElement) actions.get(actObject);
  153. if (element != null) {
  154. element.paused = false;
  155. }
  156. }
  157. }
  158. public void start(Actor actObject) {
  159. synchronized (actions) {
  160. ActionElement element = (ActionElement) actions.get(actObject);
  161. if (element != null) {
  162. element.paused = true;
  163. }
  164. }
  165. }
  166. final static class ActionElement {
  167. private Actor key;
  168. private int actionIndex;
  169. private boolean paused;
  170. private ArrayList<ActionEvent> actions;
  171. private ActionEvent currentAction;
  172. public ActionElement(Actor k, boolean v) {
  173. this.actions = new ArrayList<ActionEvent>(10);
  174. this.key = k;
  175. this.paused = v;
  176. }
  177. }
  178. }