source: lost-haven/main/LostHavenRPG.java@ 155577b

Last change on this file since 155577b was 0870468, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 5 years ago

Move all global classes into the main package

  • Property mode set to 100644
File size: 21.3 KB
Line 
1package main;
2
3import java.awt.*;
4import java.awt.image.*;
5import java.awt.event.*;
6import java.io.*;
7import java.util.*;
8import javax.imageio.*;
9import java.text.*;
10
11import gamegui.*;
12
13/*
14 * This is the main class in the project. It initializes wide-screen mode and is responsible for drawing to the screen and handling
15 * input.
16 *
17 * The classes in the gamegui folder are similar to the Swing classes in that they help in building a gui. They are all extended from
18 * the Member class and instances of each of them can be added to a Window class. They also have input-handling functionality.
19 */
20
21public class LostHavenRPG implements KeyListener, MouseListener {
22 private static DisplayMode[] BEST_DISPLAY_MODES = new DisplayMode[] {
23 new DisplayMode(800, 600, 32, 0),
24 new DisplayMode(800, 600, 16, 0),
25 new DisplayMode(800, 600, 8, 0)
26 };
27
28 boolean done;
29 boolean changePending;
30
31 Player player;
32 Map map;
33
34 //all "types" should be enums so it's easier to see all possible values while programming
35 HashMap<LandType, Land> landMap;
36 HashMap<StructureType, Structure> structMap;
37 HashMap<CreatureType, Creature> creatureMap;
38
39 BufferedImage girl;
40 BufferedImage guy;
41
42 public GameState gameState;
43 public AuxState auxState;
44
45 //GUI elements
46 Frame frmMain;
47
48 gamegui.Window wndMain;
49 gamegui.Window wndCreateAccount;
50 gamegui.Window wndChooseClass;
51 gamegui.Window wndGameInfo;
52 gamegui.Window wndCredits;
53
54 RadioGroup rdgGenderSelection;
55 RadioGroup rdgClassSelection;
56
57 gamegui.Window wndMessage;
58 gamegui.Window wndProgress;
59 gamegui.Window wndConnecting;
60
61 Textbox selectedText;
62
63 public LostHavenRPG(GraphicsDevice device) {
64 try {
65 GraphicsConfiguration gc = device.getDefaultConfiguration();
66 frmMain = new Frame(gc);
67 frmMain.setUndecorated(true);
68 frmMain.setIgnoreRepaint(true);
69 device.setFullScreenWindow(frmMain);
70
71 if (device.isDisplayChangeSupported()) {
72 chooseBestDisplayMode(device);
73 }
74
75 frmMain.addMouseListener(this);
76 frmMain.addKeyListener(this);
77 frmMain.createBufferStrategy(2);
78 BufferStrategy bufferStrategy = frmMain.getBufferStrategy();
79
80 player = new Player();
81 done = false;
82 changePending = false;
83
84 gameState = GameState.Main;
85 auxState = AuxState.None;
86
87 loadMap();
88 map = new Map("mapInfo.txt", "structInfo.txt", landMap, structMap);
89 map.getLoc(10, 10).addCreature(new Creature());
90 initGUIElements();
91
92 while (!done) {
93 Graphics g = bufferStrategy.getDrawGraphics();
94 move();
95 render(g);
96 g.dispose();
97 bufferStrategy.show();
98 }
99 }
100 catch (Exception e) {
101 e.printStackTrace();
102 }
103 finally {
104 device.setFullScreenWindow(null);
105 }
106 }
107
108 private void initGUIElements() {
109 Font font10 = new Font("Arial", Font.PLAIN, 10);
110 Font font11 = new Font("Arial", Font.PLAIN, 11);
111 Font font12 = new Font("Arial", Font.PLAIN, 12);
112 Font font14 = new Font("Arial", Font.PLAIN, 14);
113 Font font24 = new Font("Arial", Font.PLAIN, 24);
114
115 wndMain = new gamegui.Window("main", 0, 0, 800, 600, true);
116
117 Animation anmTitle = new Animation("title", 144, 0, 512, 95, 1000/12);
118
119 try {
120 anmTitle.addFrame(ImageIO.read(getClass().getResource("images/Frame1.png")));
121 anmTitle.addFrame(ImageIO.read(getClass().getResource("images/Frame2.png")));
122 anmTitle.addFrame(ImageIO.read(getClass().getResource("images/Frame3.png")));
123 anmTitle.addFrame(ImageIO.read(getClass().getResource("images/Frame4.png")));
124 anmTitle.addFrame(ImageIO.read(getClass().getResource("images/Frame5.png")));
125 }catch(IOException ioe) {
126 ioe.printStackTrace();
127 }
128 wndMain.add(anmTitle);
129
130 wndMain.add(new gamegui.Button("new game", 500, 140, 200, 40, "New Game", font12));
131 wndMain.add(new gamegui.Button("load game", 500, 230, 200, 40, "Load Game", font12));
132 wndMain.add(new gamegui.Button("game info", 500, 320, 200, 40, "Game Information", font12));
133 wndMain.add(new gamegui.Button("credits", 500, 410, 200, 40, "Credits", font12));
134 wndMain.add(new gamegui.Button("quit", 500, 500, 200, 40, "Quit", font12));
135
136 wndCreateAccount = new gamegui.Window("create account", 0, 0, 800, 600, true);
137
138 rdgGenderSelection = new RadioGroup("gender selection", 400, 315, 190, 30, "Gender:", font12);
139
140 rdgGenderSelection.add(new RadioButton("male", 438, 318, 24, 24, "Male", font11, false));
141 rdgGenderSelection.add(new RadioButton("female", 528, 318, 24, 24, "Female", font11, false));
142
143 wndCreateAccount.add(new gamegui.Label("title", 250, 15, 300, 20, "Create an Account", font24, true));
144 wndCreateAccount.add(new Textbox("user", 400, 150, 190, 30, "Username:", font12, false));
145 wndCreateAccount.add(rdgGenderSelection);
146 wndCreateAccount.add(new gamegui.Label("show class", 330, 370, 70, 30, "None", font12, false));
147 wndCreateAccount.add(new gamegui.Button("choose class", 400, 370, 190, 30, "Choose Your Class", font12));
148 wndCreateAccount.add(new gamegui.Button("create", 245, 520, 140, 30, "Create", font12));
149 wndCreateAccount.add(new gamegui.Button("cancel", 415, 520, 140, 30, "Cancel", font12));
150
151 wndChooseClass = new gamegui.Window("choose class", 0, 0, 800, 600, true);
152
153 rdgClassSelection = new RadioGroup("class selection", 0, 0, 0, 0, "", font12);
154
155 rdgClassSelection.add(new RadioButton("fighter", 138, 88, 24, 24, "Fighter", font14, true));
156 rdgClassSelection.add(new RadioButton("ranger", 138, 158, 24, 24, "Ranger", font14, true));
157 rdgClassSelection.add(new RadioButton("barbarian", 138, 228, 24, 24, "Barbarian", font14, true));
158 rdgClassSelection.add(new RadioButton("sorceror", 138, 298, 24, 24, "Sorceror", font14, true));
159 rdgClassSelection.add(new RadioButton("druid", 138, 368, 24, 24, "Druid", font14, true));
160 rdgClassSelection.add(new RadioButton("wizard", 138, 438, 24, 24, "Wizard", font14, true));
161
162 wndChooseClass.add(new gamegui.Label("title", 250, 15, 300, 20, "Choose a Character", font24, true));
163 wndChooseClass.add(rdgClassSelection);
164 wndChooseClass.add(new gamegui.Label("fighter", 170, 114, 170, 0, "A resolute and steadfast champion who has perfected the art of battle and his skill in melee weapons", font10, false));
165 wndChooseClass.add(new gamegui.Label("ranger", 170, 184, 170, 0, "A skilled combatant who sneaks up on his opponents or shoots them from afar before they know it", font10, false));
166 wndChooseClass.add(new gamegui.Label("barbarian", 170, 254, 170, 0, "A wild warrior who is unstoppable in battle and uses his own fury to strengthen his attacks", font10, false));
167 wndChooseClass.add(new gamegui.Label("sorceror", 170, 324, 170, 0, "A chaotic spellcaster who uses his charisma and force of will to power his spells", font10, false));
168 wndChooseClass.add(new gamegui.Label("druid", 170, 394, 170, 0, "A mystical enchanter who relies on the power of nature and his wisdom to work his magic", font10, false));
169 wndChooseClass.add(new gamegui.Label("wizard", 170, 464, 170, 0, "A methodical and studious character who studies his opponents to know how to best attack them", font10, false));
170 wndChooseClass.add(new gamegui.Button("select", 245, 520, 140, 30, "Select", font12));
171 wndChooseClass.add(new gamegui.Button("cancel", 415, 520, 140, 30, "Cancel", font12));
172
173 wndMessage = new gamegui.Window("message", 290, 135, 220, 160, false);
174 wndMessage.add(new gamegui.Label("label", 70, 15, 80, 12, "none", font12, true));
175 wndMessage.add(new gamegui.Button("button", 70, 115, 80, 30, "OK", font12));
176 }
177
178 private void loadMap() {
179 landMap = new HashMap<LandType, Land>();
180 structMap = new HashMap<StructureType, Structure>();
181 BufferedImage nullImg = null;
182
183 try {
184 girl = ImageIO.read(getClass().getResource("images/ArmoredGirl.png"));
185 guy = ImageIO.read(getClass().getResource("images/ArmoredGuy.png"));
186 }catch(IOException ioe) {
187 ioe.printStackTrace();
188 }
189
190 landMap.put(LandType.Ocean, new Land(LandType.Ocean, "Ocean.png", false));
191 landMap.put(LandType.Grass, new Land(LandType.Grass, "Grass.png", true));
192
193 structMap.put(StructureType.None, new Structure(StructureType.None, nullImg, true));
194 structMap.put(StructureType.BlueOrb, new Structure(StructureType.BlueOrb, "Blue Orb.png", false));
195 structMap.put(StructureType.Cave, new Structure(StructureType.Cave, "Cave.png", false));
196 structMap.put(StructureType.Gravestone, new Structure(StructureType.Gravestone, "Gravestone.png", false));
197 structMap.put(StructureType.GraveyardFence1, new Structure(StructureType.GraveyardFence1, "HorGrave.png", false));
198 structMap.put(StructureType.GraveyardFence2, new Structure(StructureType.GraveyardFence2, "VerGrave.png", false));
199 structMap.put(StructureType.PicketFence1, new Structure(StructureType.PicketFence1, "HorPalisade.png", false));
200 structMap.put(StructureType.PicketFence2, new Structure(StructureType.PicketFence2, "VerPalisade.png", false));
201 structMap.put(StructureType.Hut, new Structure(StructureType.Hut, "Hut.png", false));
202 structMap.put(StructureType.WitchHut, new Structure(StructureType.WitchHut, "Witch Hut.png", false));
203 structMap.put(StructureType.Tent, new Structure(StructureType.Tent, "Tent.png", false));
204 structMap.put(StructureType.LargeTent, new Structure(StructureType.LargeTent, "LargeTent.png", false));
205 structMap.put(StructureType.House, new Structure(StructureType.House, "House.png", false));
206 structMap.put(StructureType.Tree, new Structure(StructureType.Tree, "Trees.png", false));
207 structMap.put(StructureType.BlueOrb, new Structure(StructureType.BlueOrb, "Blue Orb.png", false));
208 structMap.put(StructureType.RedOrb, new Structure(StructureType.RedOrb, "Red Orb.png", false));
209 structMap.put(StructureType.LoginPedestal, new Structure(StructureType.LoginPedestal, "YellowPedestal.png", true));
210 structMap.put(StructureType.RejuvenationPedestal, new Structure(StructureType.RejuvenationPedestal, "PurplePedestal.png", true));
211 structMap.put(StructureType.LifePedestal, new Structure(StructureType.LifePedestal, "RedPedestal.png", true));
212 structMap.put(StructureType.ManaPedestal, new Structure(StructureType.ManaPedestal, "BluePedestal.png", true));
213 }
214
215 private void move() {
216 double dist = player.getSpeed()*(System.currentTimeMillis()-player.getLastMoved())/1000;
217 Point lastLoc = player.getLoc();
218 Point target = player.getTarget();
219 Point newLoc;
220
221 player.setLastMoved(System.currentTimeMillis());
222 if(Point.dist(lastLoc, player.getTarget()) <= dist)
223 player.setLoc(player.getTarget());
224 else {
225 int xDif = (int)(Point.xDif(lastLoc, target)*dist/Point.dist(lastLoc, target));
226 int yDif = (int)(Point.yDif(lastLoc, target)*dist/Point.dist(lastLoc, target));
227 newLoc = new Point(lastLoc.getX(), lastLoc.getXMin()+xDif, lastLoc.getY(), lastLoc.getYMin()+yDif);
228 newLoc.setX(newLoc.getX()+newLoc.getXMin()/100);
229 newLoc.setXMin(newLoc.getXMin()%100);
230 newLoc.setY(newLoc.getY()+newLoc.getYMin()/100);
231 newLoc.setYMin(newLoc.getYMin()%100);
232 if(newLoc.getXMin()<0) {
233 newLoc.setX(newLoc.getX()-1);
234 newLoc.setXMin(newLoc.getXMin()+100);
235 }else if(newLoc.getYMin()<0) {
236 newLoc.setY(newLoc.getY()-1);
237 newLoc.setYMin(newLoc.getYMin()+100);
238 }
239 if(map.getLoc(newLoc.getX()/100, newLoc.getY()/100).isPassable())
240 player.setLoc(newLoc);
241 else
242 player.setTarget(player.getLoc());
243 }
244 }
245
246 private void render(Graphics g) {
247 g.setColor(Color.black);
248 g.fillRect(0, 0, 800, 600);
249
250 switch(gameState) {
251 case Main:
252 drawMain(g);
253 break;
254 case CreateAccount:
255 drawCreateAccount(g);
256 break;
257 case CreateClass:
258 drawCreateClass(g);
259 break;
260 case LoadGame:
261 drawLoadGame(g);
262 break;
263 case Info:
264 drawInfo(g);
265 break;
266 case Credits:
267 drawCredits(g);
268 break;
269 case Game:
270 calculateMapVertices();
271 drawMap(g);
272 drawItems(g);
273 drawCreatures(g);
274 drawChar(g);
275 drawStatDisplay(g);
276 drawChat(g);
277 break;
278 case GameMenu:
279 calculateMapVertices();
280 drawMap(g);
281 drawItems(g);
282 drawCreatures(g);
283 drawChar(g);
284 drawStatDisplay(g);
285 drawChat(g);
286 drawGameMenu(g);
287 break;
288 case GameInventory:
289 calculateMapVertices();
290 drawMap(g);
291 drawItems(g);
292 drawCreatures(g);
293 drawChar(g);
294 drawStatDisplay(g);
295 drawChat(g);
296 drawGameInventory(g);
297 break;
298 case GameStats:
299 calculateMapVertices();
300 drawMap(g);
301 drawItems(g);
302 drawCreatures(g);
303 drawChar(g);
304 drawStatDisplay(g);
305 drawChat(g);
306 drawGameStats(g);
307 break;
308 }
309
310 switch(auxState) {
311 case None:
312 break;
313 case MsgBox:
314 wndMessage.draw(g);
315 break;
316 }
317 }
318
319 public static String dateString() {
320 return new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new Date());
321 }
322
323 public void showMessage(String text) {
324 auxState = AuxState.MsgBox;
325 ((gamegui.Label)wndMessage.getMember("label")).setText(text);
326 }
327
328 private void calculateMapVertices() {
329
330 }
331
332 private void drawMain(Graphics g) {
333 wndMain.draw(g);
334
335 g.setColor(Color.red);
336 g.drawRect(10, 100, 380, 490);
337 g.drawRect(410, 100, 380, 490);
338 }
339
340 private void drawCreateAccount(Graphics g) {
341 wndCreateAccount.draw(g);
342 }
343
344 private void drawCreateClass(Graphics g) {
345 wndChooseClass.draw(g);
346 }
347
348 private void drawLoadGame(Graphics g) {
349 Font tempFont = new Font("Arial", Font.PLAIN, 12);
350 FontMetrics metrics = g.getFontMetrics(tempFont);
351
352 g.setFont(tempFont);
353 g.setColor(Color.green);
354 g.drawString("There is not a whole lot here right now. You can click anywhere on the screen to get back to the main menu.", 0, metrics.getHeight());
355 }
356
357 private void drawInfo(Graphics g) {
358 Font tempFont = new Font("Arial", Font.PLAIN, 12);
359 FontMetrics metrics = g.getFontMetrics(tempFont);
360
361 g.setFont(tempFont);
362 g.setColor(Color.green);
363 g.drawString("There is not a whole lot here right now. You can click anywhere on the screen to get back to the main menu.", 0, metrics.getHeight());
364 }
365
366 private void drawCredits(Graphics g) {
367 Font tempFont = new Font("Arial", Font.PLAIN, 12);
368 FontMetrics metrics = g.getFontMetrics(tempFont);
369
370 g.setFont(tempFont);
371 g.setColor(Color.green);
372 g.drawString("There is not a whole lot here right now. You can click anywhere on the screen to get back to the main menu.", 0, metrics.getHeight());
373 }
374
375 private void drawMap(Graphics g) {
376 int locX = player.getLoc().getX();
377 int locY = player.getLoc().getY();
378 int xLow = locX/100-4;
379 int xHigh = xLow+9;
380 int yLow = locY/100-3;
381 int yHigh = yLow+7;
382
383 if(xLow<0)
384 xLow = 0;
385 if(xHigh>=map.getLength())
386 xHigh = map.getLength()-1;
387 if(yLow<0)
388 yLow = 0;
389 if(yHigh>=map.getHeight())
390 yHigh = map.getHeight()-1;
391
392 for(int x=xLow; x<xHigh; x++) {
393 for(int y=yLow; y<yHigh; y++) {
394 g.drawImage(map.getLoc(x, y).getLand().getImg(), 400+x*100-locX, 300+y*100-locY, null);
395 g.drawImage(map.getLoc(x, y).getStruct().getImg(), 400+x*100-locX, 300+y*100-locY, null);
396 }
397 }
398 }
399
400 private void drawItems(Graphics g) {
401
402 }
403
404 private void drawCreatures(Graphics g) {
405
406 }
407
408 private void drawChar(Graphics g) {
409 switch(player.getGender()) {
410 case Female:
411 g.drawImage(girl, 375, 200, null);
412 break;
413 case Male:
414 g.drawImage(guy, 375, 200, null);
415 break;
416 }
417 }
418
419 private void drawStatDisplay(Graphics g) {
420
421 }
422
423 private void drawChat(Graphics g) {
424
425 }
426
427 private void drawGameMenu(Graphics g) {
428
429 }
430
431 private void drawGameInventory(Graphics g) {
432
433 }
434
435 private void drawGameStats(Graphics g) {
436
437 }
438
439 private void selectText(Textbox text) {
440 if(selectedText != null)
441 selectedText.setSelected(false);
442 selectedText = text;
443
444 if(text != null)
445 text.setSelected(true);
446 }
447
448 private static DisplayMode getBestDisplayMode(GraphicsDevice device) {
449 for (int x = 0; x < BEST_DISPLAY_MODES.length; x++) {
450 DisplayMode[] modes = device.getDisplayModes();
451 for (int i = 0; i < modes.length; i++) {
452 if (modes[i].getWidth() == BEST_DISPLAY_MODES[x].getWidth()
453 && modes[i].getHeight() == BEST_DISPLAY_MODES[x].getHeight()
454 && modes[i].getBitDepth() == BEST_DISPLAY_MODES[x].getBitDepth())
455 {
456 return BEST_DISPLAY_MODES[x];
457 }
458 }
459 }
460 return null;
461 }
462
463 public static void chooseBestDisplayMode(GraphicsDevice device) {
464 DisplayMode best = getBestDisplayMode(device);
465 if (best != null) {
466 device.setDisplayMode(best);
467 }
468 }
469
470 public void mousePressed(MouseEvent e) {
471 switch(auxState) {
472 case None:
473 switch(gameState) {
474 case Main:
475 if(wndMain.getMember("new game").isClicked(e.getX(),e.getY()))
476 gameState = GameState.CreateAccount;
477 else if(wndMain.getMember("load game").isClicked(e.getX(),e.getY()))
478 gameState = GameState.LoadGame;
479 else if(wndMain.getMember("game info").isClicked(e.getX(),e.getY()))
480 gameState = GameState.Info;
481 else if(wndMain.getMember("credits").isClicked(e.getX(),e.getY()))
482 gameState = GameState.Credits;
483 else if(wndMain.getMember("quit").isClicked(e.getX(),e.getY()))
484 done = true;
485 break;
486 case CreateAccount:
487 if(wndCreateAccount.getMember("user").isClicked(e.getX(),e.getY()))
488 selectText((Textbox)wndCreateAccount.getMember("user"));
489 else if(wndCreateAccount.getMember("choose class").isClicked(e.getX(),e.getY())) {
490 selectText(null);
491 gameState = GameState.CreateClass;
492 }else if(wndCreateAccount.getMember("create").isClicked(e.getX(),e.getY())) {
493 String user = ((Textbox)wndCreateAccount.getMember("user")).getText();
494 Gender gender = Gender.valueOf(rdgGenderSelection.getButton(rdgGenderSelection.getSelected()).getLabel());
495
496 if(user.equals("")) {
497 showMessage("The username is empty");
498 }else if(gender == Gender.None) {
499 showMessage("No gender has been selected");
500 }else{
501 player = new Player(user, gender);
502 player.setSpeed(200);
503 player.setLoc(new Point(750, 860));
504 player.setTarget(player.getLoc());
505 gameState = GameState.Game;
506 }
507 }else if(wndCreateAccount.getMember("cancel").isClicked(e.getX(),e.getY())) {
508 selectText(null);
509 wndCreateAccount.clear();
510 wndChooseClass.clear();
511 gameState = GameState.Main;
512 }else if(wndCreateAccount.handleEvent(e)) {
513 }
514 break;
515 case CreateClass:
516 if(wndChooseClass.getMember("select").isClicked(e.getX(),e.getY())) {
517 gameState = GameState.CreateAccount;
518 }
519 else if(wndChooseClass.getMember("cancel").isClicked(e.getX(),e.getY())) {
520 gameState = GameState.CreateAccount;
521 }else if(wndChooseClass.handleEvent(e)) {
522 }
523 break;
524 case LoadGame:
525 gameState = GameState.Main;
526 break;
527 case Info:
528 gameState = GameState.Main;
529 break;
530 case Credits:
531 gameState = GameState.Main;
532 break;
533 case Game:
534 int newX = player.getLoc().getX()+e.getX()-400;
535 int newY = player.getLoc().getY()+e.getY()-300;
536 if(map.getLoc((int)(Math.floor(newX/100)), (int)(Math.floor(newY/100))).isPassable()) {
537 player.setTarget(new Point(newX, newY));
538 player.setLastMoved(System.currentTimeMillis());
539 }
540 break;
541 case GameMenu:
542 break;
543 case GameInventory:
544 break;
545 case GameStats:
546 break;
547 }
548 break;
549 case MsgBox:
550 if(wndMessage.getMember("button").isClicked(e.getX(), e.getY())) {
551 auxState = AuxState.None;
552 }
553 break;
554 }
555 }
556
557 public void mouseReleased(MouseEvent e) {
558
559 }
560
561 public void mouseEntered(MouseEvent e) {
562
563 }
564
565 public void mouseExited(MouseEvent e) {
566
567 }
568
569 public void mouseClicked(MouseEvent e) {
570
571 }
572
573 public void keyTyped(KeyEvent e) {
574
575 }
576
577 public void keyPressed(KeyEvent e) {
578 if(selectedText != null)
579 selectedText.handleEvent(e);
580
581 if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
582 if(gameState == GameState.Game)
583 gameState = GameState.Main;
584 else
585 done = true;
586 }
587
588 public void keyReleased(KeyEvent e) {
589
590 }
591
592 public static void main(String[] args) {
593 try {
594 PrintStream st = new PrintStream(new FileOutputStream("err.txt", true));
595 System.setErr(st);
596 System.setOut(st);
597 System.out.println("-----[ Session started on " + dateString() + " ]-----");
598
599 GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
600 GraphicsDevice device = env.getDefaultScreenDevice();
601 new LostHavenRPG(device);
602 }
603 catch (Exception e) {
604 e.printStackTrace();
605 }
606 System.exit(0);
607 }
608}
Note: See TracBrowser for help on using the repository browser.