1 package de.jos.game.actions.permanent;
2
3 import java.awt.Graphics2D;
4 import java.util.ArrayList;
5 import java.util.Iterator;
6 import java.util.List;
7
8 import org.apache.commons.lang.StringUtils;
9
10 import com.golden.gamedev.GameObject;
11
12 import de.jos.game.actions.ActionFadeBullets;
13 import de.jos.game.actions.ActionTextFader;
14 import de.jos.game.actions.LayerConstants;
15 import de.jos.game.actions.SpecialActionEvent;
16 import de.jos.game.actions.cleanup.CleanupActionNone;
17 import de.jos.game.logic.AbstractGameBoard;
18 import de.jos.game.logic.CombinationUtils;
19 import de.jos.game.logic.NInARowValidator;
20 import de.jos.game.objects.Bullet;
21 import de.jos.game.objects.Canister;
22 import de.jos.game.xml.Action;
23 import de.jos.game.xml.Colors;
24 import de.jos.game.xml.Combination;
25
26 public class ActionCanister extends AbstractPermanentAction implements SpecialEventPermanent {
27
28 private List<Canister> canisterList = new ArrayList<Canister>();
29
30 private AbstractGameBoard gameBoard = null;
31
32 public ActionCanister(AbstractGameBoard gameBoard) {
33 this.gameBoard = gameBoard;
34 setCleanupAction(new CleanupActionNone(gameBoard));
35 }
36
37 public boolean isFinished() {
38 return false;
39 }
40
41 public int getNumberCanisters() {
42 return canisterList.size();
43 }
44
45 public void addCanister(Canister canister) {
46 canisterList.add(canister);
47 }
48
49 public void render(Graphics2D graphics, GameObject gameObject) {
50 for (Canister tmpCanister : canisterList) {
51 tmpCanister.render(graphics);
52 }
53 }
54
55 public void update(long elapsedTime, GameObject gameObject) {
56 boolean canisterAnimationsFinished = true;
57
58
59 Iterator<Canister> iter = getCanisterList().iterator();
60 while (iter.hasNext()) {
61 Canister tmpCanister = iter.next();
62 if (tmpCanister.isDestroyable()) {
63 iter.remove();
64 }
65 if (tmpCanister.isWaiting() == false) {
66 canisterAnimationsFinished = false;
67 }
68 }
69
70
71 if (canisterAnimationsFinished == true) {
72 Combination combination = checkDissolveStack(getGameBoard().getLevel().getAccumulatedCombinations(),
73 getCanisterList());
74 if (combination != null) {
75
76 clearCanisters();
77
78 getNewSpecialEventList().add(handleFoundCombination(combination));
79
80 ActionTextFader actionTextFader = new ActionTextFader();
81 actionTextFader.setResourceContainer(getResourceContainer());
82 actionTextFader.setText(StringUtils.upperCase(combination.getAction().getDescriptionShort()));
83 actionTextFader.setX(120.0f);
84 actionTextFader.setY(300.0f);
85 getNewSpecialEventList().add(actionTextFader);
86 }
87 }
88
89
90 for (Canister tmpCanister : canisterList) {
91 tmpCanister.update(elapsedTime);
92 }
93 }
94
95 public void clearCanisters() {
96 for (Canister tmpCanister : getCanisterList()) {
97 tmpCanister.empty();
98 }
99 }
100
101 public boolean combinationMatchPossible(Canister canisterToBeAdded, List<Combination> combinations) {
102
103 List<Canister> tmpCanisterList = new ArrayList<Canister>();
104 tmpCanisterList.addAll(getCanisterList());
105 tmpCanisterList.add(canisterToBeAdded);
106
107 Integer color1 = tmpCanisterList.size() < 1 ? null : tmpCanisterList.get(0).getColor();
108 Integer color2 = tmpCanisterList.size() < 2 ? null : tmpCanisterList.get(1).getColor();
109 Integer color3 = tmpCanisterList.size() < 3 ? null : tmpCanisterList.get(2).getColor();
110
111
112 boolean partialMatch = false;
113
114 for (Combination tmpCombination : combinations) {
115 Colors colors = tmpCombination.getColors();
116 partialMatch = ((color1 == null ? true : color1.equals(colors.getColor1()))
117 && (color2 == null ? true : color2.equals(colors.getColor2())) && (color3 == null ? true : color3
118 .equals(colors.getColor3())));
119 if (partialMatch == true) {
120 break;
121 }
122 }
123
124 return partialMatch;
125 }
126
127 private Combination checkDissolveStack(List<Combination> combinations, List<Canister> canisterStack) {
128
129 Combination foundCombination = null;
130
131 if (!canisterStack.isEmpty()) {
132
133 for (Combination tmpCombination : combinations) {
134 Integer color1 = canisterStack.size() < 1 ? null : canisterStack.get(0).getColor();
135 Integer color2 = canisterStack.size() < 2 ? null : canisterStack.get(1).getColor();
136 Integer color3 = canisterStack.size() < 3 ? null : canisterStack.get(2).getColor();
137
138 Colors colors = tmpCombination.getColors();
139 boolean match = (colors.getColor1().equals(color1) && colors.getColor2().equals(color2) && colors.getColor3()
140 .equals(color3));
141
142 if (match == true) {
143 foundCombination = tmpCombination;
144 break;
145 }
146 }
147 }
148
149 return foundCombination;
150 }
151
152 private SpecialActionEvent handleFoundCombination(Combination combination) {
153 SpecialActionEvent specialActionEvent = null;
154 List<Bullet> bulletList = null;
155
156 switch(combination.getAction().getType()) {
157 case Action.TYPE_TIME_BONUS:
158 double bonusInPixels = CombinationUtils.getTimeBonusInPixels(combination.getAction().getTimeBonus());
159 getGameBoard().getActionContainer().getActionCountdown().setTimeBonus(bonusInPixels);
160
161 break;
162 case Action.TYPE_CLEAR_BOARD_COLOR:
163 bulletList = CombinationUtils.getListOfBulletsOnBoardWithColor(combination.getAction().getClearBoardColor(),
164 getGameBoard());
165
166 ActionFadeBullets actionDissolveBullets = new ActionFadeBullets(Bullet.State.DISSOLVING);
167 actionDissolveBullets.setResourceContainer(getResourceContainer());
168 actionDissolveBullets.getBulletList().addAll(bulletList);
169 actionDissolveBullets.init();
170
171
172 for (Bullet tmpBullet : bulletList) {
173 getGameBoard().removeBullet(tmpBullet.getBoardX(), tmpBullet.getBoardY());
174 }
175
176 specialActionEvent = actionDissolveBullets;
177
178 break;
179
180 case Action.TYPE_RESET_REAR_RANK:
181 bulletList = CombinationUtils.getListOfRearRankBullets(getGameBoard());
182
183
184 for (Bullet tmpBullet : bulletList) {
185 tmpBullet.setColor(combination.getAction().getResetRearRank());
186 tmpBullet.init();
187 }
188
189 break;
190
191 case Action.TYPE_FOUR_BALL_FORMATIONS:
192 getResourceContainer().getNInARowValidator().setRequiredAlignedBullets(
193 NInARowValidator.REQUIRED_ALIGNED_BULLETS_FOUR);
194 break;
195
196 default:
197
198 }
199
200 return specialActionEvent;
201 }
202
203 public Integer getLayer() {
204 return LayerConstants.LAYER_1;
205 }
206
207 public boolean isBlocking() {
208 return false;
209 }
210
211 protected List<Canister> getCanisterList() {
212 return canisterList;
213 }
214
215 public AbstractGameBoard getGameBoard() {
216 return gameBoard;
217 }
218
219 public String getName() {
220 return this.getClass().getName();
221 }
222
223 }