source: java-rpg-client/LostHavenClient.java

Last change on this file was 92d1b2a, checked in by dportnoy <dmp1488@…>, 17 years ago

[svn r27]

  • Property mode set to 100644
File size: 37.0 KB
Line 
1import java.awt.*;
2import java.awt.image.*;
3import java.awt.event.*;
4import java.io.*;
5import java.util.*;
6
7import javax.imageio.*;
8
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 LostHavenClient 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 HashMap<LandType, Land> landMap;
35 HashMap<StructureType, Structure> structMap;
36 BufferedImage girl;
37 BufferedImage guy;
38
39 public GameState gameState;
40 public AuxState auxState;
41
42 //GUI elements
43 Frame frmMain;
44
45 gamegui.Window wndMain;
46 gamegui.Window wndLogin;
47 gamegui.Window wndCreateAccount;
48 gamegui.Window wndChooseClass;
49 gamegui.Window wndTavern;
50
51 TabbedWindow wndTabbed1;
52 TabbedWindow wndTabbed2;
53
54 gamegui.Window wndUpdates;
55 gamegui.Window wndChat;
56 gamegui.Window wndTutorial;
57 gamegui.Window wndRegistered;
58 gamegui.Window wndOnline;
59 gamegui.Window wndSearch;
60
61 gamegui.Menu mnuChannels;
62 MultiTextbox mtxChat;
63
64 ScrollList lstRegistered;
65 ScrollList lstOnline;
66
67 RadioGroup rdgGenderSelection;
68 RadioGroup rdgClassSelection;
69
70 gamegui.Window wndMessage;
71 gamegui.Window wndProgress;
72 gamegui.Window wndConnecting;
73
74 Textbox selectedText;
75
76 int frameCount;
77 long startTime;
78
79 //networking elements
80 ClientThread client;
81 HashMap<String, Player> registered;
82 PriorityQueue<Player> orderedReg;
83 PriorityQueue<Player> orderedOnline;
84
85 public LostHavenClient(GraphicsDevice device) {
86 try {
87 GraphicsConfiguration gc = device.getDefaultConfiguration();
88 frmMain = new Frame(gc);
89 frmMain.setUndecorated(true);
90 frmMain.setIgnoreRepaint(true);
91 device.setFullScreenWindow(frmMain);
92
93 if (device.isDisplayChangeSupported()) {
94 chooseBestDisplayMode(device);
95 }
96
97 frmMain.addMouseListener(this);
98 frmMain.addKeyListener(this);
99 frmMain.createBufferStrategy(2);
100 BufferStrategy bufferStrategy = frmMain.getBufferStrategy();
101
102 done = false;
103 changePending = false;
104 player = new Player();
105
106 gameState = GameState.Main;
107 auxState = AuxState.None;
108
109 registered = new HashMap<String, Player>();
110 orderedReg = new PriorityQueue<Player>();
111 orderedOnline = new PriorityQueue<Player>();
112
113 initGUIElements();
114
115 while (!done) {
116 Graphics g = bufferStrategy.getDrawGraphics();
117 render(g);
118 g.dispose();
119 bufferStrategy.show();
120 frameCount++;
121 }
122 if(client != null)
123 client.closeConnection();
124 }
125 catch (Exception e) {
126 e.printStackTrace();
127 }
128 finally {
129 device.setFullScreenWindow(null);
130 }
131 }
132
133 private void initGUIElements() {
134 Font font10 = new Font("Arial", Font.PLAIN, 10);
135 Font font11 = new Font("Arial", Font.PLAIN, 11);
136 Font font12 = new Font("Arial", Font.PLAIN, 12);
137 Font font14 = new Font("Arial", Font.PLAIN, 14);
138 Font font24 = new Font("Arial", Font.PLAIN, 24);
139
140 loadMap();
141
142 wndMain = new gamegui.Window("main", 0, 0, 800, 600, true);
143
144 Animation anmTitle = new Animation("title", 144, 0, 512, 95, 1000/12);
145
146 try {
147 anmTitle.addFrame(ImageIO.read(getClass().getResource("images/Frame1.png")));
148 anmTitle.addFrame(ImageIO.read(getClass().getResource("images/Frame2.png")));
149 anmTitle.addFrame(ImageIO.read(getClass().getResource("images/Frame3.png")));
150 anmTitle.addFrame(ImageIO.read(getClass().getResource("images/Frame4.png")));
151 anmTitle.addFrame(ImageIO.read(getClass().getResource("images/Frame5.png")));
152 }catch(IOException ioe) {
153 ioe.printStackTrace();
154 }
155 wndMain.add(anmTitle);
156
157 wndMain.add(new gamegui.Button("login", 500, 140, 200, 40, "Log In", font12));
158 wndMain.add(new gamegui.Button("create account", 500, 200, 200, 40, "Create an Account", font12));
159 wndMain.add(new gamegui.Button("hall of fame", 500, 260, 200, 40, "Hall of Fame", font12));
160 wndMain.add(new gamegui.Button("updates", 500, 320, 200, 40, "Updates", font12));
161 wndMain.add(new gamegui.Button("game info", 500, 380, 200, 40, "Game Information", font12));
162 wndMain.add(new gamegui.Button("rules", 500, 440, 200, 40, "Rules and Guidelines", font12));
163 wndMain.add(new gamegui.Button("quit", 500, 500, 200, 40, "Quit", font12));
164
165 wndLogin = new gamegui.Window("login", 250, 150, 300, 200);
166
167 wndLogin.add(new Textbox("user", 90, 40, 190, 30, "Username:", font12, false));
168 wndLogin.add(new Textbox("pass", 90, 90, 190, 30, "Password:", font12, true));
169 wndLogin.add(new gamegui.Button("login", 20, 160, 120, 30, "Log In", font12));
170 wndLogin.add(new gamegui.Button("cancel", 160, 160, 120, 30, "Cancel", font12));
171
172 wndCreateAccount = new gamegui.Window("create account", 0, 0, 800, 600, true);
173
174 rdgGenderSelection = new RadioGroup("gender selection", 400, 315, 190, 30, "Gender:", font12);
175
176 rdgGenderSelection.add(new RadioButton("male", 438, 318, 24, 24, "Male", font11, false));
177 rdgGenderSelection.add(new RadioButton("female", 528, 318, 24, 24, "Female", font11, false));
178
179 wndCreateAccount.add(new gamegui.Label("title", 250, 15, 300, 20, "Create an Account", font24, true));
180 wndCreateAccount.add(new Textbox("user", 400, 150, 190, 30, "Username:", font12, false));
181 wndCreateAccount.add(new Textbox("pass", 400, 205, 190, 30, "Password:", font12, true));
182 wndCreateAccount.add(new Textbox("confirm pass", 400, 260, 190, 30, "Confirm Password:", font12, true));
183 wndCreateAccount.add(rdgGenderSelection);
184 wndCreateAccount.add(new gamegui.Label("show class", 330, 370, 70, 30, "None", font12, false));
185 wndCreateAccount.add(new gamegui.Button("choose class", 400, 370, 190, 30, "Choose Your Class", font12));
186 wndCreateAccount.add(new gamegui.Button("create", 245, 520, 140, 30, "Create", font12));
187 wndCreateAccount.add(new gamegui.Button("cancel", 415, 520, 140, 30, "Cancel", font12));
188
189 wndChooseClass = new gamegui.Window("choose class", 0, 0, 800, 600, true);
190
191 rdgClassSelection = new RadioGroup("class selection", 0, 0, 0, 0, "", font12);
192
193 rdgClassSelection.add(new RadioButton("fighter", 138, 88, 24, 24, "Fighter", font14, true));
194 rdgClassSelection.add(new RadioButton("ranger", 138, 158, 24, 24, "Ranger", font14, true));
195 rdgClassSelection.add(new RadioButton("barbarian", 138, 228, 24, 24, "Barbarian", font14, true));
196 rdgClassSelection.add(new RadioButton("sorceror", 138, 298, 24, 24, "Sorceror", font14, true));
197 rdgClassSelection.add(new RadioButton("druid", 138, 368, 24, 24, "Druid", font14, true));
198 rdgClassSelection.add(new RadioButton("wizard", 138, 438, 24, 24, "Wizard", font14, true));
199
200 wndChooseClass.add(new gamegui.Label("title", 250, 15, 300, 20, "Choose a Character", font24, true));
201 wndChooseClass.add(rdgClassSelection);
202 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));
203 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));
204 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));
205 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));
206 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));
207 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));
208 wndChooseClass.add(new gamegui.Button("select", 245, 520, 140, 30, "Select", font12));
209 wndChooseClass.add(new gamegui.Button("cancel", 415, 520, 140, 30, "Cancel", font12));
210
211 wndTavern = new gamegui.Window("tavern", 0, 0, 800, 600, true);
212
213 wndTabbed1 = new TabbedWindow("tabbed1", 10, 100, 380, 490, 40, font12);
214 wndTabbed2 = new TabbedWindow("tabbed2", 410, 100, 380, 380, 40, font12);
215
216 wndUpdates = new gamegui.Window("updates", 0, 0, 380, 450, true);
217
218 wndChat = new gamegui.Window("chat", 0, 0, 380, 450, true);
219 wndChat.add(new Textbox("chat", 70, 420, 160, 20, "", font12, false));
220 wndChat.add(new gamegui.Button("sendchat", 240, 420, 50, 20, "Send", font12));
221 mtxChat = new MultiTextbox("multichat", 10, 40, 340, 370, "", false, font12, frmMain.getBufferStrategy().getDrawGraphics().getFontMetrics(font12));
222 mtxChat.addScrollBar(new ScrollBar("scrollchat", 340, 0, 20, 370, 3));
223 wndChat.add(mtxChat);
224 mnuChannels = new gamegui.Menu("channels", 110, 10, 160, 20, "Channel", font12);
225 mnuChannels.add("None");
226 mnuChannels.add("Normal");
227 mnuChannels.add("Dev");
228 mnuChannels.add("Mod");
229 mnuChannels.add("Clan");
230 wndChat.add(mnuChannels);
231
232 wndTutorial = new gamegui.Window("tutorial", 0, 0, 380, 450, true);
233
234 wndRegistered = new gamegui.Window("registered", 0, 0, 380, 340, true);
235
236 lstRegistered = new ScrollList("registered list", 10, 25, 340, 305, font12, frmMain.getBufferStrategy().getDrawGraphics().getFontMetrics(font12));
237 lstRegistered.addScrollBar(new ScrollBar("scrollreg", 340, 0, 20, 305, 3));
238 for(int x=0; x<orderedReg.size(); x++)
239 lstRegistered.getList().add(new Registered((Player)orderedReg.toArray()[x]));
240 wndRegistered.add(new gamegui.Label("id", 30, 20, 0, 0, "ID", font12, false, true));
241 wndRegistered.add(new gamegui.Label("name", 110, 20, 0, 0, "Name", font12, false, true));
242 wndRegistered.add(new gamegui.Label("status", 250, 20, 0, 0, "Status", font12, false, true));
243 wndRegistered.add(lstRegistered);
244
245 wndOnline = new gamegui.Window("online", 0, 0, 380, 340, true);
246
247 lstOnline = new ScrollList("online list", 10, 25, 340, 305, font12, frmMain.getBufferStrategy().getDrawGraphics().getFontMetrics(font12));
248 lstOnline.addScrollBar(new ScrollBar("scrollonline", 340, 0, 20, 305, 3));
249 for(int x=0; x<orderedOnline.size(); x++)
250 lstOnline.getList().add(new Online((Player)orderedOnline.toArray()[x]));
251 wndOnline.add(new gamegui.Label("id", 30, 20, 0, 0, "ID", font12, false, true));
252 wndOnline.add(new gamegui.Label("name", 110, 20, 0, 0, "Name", font12, false, true));
253 wndOnline.add(lstOnline);
254
255 wndSearch = new gamegui.Window("search", 0, 0, 380, 340, true);
256
257 wndTabbed1.add(wndUpdates, "Updates");
258 wndTabbed1.add(wndChat, "Chat");
259 wndTabbed1.add(wndTutorial, "Tutorial");
260 wndTabbed2.add(wndRegistered, "Registered");
261 wndTabbed2.add(wndOnline, "Online");
262 wndTabbed2.add(wndSearch, "Search");
263
264 wndTavern.add(wndTabbed1);
265 wndTavern.add(wndTabbed2);
266
267 wndTavern.add(new gamegui.Label("online", 409, 495, 0, 12, "# of players online: 0", font12, false));
268 wndTavern.add(new gamegui.Label("registered", 409, 510, 0, 12, "# of players registered: 0", font12, false));
269 wndTavern.add(new gamegui.Button("play", 440, 540, 120, 30, "Play", font12));
270 wndTavern.add(new gamegui.Button("logout", 640, 540, 120, 30, "Logout", font12));
271
272 wndMessage = new gamegui.Window("message", 290, 135, 220, 160, false);
273 wndMessage.add(new gamegui.Label("label", 70, 15, 80, 12, "none", font12, true));
274 wndMessage.add(new gamegui.Button("button", 70, 115, 80, 30, "OK", font12));
275
276 wndProgress = new gamegui.Window("message", 290, 135, 220, 160, false);
277 wndProgress.add(new gamegui.Label("label", 70, 15, 80, 12, "Loading...", font12, true));
278 wndProgress.add(new ProgressBar("bar", 15, 40, 190, 30));
279 wndProgress.add(new gamegui.Button("button", 70, 115, 80, 30, "Cancel", font12));
280
281 wndConnecting = new gamegui.Window("connecting", 290, 135, 220, 160, false);
282 wndConnecting.add(new gamegui.Label("label", 70, 15, 80, 12, "Connecting...", font12, true));
283 wndConnecting.add(new gamegui.Button("button", 70, 115, 80, 30, "Cancel", font12));
284 }
285
286 private void loadMap() {
287 landMap = new HashMap<LandType, Land>();
288 structMap = new HashMap<StructureType, Structure>();
289 BufferedImage nullImg = null;
290
291 try {
292 girl = ImageIO.read(getClass().getResource("images/ArmoredGirl.png"));
293 guy = ImageIO.read(getClass().getResource("images/ArmoredGuy.png"));
294 }catch(IOException ioe) {
295 ioe.printStackTrace();
296 }
297
298 landMap.put(LandType.Ocean, new Land(LandType.Ocean, "Ocean.png", false));
299 landMap.put(LandType.Grass, new Land(LandType.Grass, "Grass.png", true));
300
301 structMap.put(StructureType.None, new Structure(StructureType.None, nullImg, false));
302 structMap.put(StructureType.BlueOrb, new Structure(StructureType.BlueOrb, "Blue Orb.png", false));
303 structMap.put(StructureType.Cave, new Structure(StructureType.Cave, "Cave.png", false));
304 structMap.put(StructureType.Gravestone, new Structure(StructureType.Gravestone, "Gravestone.png", false));
305 structMap.put(StructureType.GraveyardFence1, new Structure(StructureType.GraveyardFence1, "HorGrave.png", false));
306 structMap.put(StructureType.GraveyardFence2, new Structure(StructureType.GraveyardFence2, "VerGrave.png", false));
307 structMap.put(StructureType.PicketFence1, new Structure(StructureType.PicketFence1, "HorPalisade.png", false));
308 structMap.put(StructureType.PicketFence2, new Structure(StructureType.PicketFence2, "VerPalisade.png", false));
309 structMap.put(StructureType.Hut, new Structure(StructureType.Hut, "Hut.png", false));
310 structMap.put(StructureType.WitchHut, new Structure(StructureType.WitchHut, "Witch Hut.png", false));
311 structMap.put(StructureType.Tent, new Structure(StructureType.Tent, "Tent.png", false));
312 structMap.put(StructureType.LargeTent, new Structure(StructureType.LargeTent, "LargeTent.png", false));
313 structMap.put(StructureType.House, new Structure(StructureType.House, "House.png", false));
314 structMap.put(StructureType.Tree, new Structure(StructureType.Tree, "Trees.png", false));
315 structMap.put(StructureType.BlueOrb, new Structure(StructureType.BlueOrb, "Blue Orb.png", false));
316 structMap.put(StructureType.RedOrb, new Structure(StructureType.RedOrb, "Red Orb.png", false));
317 structMap.put(StructureType.LoginPedestal, new Structure(StructureType.LoginPedestal, "YellowPedestal.png", true));
318 structMap.put(StructureType.RejuvenationPedestal, new Structure(StructureType.RejuvenationPedestal, "PurplePedestal.png", true));
319 structMap.put(StructureType.LifePedestal, new Structure(StructureType.LifePedestal, "RedPedestal.png", true));
320 structMap.put(StructureType.ManaPedestal, new Structure(StructureType.ManaPedestal, "BluePedestal.png", true));
321 }
322
323 public void processMessage(MessageType type, String input) {
324 Player p;
325 int id;
326 String name;
327 boolean online;
328 ProgressBar bar;
329
330 switch(type) {
331 case Chat:
332 mtxChat.append(input);
333 break;
334 case Channel:
335 changePending = false;
336 player.setChannel(mnuChannels.getSelected());
337 mtxChat.append("Switched to channel " + mnuChannels.getSelected());
338 break;
339 case Login:
340 if(input.equals("Login successful")) {
341 gameState = GameState.GameTavern;
342 auxState = AuxState.None;
343 wndLogin.clear();
344 }else {
345 showMessage(input);
346 client.closeConnection();
347 }
348 break;
349 case Create:
350 showMessage(input);
351 client.closeConnection();
352 break;
353 case LoadStart:
354 auxState = AuxState.Loading;
355 bar = ((ProgressBar)wndProgress.getMember("bar"));
356 bar.setMax(Integer.valueOf(input));
357 bar.setCurrent(1);
358 break;
359 case LoadEnd:
360 bar = ((ProgressBar)wndProgress.getMember("bar"));
361 bar.setCurrent(bar.getCurrent()+1);
362 auxState = AuxState.None;
363 break;
364 case LoadMap:
365 if(map == null) {
366 int x = Integer.valueOf(input.substring(0, input.indexOf("x")));
367 int y = Integer.valueOf(input.substring(input.indexOf("x")+1));
368 map = new Map(x, y);
369 }else {
370 int x = Integer.valueOf(input.substring(0, input.indexOf(",")));
371 input = input.substring(input.indexOf(",")+1);
372 int y = Integer.valueOf(input.substring(0, input.indexOf(" ")));
373 input = input.substring(input.indexOf(" ")+1);
374 LandType landType = LandType.valueOf(input.substring(0, input.indexOf(" ")));
375 input = input.substring(input.indexOf(",")+1);
376 StructureType structType = StructureType.valueOf(input.substring(input.indexOf(" ")+1));
377
378 map.setLoc(new Location(landMap.get(landType), structMap.get(structType)), x, y);
379 }
380 bar = ((ProgressBar)wndProgress.getMember("bar"));
381 bar.setCurrent(bar.getCurrent()+1);
382 break;
383 case Registered:
384 id = Integer.parseInt(input.substring(0, input.indexOf(";")));
385 input = input.substring(input.indexOf(";")+1);
386 name = input.substring(0, input.indexOf(";"));
387 input = input.substring(input.indexOf(";")+1);
388 online = input.equals("true");
389
390 p = new Player(id, name, "");
391
392 if(!registered.containsKey(name)) {
393 registered.put(name, p);
394 orderedReg.add(p);
395 lstRegistered.getList().clear();
396 for(int x=0; x<orderedReg.size(); x++)
397 lstRegistered.getList().add(new Registered((Player)orderedReg.toArray()[x]));
398 if(online) {
399 orderedOnline.add(p);
400 lstOnline.getList().clear();
401 for(int x=0; x<orderedOnline.size(); x++)
402 lstOnline.getList().add(new Online((Player)orderedOnline.toArray()[x]));
403 }
404 }
405 break;
406 case PlayerJoined:
407 input = input.substring(input.indexOf(" ")+1);
408 name = input.substring(0, input.indexOf(" "));
409
410 if(!orderedOnline.contains(registered.get(name))) {
411 orderedOnline.add(registered.get(name));
412 lstOnline.getList().clear();
413 for(int x=0; x<orderedOnline.size(); x++)
414 lstOnline.getList().add(new Online((Player)orderedOnline.toArray()[x]));
415 }
416 break;
417 case PlayerLeft:
418 input = input.substring(input.indexOf(" ")+1);
419 name = input.substring(0, input.indexOf(" "));
420
421 if(orderedOnline.contains(registered.get(name))) {
422 orderedOnline.remove(registered.get(name));
423 lstOnline.getList().clear();
424 for(int x=0; x<orderedOnline.size(); x++)
425 lstOnline.getList().add(new Online((Player)orderedOnline.toArray()[x]));
426 }
427 break;
428 }
429 }
430
431 private void render(Graphics g) {
432 g.setColor(Color.black);
433 g.fillRect(0, 0, 800, 600);
434
435 switch(gameState) {
436 case Main:
437 drawMain(g);
438 break;
439 case Login:
440 drawMain(g);
441 drawLogin(g);
442 break;
443 case CreateAccount:
444 drawCreateAccount(g);
445 break;
446 case CreateClass:
447 drawCreateClass(g);
448 break;
449 case Records:
450 drawRecords(g);
451 break;
452 case Updates:
453 drawUpdates(g);
454 break;
455 case Info:
456 drawInfo(g);
457 break;
458 case Rules:
459 drawRules(g);
460 break;
461 case Game:
462 calculateMapVertices();
463 drawMap(g);
464 drawItems(g);
465 drawCreatures(g);
466 drawChars(g);
467 drawStatDisplay(g);
468 drawChat(g);
469 break;
470 case GameUpgrade:
471 drawGameUpgrade(g);
472 break;
473 case GameTavern:
474 drawTavernMenu(g);
475 break;
476 case GameMenu:
477 calculateMapVertices();
478 drawMap(g);
479 drawItems(g);
480 drawCreatures(g);
481 drawChars(g);
482 drawStatDisplay(g);
483 drawChat(g);
484 drawGameMenu(g);
485 break;
486 case GameInventory:
487 calculateMapVertices();
488 drawMap(g);
489 drawItems(g);
490 drawCreatures(g);
491 drawChars(g);
492 drawStatDisplay(g);
493 drawChat(g);
494 drawGameInventory(g);
495 break;
496 case GameStats:
497 calculateMapVertices();
498 drawMap(g);
499 drawItems(g);
500 drawCreatures(g);
501 drawChars(g);
502 drawStatDisplay(g);
503 drawChat(g);
504 drawGameStats(g);
505 break;
506 }
507
508 switch(auxState) {
509 case None:
510 break;
511 case MsgBox:
512 wndMessage.draw(g);
513 break;
514 case Connecting:
515 wndConnecting.draw(g);
516 break;
517 case Loading:
518 wndProgress.draw(g);
519 break;
520 }
521 }
522
523 public static String dateString() {
524 return new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new Date());
525 }
526
527 public void showMessage(String text) {
528 auxState = AuxState.MsgBox;
529 ((gamegui.Label)wndMessage.getMember("label")).setText(text);
530 }
531
532 private void calculateMapVertices() {
533
534 }
535
536 private void drawMain(Graphics g) {
537 wndMain.draw(g);
538
539 g.setColor(Color.red);
540 g.drawRect(10, 100, 380, 490);
541 g.drawRect(410, 100, 380, 490);
542 }
543
544 private void drawLogin(Graphics g) {
545 wndLogin.draw(g);
546 }
547
548 private void drawCreateAccount(Graphics g) {
549 ((gamegui.Label)wndCreateAccount.getMember("show class")).setText(player.getJob().toString());
550
551 wndCreateAccount.draw(g);
552 }
553
554 private void drawCreateClass(Graphics g) {
555 wndChooseClass.draw(g);
556 }
557
558 private void drawRecords(Graphics g) {
559 Font tempFont = new Font("Arial", Font.PLAIN, 12);
560 FontMetrics metrics = g.getFontMetrics(tempFont);
561
562 g.setFont(tempFont);
563 g.setColor(Color.green);
564 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());
565 }
566
567 private void drawUpdates(Graphics g) {
568 Font tempFont = new Font("Arial", Font.PLAIN, 12);
569 FontMetrics metrics = g.getFontMetrics(tempFont);
570
571 g.setFont(tempFont);
572 g.setColor(Color.green);
573 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());
574 }
575
576 private void drawInfo(Graphics g) {
577 Font tempFont = new Font("Arial", Font.PLAIN, 12);
578 FontMetrics metrics = g.getFontMetrics(tempFont);
579
580 g.setFont(tempFont);
581 g.setColor(Color.green);
582 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());
583 }
584
585 private void drawRules(Graphics g) {
586 Font tempFont = new Font("Arial", Font.PLAIN, 12);
587 FontMetrics metrics = g.getFontMetrics(tempFont);
588
589 g.setFont(tempFont);
590 g.setColor(Color.green);
591 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());
592 }
593
594 private void drawMap(Graphics g) {
595 int locX = player.getLoc().getX();
596 int locY = player.getLoc().getY();
597 int xLow = locX/100-4;
598 int xHigh = xLow+9;
599 int yLow = locY/100-3;
600 int yHigh = yLow+7;
601
602 if(startTime == 0) {
603 startTime = System.currentTimeMillis();
604 frameCount = 0;
605 }
606
607 if(xLow<0)
608 xLow = 0;
609 if(xHigh>=map.getLength())
610 xHigh = map.getLength()-1;
611 if(yLow<0)
612 yLow = 0;
613 if(yHigh>=map.getHeight())
614 yHigh = map.getHeight()-1;
615
616 for(int x=xLow; x<xHigh; x++) {
617 for(int y=yLow; y<yHigh; y++) {
618 g.drawImage(map.getLoc(x, y).getLand().getImg(), 400+x*100-locX, 300+y*100-locY, null);
619 g.drawImage(map.getLoc(x, y).getStruct().getImg(), 400+x*100-locX, 300+y*100-locY, null);
620 }
621 }
622
623 Iterator<Player> iter = orderedOnline.iterator();
624 Player p;
625
626 while(iter.hasNext()) {
627 p = iter.next();
628 switch(p.getGender()) {
629 case Female:
630 g.drawImage(girl, 375+p.getLoc().getX()-player.getLoc().getX(), 200+p.getLoc().getY()-player.getLoc().getY(), null);
631 break;
632 case Male:
633 g.drawImage(guy, 375+p.getLoc().getX()-player.getLoc().getX(), 200+p.getLoc().getY()-player.getLoc().getY(), null);
634 break;
635 }
636 }
637
638 g.drawString(Long.toString(1000*frameCount/(System.currentTimeMillis()-startTime)), 0, 15);
639 }
640
641 private void drawItems(Graphics g) {
642
643 }
644
645 private void drawCreatures(Graphics g) {
646
647 }
648
649 private void drawChars(Graphics g) {
650
651 }
652
653 private void drawStatDisplay(Graphics g) {
654
655 }
656
657 private void drawChat(Graphics g) {
658
659 }
660
661 private void drawGameUpgrade(Graphics g) {
662
663 }
664
665 private void drawTavernMenu(Graphics g) {
666 ((gamegui.Label)wndTavern.getMember("online")).setText("# of players online: " + String.valueOf(orderedOnline.size()));
667 ((gamegui.Label)wndTavern.getMember("registered")).setText("# of players registered: " + String.valueOf(orderedReg.size()));
668
669 wndTavern.draw(g);
670 }
671
672 private void drawGameMenu(Graphics g) {
673
674 }
675
676 private void drawGameInventory(Graphics g) {
677
678 }
679
680 private void drawGameStats(Graphics g) {
681
682 }
683
684 private void selectText(Textbox text) {
685 if(selectedText != null)
686 selectedText.setSelected(false);
687 selectedText = text;
688
689 if(text != null)
690 text.setSelected(true);
691 }
692
693 private static DisplayMode getBestDisplayMode(GraphicsDevice device) {
694 for (int x = 0; x < BEST_DISPLAY_MODES.length; x++) {
695 DisplayMode[] modes = device.getDisplayModes();
696 for (int i = 0; i < modes.length; i++) {
697 if (modes[i].getWidth() == BEST_DISPLAY_MODES[x].getWidth()
698 && modes[i].getHeight() == BEST_DISPLAY_MODES[x].getHeight()
699 && modes[i].getBitDepth() == BEST_DISPLAY_MODES[x].getBitDepth())
700 {
701 return BEST_DISPLAY_MODES[x];
702 }
703 }
704 }
705 return null;
706 }
707
708 public static void chooseBestDisplayMode(GraphicsDevice device) {
709 DisplayMode best = getBestDisplayMode(device);
710 if (best != null) {
711 device.setDisplayMode(best);
712 }
713 }
714
715 private void sendChatMessage(String message, String receiver) {
716 if(message != "") {
717 if(receiver != "")
718 client.sendMessage(MessageType.Chat, receiver + ">" + message);
719 else
720 client.sendMessage(MessageType.Chat, mnuChannels.getSelected() + "]" + message);
721 }
722 }
723
724 private void sendChannelMessage() {
725 changePending = true;
726 client.sendMessage(MessageType.Channel, mnuChannels.getSelected());
727 }
728
729 public void mousePressed(MouseEvent e) {
730 switch(auxState) {
731 case None:
732 switch(gameState) {
733 case Main:
734 if(wndMain.getMember("login").isClicked(e.getX(),e.getY()))
735 gameState = GameState.Login;
736 else if(wndMain.getMember("create account").isClicked(e.getX(),e.getY()))
737 gameState = GameState.CreateAccount;
738 else if(wndMain.getMember("hall of fame").isClicked(e.getX(),e.getY()))
739 gameState = GameState.Records;
740 else if(wndMain.getMember("updates").isClicked(e.getX(),e.getY()))
741 gameState = GameState.Updates;
742 else if(wndMain.getMember("game info").isClicked(e.getX(),e.getY()))
743 gameState = GameState.Info;
744 else if(wndMain.getMember("rules").isClicked(e.getX(),e.getY()))
745 gameState = GameState.Rules;
746 else if(wndMain.getMember("quit").isClicked(e.getX(),e.getY()))
747 done = true;
748 break;
749 case Login:
750 if(wndLogin.getMember("user").isClicked(e.getX(),e.getY()))
751 selectText((Textbox)wndLogin.getMember("user"));
752 else if(wndLogin.getMember("pass").isClicked(e.getX(),e.getY()))
753 selectText((Textbox)wndLogin.getMember("pass"));
754 else if(wndLogin.getMember("login").isClicked(e.getX(),e.getY())) {
755 String user = ((Textbox)wndLogin.getMember("user")).getText();
756 String pass = ((Textbox)wndLogin.getMember("pass")).getText();
757
758 if(user.equals("")) {
759 showMessage("The username is empty");
760 }else if(pass.equals("")) {
761 showMessage("The password is empty");
762 }else {
763 player = new Player(0, user, "");
764 client = new ClientThread("127.0.0.1", 5729, this);
765 //client = new ClientThread("64.9.205.76", 5729, this);
766 //client = new ClientThread("69.138.160.41", 5729, this);
767 client.start();
768 }
769 }
770 else if(wndLogin.getMember("cancel").isClicked(e.getX(),e.getY())) {
771 selectText(null);
772 wndLogin.clear();
773 gameState = GameState.Main;
774 }
775 break;
776 case CreateAccount:
777 if(wndCreateAccount.getMember("user").isClicked(e.getX(),e.getY()))
778 selectText((Textbox)wndCreateAccount.getMember("user"));
779 else if(wndCreateAccount.getMember("pass").isClicked(e.getX(),e.getY()))
780 selectText((Textbox)wndCreateAccount.getMember("pass"));
781 else if(wndCreateAccount.getMember("confirm pass").isClicked(e.getX(),e.getY()))
782 selectText((Textbox)wndCreateAccount.getMember("confirm pass"));
783 else if(wndCreateAccount.getMember("choose class").isClicked(e.getX(),e.getY())) {
784 selectText(null);
785 gameState = GameState.CreateClass;
786 }else if(wndCreateAccount.getMember("create").isClicked(e.getX(),e.getY())) {
787 String user = ((Textbox)wndCreateAccount.getMember("user")).getText();
788 String pass = ((Textbox)wndCreateAccount.getMember("pass")).getText();
789 String confirmPass = ((Textbox)wndCreateAccount.getMember("confirm pass")).getText();
790 Gender gender = Gender.valueOf(rdgGenderSelection.getButton(rdgGenderSelection.getSelected()).getLabel());
791 Job job = player.getJob();
792
793 if(user.equals("")) {
794 showMessage("The username is empty");
795 }else if(pass.equals("")) {
796 showMessage("The password is empty");
797 }else if(!pass.equals(confirmPass)) {
798 showMessage("The passwords do not match");
799 }else if(gender == Gender.None) {
800 showMessage("No gender has been selected");
801 }else if(job == Job.None) {
802 showMessage("No class has been selected");
803 }else{
804 client = new ClientThread("127.0.0.1", 5729, this);
805 //client = new ClientThread("64.9.205.76", 5729, this);
806 //client = new ClientThread("69.138.160.41", 5729, this);
807 client.start();
808 }
809 }else if(wndCreateAccount.getMember("cancel").isClicked(e.getX(),e.getY())) {
810 selectText(null);
811 wndCreateAccount.clear();
812 wndChooseClass.clear();
813 player.setJob(Job.None);
814 gameState = GameState.Main;
815 }else if(wndCreateAccount.handleEvent(e)) {
816 }
817 break;
818 case CreateClass:
819 if(wndChooseClass.getMember("select").isClicked(e.getX(),e.getY())) {
820 player.setJob(Job.valueOf(rdgClassSelection.getButton(rdgClassSelection.getSelected()).getLabel()));
821 gameState = GameState.CreateAccount;
822 }
823 else if(wndChooseClass.getMember("cancel").isClicked(e.getX(),e.getY())) {
824 rdgClassSelection.setSelected(player.getJob().toString());
825 gameState = GameState.CreateAccount;
826 }else if(wndChooseClass.handleEvent(e)) {
827 }
828 break;
829 case Records:
830 gameState = GameState.Main;
831 break;
832 case Updates:
833 gameState = GameState.Main;
834 break;
835 case Info:
836 gameState = GameState.Main;
837 break;
838 case Rules:
839 gameState = GameState.Main;
840 break;
841 case Game:
842 client.sendMessage(MessageType.Movement, (player.getLoc().getX()+e.getX()-400)+","+(player.getLoc().getY()+e.getY()-300));
843 break;
844 case GameUpgrade:
845 break;
846 case GameTavern:
847 if(wndTavern.handleEvent(e)) {
848 if(!player.getChannel().equals(mnuChannels.getSelected()) && !changePending)
849 sendChannelMessage();
850 if(wndTabbed1.getActive().equals("chat")) {
851 Textbox txt = (Textbox)wndChat.getMember("chat");
852 if(lstRegistered.isChanged()) {
853 if(lstRegistered.getSelected() == null)
854 txt.setText(txt.getText().substring(txt.getText().indexOf(">")+1));
855 else
856 txt.setText(((Registered)lstRegistered.getSelected()).getPlayer().getName()+">"+txt.getText().substring(txt.getText().indexOf(">")+1));
857 lstRegistered.changeHandled();
858 }else if(lstOnline.isChanged()) {
859 if(lstRegistered.getSelected() == null)
860 txt.setText(txt.getText().substring(txt.getText().indexOf(">")+1));
861 else
862 txt.setText(((Online)lstOnline.getSelected()).getPlayer().getName()+">"+txt.getText().substring(txt.getText().indexOf(">")+1));
863 lstOnline.changeHandled();
864 }
865 }else {
866 lstRegistered.changeHandled();
867 lstOnline.changeHandled();
868 }
869 }else if(wndTavern.getMember("play").isClicked(e.getX(),e.getY())) {
870 gameState = GameState.Game;
871 }else if(wndTavern.getMember("logout").isClicked(e.getX(),e.getY())) {
872 gameState = GameState.Main;
873 wndTavern.clear();
874 client.closeConnection();
875 }else if(wndChat.getMember("sendchat").isClicked(e.getX(),e.getY())) {
876 String msg=((Textbox)wndChat.getMember("chat")).getText();
877 if(msg.contains(">"))
878 sendChatMessage(msg.substring(msg.indexOf(">")+1).trim(), msg.substring(0, msg.indexOf(">")));
879 else
880 sendChatMessage(msg, "");
881 ((Textbox)wndChat.getMember("chat")).clear();
882 ((Textbox)wndChat.getMember("chat")).setSelected(true);
883
884 if(wndTabbed1.getActive().equals("chat")) {
885 if(wndTabbed2.getActive().equals("registered")) {
886 if(lstRegistered.getSelected() != null)
887 ((Textbox)wndChat.getMember("chat")).setText(((Registered)lstRegistered.getSelected()).getPlayer().getName()+">");
888 }else if(wndTabbed2.getActive().equals("online")) {
889 if(lstOnline.getSelected() != null)
890 ((Textbox)wndChat.getMember("chat")).setText(((Online)lstOnline.getSelected()).getPlayer().getName()+">");
891 }
892 }
893 }else if(wndChat.getMember("chat").isClicked(e.getX(),e.getY())) {
894 selectText((Textbox)wndChat.getMember("chat"));
895 }else {
896 lstRegistered.deselect();
897 lstOnline.deselect();
898 Textbox txt = (Textbox)wndChat.getMember("chat");
899 txt.setText(txt.getText().substring(txt.getText().indexOf(">")+1).trim());
900 }
901 break;
902 case GameMenu:
903 break;
904 case GameInventory:
905 break;
906 case GameStats:
907 break;
908 }
909 break;
910 case MsgBox:
911 if(wndMessage.getMember("button").isClicked(e.getX(), e.getY())) {
912 auxState = AuxState.None;
913 }
914 break;
915 case Connecting:
916 if(wndConnecting.getMember("button").isClicked(e.getX(), e.getY())) {
917 auxState = AuxState.None;
918 client.interrupt();
919 }
920 break;
921 case Loading:
922 if(wndProgress.getMember("button").isClicked(e.getX(), e.getY())) {
923 auxState = AuxState.None;
924 gameState = GameState.Main;
925 client.closeConnection();
926 }
927 break;
928 }
929 }
930
931 public void mouseReleased(MouseEvent e) {
932
933 }
934
935 public void mouseEntered(MouseEvent e) {
936
937 }
938
939 public void mouseExited(MouseEvent e) {
940
941 }
942
943 public void mouseClicked(MouseEvent e) {
944
945 }
946
947 public void keyTyped(KeyEvent e) {
948
949 }
950
951 public void keyPressed(KeyEvent e) {
952 if(selectedText != null)
953 selectedText.handleEvent(e);
954
955 if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
956 if(gameState == GameState.Game)
957 gameState = GameState.GameTavern;
958 else
959 done = true;
960 }
961
962 public void keyReleased(KeyEvent e) {
963
964 }
965
966 public static void main(String[] args) {
967 try {
968 PrintStream st = new PrintStream(new FileOutputStream("err.txt", true));
969 System.setErr(st);
970 System.setOut(st);
971 System.out.println("-----[ Session started on " + dateString() + " ]-----");
972
973 GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
974 GraphicsDevice device = env.getDefaultScreenDevice();
975 new LostHavenClient(device);
976 }
977 catch (Exception e) {
978 e.printStackTrace();
979 }
980 System.exit(0);
981 }
982
983 public class Registered implements Listable {
984 private Player player;
985
986 public Registered(Player player) {
987 this.player = player;
988 }
989
990 public Player getPlayer() {
991 return player;
992 }
993
994 public void drawListString(int x, int y, Graphics g) {
995 g.setColor(Color.green);
996 g.drawString(Integer.toString(player.getId()), x+15, y);
997 g.drawString(player.getName(), x+95, y);
998 if(orderedOnline.contains(player))
999 g.drawString("Online", x+235, y);
1000 else {
1001 g.setColor(Color.red);
1002 g.drawString("Offline", x+235, y);
1003 }
1004 }
1005 }
1006
1007 public class Online implements Listable {
1008 private Player player;
1009
1010 public Online(Player player) {
1011 this.player = player;
1012 }
1013
1014 public Player getPlayer() {
1015 return player;
1016 }
1017
1018 public void drawListString(int x, int y, Graphics g) {
1019 g.setColor(Color.green);
1020 g.drawString(Integer.toString(player.getId()), x+15, y);
1021 g.drawString(player.getName(), x+95, y);
1022 }
1023 }
1024}
Note: See TracBrowser for help on using the repository browser.