View Javadoc

1   package de.jos.game.actions;
2   
3   import java.awt.Graphics2D;
4   import java.util.ArrayList;
5   import java.util.Collections;
6   import java.util.Iterator;
7   import java.util.List;
8   
9   import com.golden.gamedev.GameObject;
10  
11  import de.jos.game.actions.permanent.ActionCanister;
12  import de.jos.game.actions.permanent.ActionCountdown;
13  import de.jos.game.actions.permanent.ActionMoveStatistics;
14  import de.jos.game.actions.permanent.ActionPuzzleSolvedCheck;
15  import de.jos.game.actions.permanent.ActionStatistics;
16  import de.jos.game.actions.permanent.SpecialEventPermanent;
17  import de.jos.game.logic.AbstractGameBoard;
18  import de.jos.game.objects.ResourceContainer;
19  
20  public class ActionContainer {
21  
22    // is the action blocking the selection of the bullets
23    private boolean blocking = false;
24    private static final ActionComparator ACTION_COMPARATOR = new ActionComparator();
25  
26    // list that contains all the special actions
27    private List<SpecialActionEvent> specialActionList = new ArrayList<SpecialActionEvent>();
28  
29    private List<SpecialEventPermanent> permanentActionList = new ArrayList<SpecialEventPermanent>();
30  
31    private ActionCanister actionCanister = null;
32    private ActionStatistics actionStatistics = null;
33    private ActionCountdown actionCountdown = null;
34    private ActionPuzzleSolvedCheck actionPuzzleSolved = null;
35    private ActionMoveStatistics actionMoveStatistics = null;
36  
37    public ActionContainer(ResourceContainer resourceContainer, AbstractGameBoard gameBoard, boolean puzzleMode) {
38      if (puzzleMode == false) {
39        // the permanent actions are only required for the infinite game mode
40        actionCanister = new ActionCanister(gameBoard);
41        actionCanister.setResourceContainer(resourceContainer);
42        actionCanister.init();
43        permanentActionList.add(actionCanister);
44  
45        actionStatistics = new ActionStatistics();
46        actionStatistics.setResourceContainer(resourceContainer);
47        actionStatistics.init();
48        permanentActionList.add(actionStatistics);
49  
50        actionCountdown = new ActionCountdown(gameBoard);
51        actionCountdown.setResourceContainer(resourceContainer);
52        actionCountdown.init();
53        permanentActionList.add(actionCountdown);
54      } else {
55        actionPuzzleSolved = new ActionPuzzleSolvedCheck(gameBoard);
56        actionPuzzleSolved.setResourceContainer(resourceContainer);
57        actionPuzzleSolved.init();
58        permanentActionList.add(actionPuzzleSolved);
59        
60        actionMoveStatistics = new ActionMoveStatistics();
61        actionMoveStatistics.setResourceContainer(resourceContainer);
62        actionMoveStatistics.init();
63        permanentActionList.add(actionMoveStatistics);
64      }
65    }
66  
67    public void render(Graphics2D graphics, GameObject gameObject) {
68      // list of new special events
69      List<SpecialActionEvent> newSpecialActionEvents = new ArrayList<SpecialActionEvent>();
70      // render all the special actions
71      Iterator<SpecialActionEvent> iter = getSpecialActionList().iterator();
72      while (iter.hasNext()) {
73        SpecialActionEvent tmpActionEvent = iter.next();
74        tmpActionEvent.render(graphics, gameObject);
75        // if the action is finished, remove the actionEvent
76        if (tmpActionEvent.isFinished() == true) {
77          // perform the final cleanup action
78          tmpActionEvent.cleanup();
79          iter.remove();
80          System.out.println("removing event ; " + tmpActionEvent.getName());
81          // get the new special event and add them to a list
82          List<SpecialActionEvent> tmpList = tmpActionEvent.getNewSpecialEventList();
83          if (tmpList != null) {
84            newSpecialActionEvents.addAll(tmpList);
85          }
86        }
87      }
88  
89      for (SpecialActionEvent tmpAction : getPermanentActionList()) {
90        tmpAction.render(graphics, gameObject);
91        if (tmpAction.isBlocking() == true) {
92          blocking = true;
93        }
94      }
95  
96      // add the new special events
97      addActions(newSpecialActionEvents);
98      // getSpecialActionList().addAll(newSpecialActionEvents);
99    }
100 
101   public void update(long elapsedTime, GameObject gameObject) {
102     boolean specialActionListEmpty = getSpecialActionList().isEmpty();
103     blocking = false;
104 
105     for (SpecialActionEvent tmpAction : getSpecialActionList()) {
106       tmpAction.update(elapsedTime, gameObject);
107       if (tmpAction.isBlocking() == true) {
108         blocking = true;
109       }
110     }
111 
112     // special event list der permanenten actions hier besonders behandeln,
113     // da es hier kein finish gibt
114     for (SpecialEventPermanent tmpAction : getPermanentActionList()) {
115       tmpAction.update(elapsedTime, gameObject, specialActionListEmpty);
116       if (tmpAction.isBlocking() == true) {
117         blocking = true;
118       }
119 
120       // queue der special events loeschen, da diese sonst pro update
121       // hinzugefuegt werden
122       addActions(tmpAction.getNewSpecialEventList());
123       tmpAction.getNewSpecialEventList().clear();
124     }
125   }
126 
127   public void addAction(SpecialActionEvent actionEvent) {
128     if (actionEvent != null) {
129       getSpecialActionList().add(actionEvent);
130       actionEvent.init();
131       Collections.sort(getSpecialActionList(), ACTION_COMPARATOR);
132     }
133   }
134 
135   public void addActions(List<SpecialActionEvent> newActions) {
136     if (newActions.isEmpty() == false) {
137       for (SpecialActionEvent tmpAction : newActions) {
138         if (tmpAction != null) {
139           System.out.println("adding actionEvent : " + tmpAction.getName());
140           addAction(tmpAction);
141         }
142       }
143     }
144   }
145 
146   public boolean isBlocking() {
147     return blocking;
148   }
149 
150   public void setBlocking(boolean blocking) {
151     this.blocking = blocking;
152   }
153 
154   private List<SpecialActionEvent> getSpecialActionList() {
155     return specialActionList;
156   }
157 
158   private List<SpecialEventPermanent> getPermanentActionList() {
159     return permanentActionList;
160   }
161 
162   public ActionCanister getActionCanister() {
163     return actionCanister;
164   }
165 
166   public ActionStatistics getActionStatistics() {
167     return actionStatistics;
168   }
169 
170   public ActionCountdown getActionCountdown() {
171     return actionCountdown;
172   }
173 
174   public ActionMoveStatistics getActionMoveStatistics() {
175     return actionMoveStatistics;
176   }
177   
178 }