source: java-rpg-client/LostHavenClient.java@ 67f5126

Last change on this file since 67f5126 was 67f5126, checked in by dportnoy <dmp1488@…>, 17 years ago

[svn r26]

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