1 | package main;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Frame;
|
---|
5 | import java.awt.Font;
|
---|
6 | import java.awt.FontMetrics;
|
---|
7 | import java.awt.Graphics;
|
---|
8 | import java.awt.GraphicsConfiguration;
|
---|
9 | import java.awt.GraphicsDevice;
|
---|
10 | import java.awt.GraphicsEnvironment;
|
---|
11 | import java.awt.MouseInfo;
|
---|
12 | import java.awt.Point;
|
---|
13 | import java.awt.Toolkit;
|
---|
14 | import java.awt.event.KeyEvent;
|
---|
15 | import java.awt.event.KeyListener;
|
---|
16 | import java.awt.event.MouseEvent;
|
---|
17 | import java.awt.event.MouseListener;
|
---|
18 | import java.awt.image.BufferStrategy;
|
---|
19 |
|
---|
20 | import java.io.FileOutputStream;
|
---|
21 | import java.io.OutputStream;
|
---|
22 | import java.io.PrintStream;
|
---|
23 |
|
---|
24 | import java.util.Iterator;
|
---|
25 | import java.util.LinkedList;
|
---|
26 |
|
---|
27 | import gamegui.Align;
|
---|
28 | import gamegui.Animation;
|
---|
29 | import gamegui.Button;
|
---|
30 | import gamegui.Label;
|
---|
31 | import gamegui.Member;
|
---|
32 | import gamegui.MultiTextbox;
|
---|
33 | import gamegui.Textbox;
|
---|
34 | import gamegui.Window;
|
---|
35 |
|
---|
36 | import utils.DynamicImage;
|
---|
37 | import utils.Utils;
|
---|
38 |
|
---|
39 | public class LastDefenseMain implements KeyListener, MouseListener {
|
---|
40 | private static final boolean RUNNING_FROM_JAR = false;
|
---|
41 | GameState gameState;
|
---|
42 | AuxState auxState;
|
---|
43 | boolean started;
|
---|
44 | boolean done;
|
---|
45 | boolean showFps;
|
---|
46 | int frameCount;
|
---|
47 | int lastFrameCount;
|
---|
48 | int refreshRate;
|
---|
49 | long lastFpsUpdate;
|
---|
50 | Graphics g;
|
---|
51 | Frame frmMain;
|
---|
52 | Window wndMain;
|
---|
53 | Window wndInfo;
|
---|
54 | Window wndCredits;
|
---|
55 | Window wndDiagnostics;
|
---|
56 | Window wndMessage;
|
---|
57 | DynamicImage background;
|
---|
58 | DynamicImage yellowCursor;
|
---|
59 | DynamicImage greenCursor;
|
---|
60 | DynamicImage redCursor;
|
---|
61 | DynamicImage cursor;
|
---|
62 | Textbox selectedText;
|
---|
63 | Button selectedButton;
|
---|
64 | Font font11;
|
---|
65 | Font font12;
|
---|
66 | Font font14;
|
---|
67 | Font font24;
|
---|
68 | Font fontTT;
|
---|
69 | Font fontCustom11;
|
---|
70 | Font fontCustom12;
|
---|
71 | Font fontCustom14;
|
---|
72 | Font fontCustom24;
|
---|
73 | Font fontCustom30;
|
---|
74 | Font fontCustom48;
|
---|
75 | Font guiFont12;
|
---|
76 | Font guiFont14;
|
---|
77 | Color color1;
|
---|
78 | Color color2;
|
---|
79 | FontMetrics m;
|
---|
80 | Color redHaze;
|
---|
81 | Color blueHaze;
|
---|
82 | LinkedList<Entity> lstObjects;
|
---|
83 | LinkedList<Unit> lstShips;
|
---|
84 | LinkedList<Unit> lstTurrets;
|
---|
85 | long timeLastUpdated;
|
---|
86 | int resources;
|
---|
87 | Animation turret;
|
---|
88 | Animation shield;
|
---|
89 | Animation projectile;
|
---|
90 | Animation assaultShip;
|
---|
91 | Level[] levels;
|
---|
92 | int curLevel;
|
---|
93 | public static int resUsed;
|
---|
94 | public static int shipsKilled;
|
---|
95 | public static int turretsKilled;
|
---|
96 | public static int shieldsKilled;
|
---|
97 | boolean pause;
|
---|
98 | long timePaused;
|
---|
99 | long pauseDuration;
|
---|
100 | boolean showCost;
|
---|
101 | long timeShowingCost;
|
---|
102 | long costDuration;
|
---|
103 | int cost;
|
---|
104 |
|
---|
105 | public LastDefenseMain(final GraphicsDevice device) {
|
---|
106 | this.started = false;
|
---|
107 | this.pauseDuration = 3000L;
|
---|
108 | this.costDuration = 1200L;
|
---|
109 | try {
|
---|
110 | final GraphicsConfiguration gc = device.getDefaultConfiguration();
|
---|
111 | (this.frmMain = new Frame(gc)).setUndecorated(true);
|
---|
112 | this.frmMain.setIgnoreRepaint(true);
|
---|
113 | device.setFullScreenWindow(this.frmMain);
|
---|
114 | if (device.isDisplayChangeSupported()) {
|
---|
115 | Utils.chooseBestDisplayMode(device, 800, 600);
|
---|
116 | }
|
---|
117 | this.frmMain.addMouseListener(this);
|
---|
118 | this.frmMain.addKeyListener(this);
|
---|
119 | this.frmMain.createBufferStrategy(2);
|
---|
120 | final BufferStrategy bufferStrategy = this.frmMain.getBufferStrategy();
|
---|
121 | this.g = bufferStrategy.getDrawGraphics();
|
---|
122 | this.refreshRate = device.getDisplayMode().getRefreshRate();
|
---|
123 | Utils.init(gc, RUNNING_FROM_JAR);
|
---|
124 | this.gameState = GameState.Main;
|
---|
125 | this.auxState = AuxState.None;
|
---|
126 | this.done = false;
|
---|
127 | this.showFps = false;
|
---|
128 | this.frameCount = 0;
|
---|
129 | this.lastFrameCount = 0;
|
---|
130 | this.lastFpsUpdate = System.nanoTime();
|
---|
131 | this.selectedText = null;
|
---|
132 | this.selectedButton = null;
|
---|
133 | this.timeLastUpdated = 0L;
|
---|
134 | this.lstObjects = new LinkedList<Entity>();
|
---|
135 | this.lstShips = new LinkedList<Unit>();
|
---|
136 | this.lstTurrets = new LinkedList<Unit>();
|
---|
137 | final Toolkit tk = Toolkit.getDefaultToolkit();
|
---|
138 | this.frmMain.setCursor(tk.createCustomCursor(tk.createImage(""), new Point(), null));
|
---|
139 | this.loadGUI(bufferStrategy);
|
---|
140 | this.loadObjects();
|
---|
141 | this.started = true;
|
---|
142 | while (!this.done) {
|
---|
143 | this.g = bufferStrategy.getDrawGraphics();
|
---|
144 | this.handleGameEvents();
|
---|
145 | this.render(this.g);
|
---|
146 | if (this.frmMain != null) {
|
---|
147 | this.detectMouseOver(this.g);
|
---|
148 | final Point e = MouseInfo.getPointerInfo().getLocation();
|
---|
149 | if (e != null) {
|
---|
150 | this.cursor.draw(this.g, e.x - 5, e.y - 2);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | this.g.dispose();
|
---|
154 | bufferStrategy.show();
|
---|
155 | this.frameCount++;
|
---|
156 | if (System.nanoTime() - 1000000000L >= this.lastFpsUpdate) {
|
---|
157 | this.lastFpsUpdate = System.nanoTime();
|
---|
158 | this.lastFrameCount = this.frameCount;
|
---|
159 | this.frameCount = 0;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | } catch (Throwable th) {
|
---|
163 | th.printStackTrace();
|
---|
164 | return;
|
---|
165 | } finally {
|
---|
166 | device.setFullScreenWindow(null);
|
---|
167 | }
|
---|
168 | device.setFullScreenWindow(null);
|
---|
169 | }
|
---|
170 |
|
---|
171 | private void loadGUI(final BufferStrategy bufferStrategy) {
|
---|
172 | (this.background = new DynamicImage("background.png")).draw(bufferStrategy.getDrawGraphics(), 0, 0);
|
---|
173 | this.font11 = new Font("Arial", 0, 11);
|
---|
174 | this.font12 = new Font("Arial", 0, 12);
|
---|
175 | this.font14 = new Font("Arial", 0, 14);
|
---|
176 | this.font24 = new Font("Arial", 0, 24);
|
---|
177 | this.fontTT = new Font("Courier New", 0, 11);
|
---|
178 | this.guiFont12 = new Font("Garamond", 1, 12);
|
---|
179 | this.guiFont14 = new Font("Garamond", 1, 14);
|
---|
180 | this.m = this.g.getFontMetrics(this.guiFont12);
|
---|
181 | try {
|
---|
182 | final Font fontCustom = Utils.loadFont("images/gui/Last_words.ttf");
|
---|
183 | this.fontCustom11 = fontCustom.deriveFont(0, 11.0f);
|
---|
184 | this.fontCustom12 = fontCustom.deriveFont(0, 12.0f);
|
---|
185 | this.fontCustom14 = fontCustom.deriveFont(0, 14.0f);
|
---|
186 | this.fontCustom24 = fontCustom.deriveFont(0, 24.0f);
|
---|
187 | this.fontCustom30 = fontCustom.deriveFont(0, 30.0f);
|
---|
188 | this.fontCustom48 = fontCustom.deriveFont(0, 48.0f);
|
---|
189 | }
|
---|
190 | catch (Exception e) {
|
---|
191 | e.printStackTrace();
|
---|
192 | }
|
---|
193 | this.yellowCursor = new DynamicImage("gui/cursor.png");
|
---|
194 | this.greenCursor = new DynamicImage("gui/cursorgreen.png");
|
---|
195 | this.redCursor = new DynamicImage("gui/cursorred.png");
|
---|
196 | this.cursor = this.yellowCursor;
|
---|
197 | this.wndMain = new Window("main", 0, 0, 800, 600, true);
|
---|
198 | FontMetrics metrics = this.g.getFontMetrics(this.fontCustom48);
|
---|
199 | this.wndMain.add(new Button("title", 400 - metrics.stringWidth("Last Defense") / 2, 120, 0, 0, "Last Defense", this.fontCustom48, new Color(20, 200, 20), false));
|
---|
200 | this.wndMain.background = this.background;
|
---|
201 | this.color2 = Color.green;
|
---|
202 | this.color1 = new Color(0, 160, 0);
|
---|
203 | metrics = this.g.getFontMetrics(this.fontCustom30);
|
---|
204 | this.wndMain.add(new Button("new game", 100, 270, metrics.stringWidth("Start Game"), metrics.getHeight(), "Start Game", this.fontCustom30, this.color1, false));
|
---|
205 | this.wndMain.add(new Button("game info", 140, 320, metrics.stringWidth("Game Info"), metrics.getHeight(), "Game Info", this.fontCustom30, this.color1, false));
|
---|
206 | metrics = this.g.getFontMetrics(this.fontCustom24);
|
---|
207 | this.wndMain.add(new Button("credits", 15, 565, metrics.stringWidth("Credits"), metrics.getHeight(), "Credits", this.fontCustom24, this.color1, false));
|
---|
208 | this.wndMain.add(new Button("quit", 730, 565, metrics.stringWidth("Quit"), metrics.getHeight(), "Quit", this.fontCustom24, this.color1, false));
|
---|
209 | this.wndInfo = new Window("info", 0, 0, 800, 600, true);
|
---|
210 | this.wndInfo.background = this.background;
|
---|
211 | this.wndInfo.add(new Label("title", 250, 15, 300, 20, "Game Info", this.font24, new Color(0, 160, 0)));
|
---|
212 | this.wndInfo.add(new MultiTextbox("info", 50, 120, 700, 430, "", false, this.font14, this.g.getFontMetrics(this.font14)));
|
---|
213 | ((MultiTextbox)this.wndInfo.getMember("info")).setBorder(true);
|
---|
214 | ((MultiTextbox)this.wndInfo.getMember("info")).setText(" You must defend against some sort of invasion. You have turrets and shields at your disposal. Turrets will automatically fire at enemy fighters and shields will generate a small forcefield that protects nearby turrets against enemy fire.\n Use your left mouse button to place turrets and your right mouse button to place shields. Both of these cost resources, so make sure to use the resources you have wisely. You start with 100 resources and you can get more as you destroy enemy fighters. Each turret costs 30 resources and each shield costs 50 resources.\n The enemy fighters descend in waves from the top of the screen. Once a wave is defeated, all surviving turrets and shields will have their hitpoints restored to full and the next wave will immediately come.\n There are 20 waves total. Good luck!\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n(Click your mouse to return to the main menu)");
|
---|
215 | this.wndInfo.add(new Label("", 150, 410, 100, 20, "Turret", this.font14, Align.Center));
|
---|
216 | this.wndInfo.add(new Label("", 350, 410, 100, 20, "Shield", this.font14, Align.Center));
|
---|
217 | this.wndInfo.add(new Label("", 550, 410, 100, 20, "Fighter", this.font14, Align.Center));
|
---|
218 | (this.wndCredits = new Window("main", 0, 0, 800, 600, true)).add(new Label("title", 250, 15, 300, 20, "Credits", this.fontCustom24, this.color2));
|
---|
219 | this.wndCredits.background = this.background;
|
---|
220 | this.wndCredits.add(new Label("1", 250, 160, 300, 20, "Created By", this.guiFont14, this.color2));
|
---|
221 | this.wndCredits.add(new Label("2", 250, 180, 300, 20, "Dmitry Portnoy", this.guiFont12, this.color2));
|
---|
222 | this.wndCredits.add(new Label("4", 250, 220, 300, 20, "Free Art", this.guiFont14, this.color2));
|
---|
223 | this.wndCredits.add(new Label("5", 250, 240, 300, 20, "martinstudio.kings-field.com", this.guiFont12, this.color2));
|
---|
224 | (this.wndMessage = new Window("message", 290, 135, 220, 160)).add(new Label("label", 20, 15, 180, 12, "none", this.font12));
|
---|
225 | this.wndMessage.add(new Button("button", 70, 115, 80, 30, "OK", this.font12, Align.Center));
|
---|
226 | this.wndDiagnostics = new Window("diagnostics", 0, 0, 400, 140, true);
|
---|
227 | }
|
---|
228 |
|
---|
229 | private void loadObjects() {
|
---|
230 | (this.assaultShip = new Animation("", 0, 0, 64, 64, 200, true)).addFrame(new DynamicImage("ships/assault/assault1.png"));
|
---|
231 | this.assaultShip.addFrame(new DynamicImage("ships/assault/assault2.png"));
|
---|
232 | this.assaultShip.addFrame(new DynamicImage("ships/assault/assault3.png"));
|
---|
233 | this.assaultShip.addFrame(new DynamicImage("ships/assault/assault4.png"));
|
---|
234 | (this.turret = new Animation("", 0, 0, 42, 45, 200, true)).addFrame(new DynamicImage("turrets/turret2.png"));
|
---|
235 | (this.shield = new Animation("", 0, 0, 70, 44, 200, true)).addFrame(new DynamicImage("turrets/shield.png"));
|
---|
236 | (this.projectile = new Animation("", 0, 0, 4, 18, 200, true)).addFrame(new DynamicImage("laser.png"));
|
---|
237 | this.levels = new Level[20];
|
---|
238 | final int[] levelNum = { 3, 3, 4, 5, 6, 8, 10, 12, 14, 17, 20, 24, 28, 34, 40, 45, 50, 56, 62, 70 };
|
---|
239 | for (int x = 0; x < levelNum.length; ++x) {
|
---|
240 | this.levels[x] = new Level();
|
---|
241 | for (int y = 0; y < levelNum[x]; ++y) {
|
---|
242 | this.levels[x].addUnit(new Ship("", this.assaultShip, 5, 40, 200, this.projectile));
|
---|
243 | }
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | private void loadLevel(final Level l) {
|
---|
248 | final Iterator<Ship> iter = l.lstUnits.iterator();
|
---|
249 | while (iter.hasNext()) {
|
---|
250 | this.addObject(new Ship(iter.next()));
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | private synchronized void handleGameEvents() {
|
---|
255 | switch (this.gameState) {
|
---|
256 | case Game: {
|
---|
257 | this.spawnObjects();
|
---|
258 | this.updateObjects();
|
---|
259 | break;
|
---|
260 | }
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | private synchronized void render(final Graphics g) {
|
---|
265 | g.setColor(Color.black);
|
---|
266 | g.fillRect(0, 0, 800, 600);
|
---|
267 | switch (this.gameState) {
|
---|
268 | case Main: {
|
---|
269 | this.wndMain.draw(g);
|
---|
270 | break;
|
---|
271 | }
|
---|
272 | case Info: {
|
---|
273 | this.drawInfo(g);
|
---|
274 | break;
|
---|
275 | }
|
---|
276 | case Credits: {
|
---|
277 | this.drawCredits(g);
|
---|
278 | break;
|
---|
279 | }
|
---|
280 | case Game: {
|
---|
281 | this.drawGame(g);
|
---|
282 | break;
|
---|
283 | }
|
---|
284 | }
|
---|
285 | switch (this.auxState) {
|
---|
286 | case MsgBox: {
|
---|
287 | this.wndMessage.draw(g);
|
---|
288 | g.drawString("Level reached: " + (this.curLevel + 1), 300, 200);
|
---|
289 | g.drawString("Ships destroyed: " + LastDefenseMain.shipsKilled, 300, 215);
|
---|
290 | g.drawString("Resources Used: " + LastDefenseMain.resUsed, 300, 230);
|
---|
291 | g.drawString("Turrets lost: " + LastDefenseMain.turretsKilled, 430, 200);
|
---|
292 | g.drawString("Shields lost: " + LastDefenseMain.shieldsKilled, 430, 215);
|
---|
293 | break;
|
---|
294 | }
|
---|
295 | }
|
---|
296 | if (this.showFps) {
|
---|
297 | this.drawDiagnostics(g);
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | public void showMessage(final String text) {
|
---|
302 | this.auxState = AuxState.MsgBox;
|
---|
303 | ((Label)this.wndMessage.getMember("label")).setText(text);
|
---|
304 | }
|
---|
305 |
|
---|
306 | private void drawInfo(final Graphics g) {
|
---|
307 | this.wndInfo.draw(g);
|
---|
308 | this.turret.draw(g, 200, 400);
|
---|
309 | this.shield.draw(g, 400, 400);
|
---|
310 | this.assaultShip.draw(g, 600, 400);
|
---|
311 | }
|
---|
312 |
|
---|
313 | private void drawCredits(final Graphics g) {
|
---|
314 | this.wndCredits.draw(g);
|
---|
315 | }
|
---|
316 |
|
---|
317 | private void drawGame(final Graphics g) {
|
---|
318 | this.background.draw(g, 0, 0);
|
---|
319 | for (final Unit cur : this.lstTurrets) {
|
---|
320 | if (cur instanceof Shield) {
|
---|
321 | final Shield shield = (Shield)cur;
|
---|
322 | g.setColor(new Color(0, 0, 255, 100 * shield.hitpoints / shield.maxHitpoints));
|
---|
323 | g.fillOval(shield.loc.getX() - shield.radius, shield.loc.getY() - shield.radius, shield.radius * 2, shield.radius * 2);
|
---|
324 | }
|
---|
325 | }
|
---|
326 | final Iterator<Entity> iter = this.lstObjects.iterator();
|
---|
327 | while (iter.hasNext()) {
|
---|
328 | iter.next().draw(g);
|
---|
329 | }
|
---|
330 | g.setColor(Color.green);
|
---|
331 | g.drawString("Resources: " + this.resources, 0, 15);
|
---|
332 | g.drawString("Level: " + (this.curLevel + 1), 750, 15);
|
---|
333 | if (this.showCost) {
|
---|
334 | if (System.currentTimeMillis() - this.timeShowingCost >= this.costDuration) {
|
---|
335 | this.showCost = false;
|
---|
336 | } else {
|
---|
337 | g.drawString(new StringBuilder().append(this.cost).toString(), MouseInfo.getPointerInfo().getLocation().x - 8, MouseInfo.getPointerInfo().getLocation().y - 25);
|
---|
338 | }
|
---|
339 | }
|
---|
340 | }
|
---|
341 |
|
---|
342 | private void drawDiagnostics(final Graphics g) {
|
---|
343 | this.wndDiagnostics.draw(g);
|
---|
344 | g.setColor(Color.green);
|
---|
345 | g.setFont(this.fontTT);
|
---|
346 | g.drawString("Unthrottled FPS: " + this.lastFrameCount, 0, 15);
|
---|
347 | g.drawString("Monitor Refresh Rate: " + this.refreshRate, 0, 30);
|
---|
348 | g.drawString("Player Info", 0, 60);
|
---|
349 | }
|
---|
350 |
|
---|
351 | private synchronized void detectMouseOver(final Graphics g) {
|
---|
352 | final Point e = MouseInfo.getPointerInfo().getLocation();
|
---|
353 | if (e == null) {
|
---|
354 | return;
|
---|
355 | }
|
---|
356 | g.setFont(this.guiFont12);
|
---|
357 | this.cursor = this.yellowCursor;
|
---|
358 | switch (this.gameState) {
|
---|
359 | case Main: {
|
---|
360 | if (this.selectedButton != null) {
|
---|
361 | this.selectedButton.color = this.color1;
|
---|
362 | }
|
---|
363 | if (this.wndMain.getMember("new game").isClicked(e.x, e.y)) {
|
---|
364 | this.selectedButton = (Button)this.wndMain.getMember("new game");
|
---|
365 | }
|
---|
366 | else if (this.wndMain.getMember("game info").isClicked(e.x, e.y)) {
|
---|
367 | this.selectedButton = (Button)this.wndMain.getMember("game info");
|
---|
368 | }
|
---|
369 | else if (this.wndMain.getMember("credits").isClicked(e.x, e.y)) {
|
---|
370 | this.selectedButton = (Button)this.wndMain.getMember("credits");
|
---|
371 | }
|
---|
372 | else if (this.wndMain.getMember("quit").isClicked(e.x, e.y)) {
|
---|
373 | this.selectedButton = (Button)this.wndMain.getMember("quit");
|
---|
374 | }
|
---|
375 | else {
|
---|
376 | this.selectedButton = null;
|
---|
377 | }
|
---|
378 | if (this.selectedButton != null) {
|
---|
379 | this.selectedButton.color = this.color2;
|
---|
380 | break;
|
---|
381 | }
|
---|
382 | break;
|
---|
383 | }
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|
387 | private void spawnObjects() {
|
---|
388 | if (this.timeLastUpdated == 0L) {
|
---|
389 | this.timeLastUpdated = System.currentTimeMillis();
|
---|
390 | return;
|
---|
391 | }
|
---|
392 | final long timeCurrent = System.currentTimeMillis();
|
---|
393 | if (timeCurrent - this.timeLastUpdated >= 2000L) {
|
---|
394 | this.timeLastUpdated = timeCurrent;
|
---|
395 | }
|
---|
396 | }
|
---|
397 |
|
---|
398 | private void updateObjects() {
|
---|
399 | if (this.pause) {
|
---|
400 | if (System.currentTimeMillis() - this.timePaused >= this.pauseDuration) {
|
---|
401 | this.pause = false;
|
---|
402 | this.loadLevel(this.levels[this.curLevel]);
|
---|
403 | }
|
---|
404 | }
|
---|
405 | else if (this.lstShips.size() == 0) {
|
---|
406 | this.pause = true;
|
---|
407 | this.timePaused = System.currentTimeMillis();
|
---|
408 | ++this.curLevel;
|
---|
409 | if (this.curLevel == 20) {
|
---|
410 | this.gameState = GameState.Main;
|
---|
411 | this.showCost = false;
|
---|
412 | this.showMessage("You have won!");
|
---|
413 | return;
|
---|
414 | }
|
---|
415 | for (final Unit cur : this.lstTurrets) {
|
---|
416 | cur.hitpoints = cur.maxHitpoints;
|
---|
417 | }
|
---|
418 | }
|
---|
419 | else if (this.lstTurrets.size() == 0 && this.resources <= 30) {
|
---|
420 | this.gameState = GameState.Main;
|
---|
421 | this.showCost = false;
|
---|
422 | this.showMessage("You have lost!");
|
---|
423 | return;
|
---|
424 | }
|
---|
425 | final LinkedList<Entity> lstTemp = new LinkedList<Entity>();
|
---|
426 | for (final Entity cur2 : this.lstObjects) {
|
---|
427 | if (cur2 instanceof Ship) {
|
---|
428 | cur2.update(lstTemp, this.lstTurrets);
|
---|
429 | }
|
---|
430 | else if (cur2 instanceof Shield) {
|
---|
431 | cur2.update(this.lstObjects, null);
|
---|
432 | }
|
---|
433 | else {
|
---|
434 | cur2.update(lstTemp, this.lstShips);
|
---|
435 | }
|
---|
436 | }
|
---|
437 | Iterator<Entity> iter2 = this.lstObjects.iterator();
|
---|
438 | while (iter2.hasNext()) {
|
---|
439 | final Entity cur2 = iter2.next();
|
---|
440 | if (cur2.remove) {
|
---|
441 | iter2.remove();
|
---|
442 | this.removeObject(cur2);
|
---|
443 | }
|
---|
444 | }
|
---|
445 | iter2 = lstTemp.iterator();
|
---|
446 | while (iter2.hasNext()) {
|
---|
447 | this.addObject(iter2.next());
|
---|
448 | }
|
---|
449 | }
|
---|
450 |
|
---|
451 | private void addObject(final Entity obj) {
|
---|
452 | if (obj instanceof Turret) {
|
---|
453 | this.lstTurrets.add((Turret)obj);
|
---|
454 | }
|
---|
455 | else if (obj instanceof Shield) {
|
---|
456 | this.lstTurrets.add((Shield)obj);
|
---|
457 | }
|
---|
458 | else if (obj instanceof Ship) {
|
---|
459 | this.lstShips.add((Ship)obj);
|
---|
460 | }
|
---|
461 | this.lstObjects.add(obj);
|
---|
462 | }
|
---|
463 |
|
---|
464 | private void removeObject(final Entity obj) {
|
---|
465 | if (obj instanceof Turret) {
|
---|
466 | this.lstTurrets.remove(obj);
|
---|
467 | ++LastDefenseMain.turretsKilled;
|
---|
468 | }
|
---|
469 | if (obj instanceof Shield) {
|
---|
470 | this.lstTurrets.remove(obj);
|
---|
471 | ++LastDefenseMain.shieldsKilled;
|
---|
472 | }
|
---|
473 | else if (obj instanceof Ship) {
|
---|
474 | this.lstShips.remove(obj);
|
---|
475 | ++LastDefenseMain.shipsKilled;
|
---|
476 | this.resources += 15;
|
---|
477 | }
|
---|
478 | }
|
---|
479 |
|
---|
480 | private synchronized void handleMouseInput(final MouseEvent e) {
|
---|
481 | if (!this.started) {
|
---|
482 | return;
|
---|
483 | }
|
---|
484 | this.selectText(null);
|
---|
485 | switch (this.auxState) {
|
---|
486 | case None: {
|
---|
487 | switch (this.gameState) {
|
---|
488 | case Main: {
|
---|
489 | if (this.wndMain.getMember("new game").isClicked(e.getX(), e.getY())) {
|
---|
490 | this.lstObjects.clear();
|
---|
491 | this.lstTurrets.clear();
|
---|
492 | this.lstShips.clear();
|
---|
493 | this.addObject(new Turret("Turret", this.turret, 5, 40, 400, this.projectile));
|
---|
494 | this.lstTurrets.get(this.lstTurrets.size() - 1).setLocation(new Location(250.0, 450.0));
|
---|
495 | this.curLevel = 0;
|
---|
496 | this.resources = 100;
|
---|
497 | LastDefenseMain.resUsed = 0;
|
---|
498 | LastDefenseMain.shipsKilled = 0;
|
---|
499 | LastDefenseMain.turretsKilled = 0;
|
---|
500 | LastDefenseMain.shieldsKilled = 0;
|
---|
501 | this.pause = true;
|
---|
502 | this.showCost = false;
|
---|
503 | this.timePaused = System.currentTimeMillis();
|
---|
504 | this.gameState = GameState.Game;
|
---|
505 | break;
|
---|
506 | }
|
---|
507 | if (this.wndMain.getMember("game info").isClicked(e.getX(), e.getY())) {
|
---|
508 | this.gameState = GameState.Info;
|
---|
509 | break;
|
---|
510 | }
|
---|
511 | if (this.wndMain.getMember("credits").isClicked(e.getX(), e.getY())) {
|
---|
512 | this.gameState = GameState.Credits;
|
---|
513 | break;
|
---|
514 | }
|
---|
515 | if (this.wndMain.getMember("quit").isClicked(e.getX(), e.getY())) {
|
---|
516 | this.done = true;
|
---|
517 | break;
|
---|
518 | }
|
---|
519 | break;
|
---|
520 | }
|
---|
521 | case Info: {
|
---|
522 | this.gameState = GameState.Main;
|
---|
523 | break;
|
---|
524 | }
|
---|
525 | case Credits: {
|
---|
526 | this.gameState = GameState.Main;
|
---|
527 | break;
|
---|
528 | }
|
---|
529 | case Game: {
|
---|
530 | if (e.getButton() == 1) {
|
---|
531 | if (this.resources >= 30) {
|
---|
532 | this.resources -= 30;
|
---|
533 | LastDefenseMain.resUsed += 30;
|
---|
534 | this.addObject(new Turret("TurretX", this.turret, 5, 40, 400, this.projectile));
|
---|
535 | this.lstTurrets.get(this.lstTurrets.size() - 1).setLocation(new Location(e.getX(), e.getY()));
|
---|
536 | this.cost = -30;
|
---|
537 | }
|
---|
538 | else {
|
---|
539 | this.cost = 0;
|
---|
540 | }
|
---|
541 | }
|
---|
542 | else if (e.getButton() == 3) {
|
---|
543 | if (this.resources >= 50) {
|
---|
544 | this.resources -= 50;
|
---|
545 | LastDefenseMain.resUsed += 50;
|
---|
546 | this.addObject(new Shield("ShieldX", this.shield, 200, 100));
|
---|
547 | this.lstTurrets.get(this.lstTurrets.size() - 1).setLocation(new Location(e.getX(), e.getY()));
|
---|
548 | this.cost = -50;
|
---|
549 | }
|
---|
550 | else {
|
---|
551 | this.cost = 0;
|
---|
552 | }
|
---|
553 | }
|
---|
554 | if (this.cost != 0) {
|
---|
555 | this.showCost = true;
|
---|
556 | this.timeShowingCost = System.currentTimeMillis();
|
---|
557 | break;
|
---|
558 | }
|
---|
559 | break;
|
---|
560 | }
|
---|
561 | }
|
---|
562 | break;
|
---|
563 | }
|
---|
564 | case MsgBox: {
|
---|
565 | if (this.wndMessage.getMember("button").isClicked(e.getX(), e.getY())) {
|
---|
566 | this.auxState = AuxState.None;
|
---|
567 | break;
|
---|
568 | }
|
---|
569 | break;
|
---|
570 | }
|
---|
571 | }
|
---|
572 | }
|
---|
573 |
|
---|
574 | private synchronized void handleMouseRelease(final MouseEvent e) {
|
---|
575 | }
|
---|
576 |
|
---|
577 | private synchronized void handleKeyboardInput(final KeyEvent e) {
|
---|
578 | if (this.selectedText != null) {
|
---|
579 | this.selectedText.handleEvent(e);
|
---|
580 | }
|
---|
581 | else if (e.getKeyCode() == 27) {
|
---|
582 | if (this.gameState == GameState.Game || this.gameState == GameState.Info) {
|
---|
583 | this.gameState = GameState.Main;
|
---|
584 | this.showCost = false;
|
---|
585 | }
|
---|
586 | else {
|
---|
587 | this.done = true;
|
---|
588 | }
|
---|
589 | }
|
---|
590 | else if (e.getKeyCode() == 83) {
|
---|
591 | this.showFps = !this.showFps;
|
---|
592 | }
|
---|
593 | }
|
---|
594 |
|
---|
595 | @Override
|
---|
596 | public void mousePressed(final MouseEvent e) {
|
---|
597 | this.handleMouseInput(e);
|
---|
598 | }
|
---|
599 |
|
---|
600 | @Override
|
---|
601 | public void mouseReleased(final MouseEvent e) {
|
---|
602 | this.handleMouseRelease(e);
|
---|
603 | }
|
---|
604 |
|
---|
605 | @Override
|
---|
606 | public void mouseEntered(final MouseEvent e) {
|
---|
607 | }
|
---|
608 |
|
---|
609 | @Override
|
---|
610 | public void mouseExited(final MouseEvent e) {
|
---|
611 | }
|
---|
612 |
|
---|
613 | @Override
|
---|
614 | public void mouseClicked(final MouseEvent e) {
|
---|
615 | }
|
---|
616 |
|
---|
617 | @Override
|
---|
618 | public void keyTyped(final KeyEvent e) {
|
---|
619 | }
|
---|
620 |
|
---|
621 | @Override
|
---|
622 | public synchronized void keyPressed(final KeyEvent e) {
|
---|
623 | this.handleKeyboardInput(e);
|
---|
624 | }
|
---|
625 |
|
---|
626 | @Override
|
---|
627 | public void keyReleased(final KeyEvent e) {
|
---|
628 | }
|
---|
629 |
|
---|
630 | private void selectText(final Textbox text) {
|
---|
631 | if (this.selectedText != null) {
|
---|
632 | this.selectedText.setSelected(false);
|
---|
633 | }
|
---|
634 | if ((this.selectedText = text) != null) {
|
---|
635 | text.setSelected(true);
|
---|
636 | }
|
---|
637 | }
|
---|
638 |
|
---|
639 | public static void main(final String[] args) {
|
---|
640 | try {
|
---|
641 | final PrintStream st = new PrintStream(new FileOutputStream("err.txt", true));
|
---|
642 | System.setErr(st);
|
---|
643 | System.setOut(st);
|
---|
644 | System.out.println("-----[ Session started on " + Utils.dateString() + " ]-----");
|
---|
645 | final GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
---|
646 | final GraphicsDevice device = env.getDefaultScreenDevice();
|
---|
647 | new LastDefenseMain(device);
|
---|
648 | }
|
---|
649 | catch (Exception e) {
|
---|
650 | e.printStackTrace();
|
---|
651 | }
|
---|
652 | System.exit(0);
|
---|
653 | }
|
---|
654 |
|
---|
655 | public enum AuxState {
|
---|
656 | None,
|
---|
657 | MsgBox
|
---|
658 | }
|
---|
659 |
|
---|
660 | public enum GameState {
|
---|
661 | Main,
|
---|
662 | Info,
|
---|
663 | Credits,
|
---|
664 | Game
|
---|
665 | }
|
---|
666 | }
|
---|