source: java-rpg-client/LostHavenClient.java@ f0f2ead

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

[svn r8]

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