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
23 private boolean blocking = false;
24 private static final ActionComparator ACTION_COMPARATOR = new ActionComparator();
25
26
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
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
69 List<SpecialActionEvent> newSpecialActionEvents = new ArrayList<SpecialActionEvent>();
70
71 Iterator<SpecialActionEvent> iter = getSpecialActionList().iterator();
72 while (iter.hasNext()) {
73 SpecialActionEvent tmpActionEvent = iter.next();
74 tmpActionEvent.render(graphics, gameObject);
75
76 if (tmpActionEvent.isFinished() == true) {
77
78 tmpActionEvent.cleanup();
79 iter.remove();
80 System.out.println("removing event ; " + tmpActionEvent.getName());
81
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
97 addActions(newSpecialActionEvents);
98
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
113
114 for (SpecialEventPermanent tmpAction : getPermanentActionList()) {
115 tmpAction.update(elapsedTime, gameObject, specialActionListEmpty);
116 if (tmpAction.isBlocking() == true) {
117 blocking = true;
118 }
119
120
121
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 }