source: lost-haven/LostHavenRPG.java@ a5b4186

Last change on this file since a5b4186 was a5b4186, checked in by dportnoy <dmp1488@…>, 18 years ago

[svn r36] Renamed remotely

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