source: network-game/client/Client/main.cpp@ 5c95436

Last change on this file since 5c95436 was 5c95436, checked in by dportnoy <dmp1488@…>, 12 years ago

Add a RadioButtonList gui control to the client, re-organize the login/registration screen into two separate screens, and allow the player to select their class during registration

  • Property mode set to 100644
File size: 26.5 KB
Line 
1#include "../../common/Compiler.h"
2
3#if defined WINDOWS
4 #include <winsock2.h>
5 #include <WS2tcpip.h>
6#elif defined LINUX
7 #include <sys/types.h>
8 #include <unistd.h>
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <netdb.h>
12 #include <cstring>
13#endif
14
15#include <cstdio>
16#include <cstdlib>
17#include <cmath>
18#include <sys/types.h>
19#include <string>
20#include <iostream>
21#include <sstream>
22#include <map>
23
24#include <allegro5/allegro.h>
25#include <allegro5/allegro_font.h>
26#include <allegro5/allegro_ttf.h>
27#include <allegro5/allegro_primitives.h>
28
29#include "../../common/Message.h"
30#include "../../common/Common.h"
31#include "../../common/WorldMap.h"
32#include "../../common/Player.h"
33#include "../../common/Projectile.h"
34
35#include "Window.h"
36#include "Textbox.h"
37#include "Button.h"
38#include "RadioButtonList.h"
39#include "chat.h"
40
41#ifdef WINDOWS
42 #pragma comment(lib, "ws2_32.lib")
43#endif
44
45using namespace std;
46
47void initWinSock();
48void shutdownWinSock();
49void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, WorldMap *gameMap, map<unsigned int, Player>& mapPlayers, map<unsigned int, Projectile>& mapProjectiles, unsigned int& curPlayerId, int &scoreBlue, int &scoreRed);
50void drawMap(WorldMap* gameMap);
51void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId);
52POSITION screenToMap(POSITION pos);
53POSITION mapToScreen(POSITION pos);
54
55// callbacks
56void goToLoginScreen();
57void goToRegisterScreen();
58void registerAccount();
59void login();
60void logout();
61void quit();
62void sendChatMessage();
63
64void error(const char *);
65
66const float FPS = 60;
67const int SCREEN_W = 640;
68const int SCREEN_H = 480;
69
70enum STATE {
71 STATE_START,
72 STATE_LOGIN // this means you're already logged in
73};
74
75int state;
76
77bool doexit;
78
79Window* wndLogin;
80Window* wndRegister;
81Window* wndMain;
82Window* wndCurrent;
83
84// wndLogin
85Textbox* txtUsername;
86Textbox* txtPassword;
87
88// wndRegister
89Textbox* txtUsernameRegister;
90Textbox* txtPasswordRegister;
91RadioButtonList* rblClasses;
92
93// wndMain
94Textbox* txtChat;
95
96int sock;
97struct sockaddr_in server, from;
98struct hostent *hp;
99NETWORK_MSG msgTo, msgFrom;
100string username;
101chat chatConsole;
102
103int main(int argc, char **argv)
104{
105 ALLEGRO_DISPLAY *display = NULL;
106 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
107 ALLEGRO_TIMER *timer = NULL;
108 bool key[4] = { false, false, false, false };
109 bool redraw = true;
110 doexit = false;
111 map<unsigned int, Player> mapPlayers;
112 map<unsigned int, Projectile> mapProjectiles;
113 unsigned int curPlayerId = -1;
114 int scoreBlue, scoreRed;
115
116 scoreBlue = 0;
117 scoreRed = 0;
118
119 state = STATE_START;
120
121 if(!al_init()) {
122 fprintf(stderr, "failed to initialize allegro!\n");
123 return -1;
124 }
125
126 if (al_init_primitives_addon())
127 cout << "Primitives initialized" << endl;
128 else
129 cout << "Primitives not initialized" << endl;
130
131 al_init_font_addon();
132 al_init_ttf_addon();
133
134 #if defined WINDOWS
135 ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0);
136 #elif defined LINUX
137 ALLEGRO_FONT *font = al_load_ttf_font("pirulen.ttf", 12, 0);
138 #endif
139
140 if (!font) {
141 fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
142 getchar();
143 return -1;
144 }
145
146 if(!al_install_keyboard()) {
147 fprintf(stderr, "failed to initialize the keyboard!\n");
148 return -1;
149 }
150
151 if(!al_install_mouse()) {
152 fprintf(stderr, "failed to initialize the mouse!\n");
153 return -1;
154 }
155
156 timer = al_create_timer(1.0 / FPS);
157 if(!timer) {
158 fprintf(stderr, "failed to create timer!\n");
159 return -1;
160 }
161
162 display = al_create_display(SCREEN_W, SCREEN_H);
163 if(!display) {
164 fprintf(stderr, "failed to create display!\n");
165 al_destroy_timer(timer);
166 return -1;
167 }
168
169 WorldMap* gameMap = WorldMap::loadMapFromFile("../../data/map.txt");
170
171 wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
172 wndLogin->addComponent(new Textbox(104, 40, 100, 20, font));
173 wndLogin->addComponent(new Textbox(104, 70, 100, 20, font));
174 wndLogin->addComponent(new Button(22, 100, 90, 20, font, "Create an Account", goToRegisterScreen));
175 wndLogin->addComponent(new Button(122, 100, 60, 20, font, "Login", login));
176 wndLogin->addComponent(new Button(540, 10, 80, 20, font, "Quit", quit));
177
178 txtUsername = (Textbox*)wndLogin->getComponent(0);
179 txtPassword = (Textbox*)wndLogin->getComponent(1);
180
181 wndRegister = new Window(0, 0, SCREEN_W, SCREEN_H);
182 wndRegister->addComponent(new Textbox(104, 40, 100, 20, font));
183 wndRegister->addComponent(new Textbox(104, 70, 100, 20, font));
184 wndRegister->addComponent(new Button(22, 100, 90, 20, font, "Back", goToLoginScreen));
185 wndRegister->addComponent(new Button(122, 100, 60, 20, font, "Submit", registerAccount));
186 wndRegister->addComponent(new Button(540, 10, 80, 20, font, "Quit", quit));
187 wndRegister->addComponent(new RadioButtonList(20, 130, "Pick a class", font));
188
189 txtUsernameRegister = (Textbox*)wndRegister->getComponent(0);
190 txtPasswordRegister = (Textbox*)wndRegister->getComponent(1);
191
192 rblClasses = (RadioButtonList*)wndRegister->getComponent(5);
193 rblClasses->addRadioButton("Warrior");
194 rblClasses->addRadioButton("Ranger");
195
196 wndMain = new Window(0, 0, SCREEN_W, SCREEN_H);
197 wndMain->addComponent(new Textbox(95, 40, 525, 20, font));
198 wndMain->addComponent(new Button(95, 70, 160, 20, font, "Send Message", sendChatMessage));
199 wndMain->addComponent(new Button(540, 10, 80, 20, font, "Logout", logout));
200
201 txtChat = (Textbox*)wndMain->getComponent(0);
202
203 wndCurrent = wndLogin;
204
205 event_queue = al_create_event_queue();
206 if(!event_queue) {
207 fprintf(stderr, "failed to create event_queue!\n");
208 al_destroy_display(display);
209 al_destroy_timer(timer);
210 return -1;
211 }
212
213 al_set_target_bitmap(al_get_backbuffer(display));
214
215 al_register_event_source(event_queue, al_get_display_event_source(display));
216 al_register_event_source(event_queue, al_get_timer_event_source(timer));
217 al_register_event_source(event_queue, al_get_keyboard_event_source());
218 al_register_event_source(event_queue, al_get_mouse_event_source());
219
220 al_clear_to_color(al_map_rgb(0,0,0));
221
222 al_flip_display();
223
224 if (argc != 3) {
225 cout << "Usage: server port" << endl;
226 exit(1);
227 }
228
229 initWinSock();
230
231 sock = socket(AF_INET, SOCK_DGRAM, 0);
232 if (sock < 0)
233 error("socket");
234
235 set_nonblock(sock);
236
237 server.sin_family = AF_INET;
238 hp = gethostbyname(argv[1]);
239 if (hp==0)
240 error("Unknown host");
241
242 memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
243 server.sin_port = htons(atoi(argv[2]));
244
245 al_start_timer(timer);
246
247 while(!doexit)
248 {
249 ALLEGRO_EVENT ev;
250
251 al_wait_for_event(event_queue, &ev);
252
253 if(wndCurrent->handleEvent(ev)) {
254 // do nothing
255 }
256 else if(ev.type == ALLEGRO_EVENT_TIMER) {
257 redraw = true; // seems like we should just call a draw function here instead
258 }
259 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
260 doexit = true;
261 }
262 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
263 }
264 else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
265 switch(ev.keyboard.keycode) {
266 case ALLEGRO_KEY_ESCAPE:
267 doexit = true;
268 break;
269 case ALLEGRO_KEY_S: // pickup an item next to you
270 if (state == STATE_LOGIN) {
271 msgTo.type = MSG_TYPE_PICKUP_FLAG;
272 memcpy(msgTo.buffer, &curPlayerId, 4);
273 sendMessage(&msgTo, sock, &server);
274 }
275 break;
276 case ALLEGRO_KEY_D: // drop the current item
277 if (state == STATE_LOGIN) {
278 // find the current player in the player list
279 map<unsigned int, Player>::iterator it;
280 Player* p = NULL;
281 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
282 {
283 if (it->second.id == curPlayerId)
284 p = &it->second;
285 }
286
287 if (p != NULL) {
288 int flagType = WorldMap::OBJECT_NONE;
289
290 if (p->hasBlueFlag)
291 flagType = WorldMap::OBJECT_BLUE_FLAG;
292 else if (p->hasRedFlag)
293 flagType = WorldMap::OBJECT_RED_FLAG;
294
295 if (flagType != WorldMap::OBJECT_NONE) {
296 msgTo.type = MSG_TYPE_DROP_FLAG;
297 memcpy(msgTo.buffer, &curPlayerId, 4);
298 sendMessage(&msgTo, sock, &server);
299 }
300 }
301 }
302 break;
303 }
304 }
305 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
306 if(wndCurrent == wndMain) {
307 if (ev.mouse.button == 1) { // left click
308 msgTo.type = MSG_TYPE_PLAYER_MOVE;
309
310 POSITION pos;
311 pos.x = ev.mouse.x;
312 pos.y = ev.mouse.y;
313 pos = screenToMap(pos);
314
315 if (pos.x != -1)
316 {
317 memcpy(msgTo.buffer, &curPlayerId, 4);
318 memcpy(msgTo.buffer+4, &pos.x, 4);
319 memcpy(msgTo.buffer+8, &pos.y, 4);
320
321 sendMessage(&msgTo, sock, &server);
322 }
323 else
324 cout << "Invalid point: User did not click on the map" << endl;
325 }else if (ev.mouse.button == 2) { // right click
326 map<unsigned int, Player>::iterator it;
327
328 cout << "Detected a right-click" << endl;
329
330 Player* curPlayer;
331 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
332 {
333 if (it->second.id == curPlayerId)
334 curPlayer = &it->second;
335 }
336
337 Player* target;
338 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
339 {
340 // need to check if the right-click was actually on this player
341 target = &it->second;
342 if (target->id != curPlayerId && target->team != curPlayer->team) {
343 msgTo.type = MSG_TYPE_START_ATTACK;
344 memcpy(msgTo.buffer, &curPlayerId, 4);
345 memcpy(msgTo.buffer+4, &target->id, 4);
346
347 sendMessage(&msgTo, sock, &server);
348 }
349 }
350 }
351 }
352 }
353
354 if (receiveMessage(&msgFrom, sock, &from) >= 0)
355 processMessage(msgFrom, state, chatConsole, gameMap, mapPlayers, mapProjectiles, curPlayerId, scoreBlue, scoreRed);
356
357 if (redraw)
358 {
359 redraw = false;
360
361 wndCurrent->draw(display);
362
363 chatConsole.draw(font, al_map_rgb(255,255,255));
364
365 if(wndCurrent == wndLogin) {
366 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Username:");
367 al_draw_text(font, al_map_rgb(0, 255, 0), 1, 73, ALLEGRO_ALIGN_LEFT, "Password:");
368 }
369 else if(wndCurrent == wndMain) {
370 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
371
372 ostringstream ossScoreBlue, ossScoreRed;
373
374 ossScoreBlue << "Blue: " << scoreBlue << endl;
375 ossScoreRed << "Red: " << scoreRed << endl;
376
377 al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
378 al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
379
380 // update players
381 map<unsigned int, Player>::iterator it;
382 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
383 {
384 it->second.updateTarget(mapPlayers);
385 }
386
387 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
388 {
389 it->second.move(gameMap); // ignore return value
390 }
391
392 // update projectile positions
393 map<unsigned int, Projectile>::iterator it2;
394 for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
395 {
396 it2->second.move(mapPlayers);
397 }
398
399 drawMap(gameMap);
400 drawPlayers(mapPlayers, font, curPlayerId);
401
402 // draw projectiles
403 for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
404 {
405 Projectile proj = it2->second;
406
407 FLOAT_POSITION target = mapPlayers[proj.target].pos;
408 float angle = atan2(target.y-proj.pos.toFloat().y, target.x-proj.pos.toFloat().x);
409
410 POSITION start, end;
411 start.x = cos(angle)*15+proj.pos.x;
412 start.y = sin(angle)*15+proj.pos.y;
413 end.x = proj.pos.x;
414 end.y = proj.pos.y;
415
416 start = mapToScreen(start);
417 end = mapToScreen(end);
418
419 al_draw_line(start.x, start.y, end.x, end.y, al_map_rgb(0, 0, 0), 4);
420 }
421 }
422
423 al_flip_display();
424 }
425 }
426
427 #if defined WINDOWS
428 closesocket(sock);
429 #elif defined LINUX
430 close(sock);
431 #endif
432
433 shutdownWinSock();
434
435 delete wndLogin;
436 delete wndMain;
437
438 delete gameMap;
439
440 al_destroy_event_queue(event_queue);
441 al_destroy_display(display);
442 al_destroy_timer(timer);
443
444 return 0;
445}
446
447// need to make a function like this that works on windows
448void error(const char *msg)
449{
450 perror(msg);
451 shutdownWinSock();
452 exit(1);
453}
454
455void initWinSock()
456{
457#if defined WINDOWS
458 WORD wVersionRequested;
459 WSADATA wsaData;
460 int wsaerr;
461
462 wVersionRequested = MAKEWORD(2, 2);
463 wsaerr = WSAStartup(wVersionRequested, &wsaData);
464
465 if (wsaerr != 0) {
466 cout << "The Winsock dll not found." << endl;
467 exit(1);
468 }else
469 cout << "The Winsock dll was found." << endl;
470#endif
471}
472
473void shutdownWinSock()
474{
475#if defined WINDOWS
476 WSACleanup();
477#endif
478}
479
480POSITION screenToMap(POSITION pos)
481{
482 pos.x = pos.x-300;
483 pos.y = pos.y-100;
484
485 if (pos.x < 0 || pos.y < 0)
486 {
487 pos.x = -1;
488 pos.y = -1;
489 }
490
491 return pos;
492}
493
494POSITION mapToScreen(POSITION pos)
495{
496 pos.x = pos.x+300;
497 pos.y = pos.y+100;
498
499 return pos;
500}
501
502POSITION mapToScreen(FLOAT_POSITION pos)
503{
504 POSITION p;
505 p.x = pos.x+300;
506 p.y = pos.y+100;
507
508 return p;
509}
510
511void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, WorldMap *gameMap, map<unsigned int, Player>& mapPlayers, map<unsigned int, Projectile>& mapProjectiles, unsigned int& curPlayerId, int &scoreBlue, int &scoreRed)
512{
513 string response = string(msg.buffer);
514
515 switch(state)
516 {
517 case STATE_START:
518 {
519 cout << "In STATE_START" << endl;
520
521 switch(msg.type)
522 {
523 case MSG_TYPE_REGISTER:
524 {
525 break;
526 }
527 case MSG_TYPE_LOGIN:
528 {
529 if (response.compare("Player has already logged in.") == 0)
530 {
531 username.clear();
532 cout << "User login failed" << endl;
533 }
534 else if (response.compare("Incorrect username or password") == 0)
535 {
536 username.clear();
537 cout << "User login failed" << endl;
538 }
539 else
540 {
541 state = STATE_LOGIN;
542 wndCurrent = wndMain;
543
544 Player p("", "");
545 p.deserialize(msg.buffer);
546 mapPlayers[p.id] = p;
547 curPlayerId = p.id;
548
549 cout << "Got a valid login response with the player" << endl;
550 cout << "Player id: " << curPlayerId << endl;
551 cout << "map size: " << mapPlayers.size() << endl;
552 }
553
554 break;
555 }
556 case MSG_TYPE_PLAYER: // kind of hacky to put this here
557 {
558 Player p("", "");
559 p.deserialize(msg.buffer);
560 p.timeLastUpdated = getCurrentMillis();
561
562 mapPlayers[p.id] = p;
563
564 break;
565 }
566 case MSG_TYPE_OBJECT:
567 {
568 WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
569 o.deserialize(msg.buffer);
570 cout << "object id: " << o.id << endl;
571 gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
572
573 break;
574 }
575 }
576
577 break;
578 }
579 case STATE_LOGIN:
580 {
581 switch(msg.type)
582 {
583 case MSG_TYPE_REGISTER:
584 {
585 break;
586 }
587 case MSG_TYPE_LOGIN:
588 {
589 cout << "Got a login message" << endl;
590
591 chatConsole.addLine(response);
592 cout << "Added new line" << endl;
593
594 break;
595 }
596 case MSG_TYPE_LOGOUT:
597 {
598 cout << "Got a logout message" << endl;
599
600 if (response.compare("You have successfully logged out.") == 0)
601 {
602 cout << "Logged out" << endl;
603 state = STATE_START;
604 wndCurrent = wndLogin;
605 }
606
607 break;
608 }
609 case MSG_TYPE_PLAYER:
610 {
611 Player p("", "");
612 p.deserialize(msg.buffer);
613 p.timeLastUpdated = getCurrentMillis();
614 p.isChasing = false;
615 if (p.health <= 0)
616 p.isDead = true;
617 else
618 p.isDead = false;
619
620 mapPlayers[p.id] = p;
621
622 break;
623 }
624 case MSG_TYPE_PLAYER_MOVE:
625 {
626 unsigned int id;
627 int x, y;
628
629 memcpy(&id, msg.buffer, 4);
630 memcpy(&x, msg.buffer+4, 4);
631 memcpy(&y, msg.buffer+8, 4);
632
633 mapPlayers[id].target.x = x;
634 mapPlayers[id].target.y = y;
635
636 break;
637 }
638 case MSG_TYPE_CHAT:
639 {
640 chatConsole.addLine(response);
641
642 break;
643 }
644 case MSG_TYPE_OBJECT:
645 {
646 cout << "Received object message in STATE_LOGIN." << endl;
647
648 WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
649 o.deserialize(msg.buffer);
650 gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
651
652 break;
653 }
654 case MSG_TYPE_REMOVE_OBJECT:
655 {
656 cout << "Received REMOVE_OBJECT message!" << endl;
657
658 int id;
659 memcpy(&id, msg.buffer, 4);
660
661 cout << "Removing object with id " << id << endl;
662
663 if (!gameMap->removeObject(id))
664 cout << "Did not remove the object" << endl;
665
666 break;
667 }
668 case MSG_TYPE_SCORE:
669 {
670 memcpy(&scoreBlue, msg.buffer, 4);
671 memcpy(&scoreRed, msg.buffer+4, 4);
672
673 break;
674 }
675 case MSG_TYPE_ATTACK:
676 {
677 cout << "Received ATTACK message" << endl;
678
679 break;
680 }
681 case MSG_TYPE_START_ATTACK:
682 {
683 cout << "Received START_ATTACK message" << endl;
684
685 unsigned int id, targetID;
686 memcpy(&id, msg.buffer, 4);
687 memcpy(&targetID, msg.buffer+4, 4);
688
689 cout << "source id: " << id << endl;
690 cout << "target id: " << targetID << endl;
691
692 Player* source = &mapPlayers[id];
693 source->targetPlayer = targetID;
694 source->isChasing = true;
695
696 break;
697 }
698 case MSG_TYPE_PROJECTILE:
699 {
700 cout << "Received a PROJECTILE message" << endl;
701
702 int id, x, y, targetId;
703
704 memcpy(&id, msg.buffer, 4);
705 memcpy(&x, msg.buffer+4, 4);
706 memcpy(&y, msg.buffer+8, 4);
707 memcpy(&targetId, msg.buffer+12, 4);
708
709 cout << "id: " << id << endl;
710 cout << "x: " << x << endl;
711 cout << "y: " << y << endl;
712 cout << "Target: " << targetId << endl;
713
714 Projectile proj(x, y, targetId, 0);
715 proj.setId(id);
716
717 mapProjectiles[id] = proj;
718
719 break;
720 }
721 case MSG_TYPE_REMOVE_PROJECTILE:
722 {
723 cout << "Received a REMOVE_PROJECTILE message" << endl;
724
725 int id;
726
727 memcpy(&id, msg.buffer, 4);
728
729 mapProjectiles.erase(id);
730
731 break;
732 }
733 default:
734 {
735 cout << "Received an unexpected message type: " << msg.type << endl;
736 }
737 }
738
739 break;
740 }
741 default:
742 {
743 cout << "The state has an invalid value: " << state << endl;
744
745 break;
746 }
747 }
748}
749
750// this should probably be in the WorldMap class
751void drawMap(WorldMap* gameMap)
752{
753 POSITION mapPos;
754 mapPos.x = 0;
755 mapPos.y = 0;
756 mapPos = mapToScreen(mapPos);
757
758 for (int x=0; x<12; x++)
759 {
760 for (int y=0; y<12; y++)
761 {
762 WorldMap::TerrainType el = gameMap->getElement(x, y);
763 WorldMap::StructureType structure = gameMap->getStructure(x, y);
764
765 if (el == WorldMap::TERRAIN_GRASS)
766 al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(0, 255, 0));
767 else if (el == WorldMap::TERRAIN_OCEAN)
768 al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(0, 0, 255));
769 else if (el == WorldMap::TERRAIN_ROCK)
770 al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(100, 100, 0));
771
772 if (structure == WorldMap::STRUCTURE_BLUE_FLAG) {
773 al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
774 //al_draw_filled_rectangle(x*25+5+mapPos.x, y*25+5+mapPos.y, x*25+20+mapPos.x, y*25+20+mapPos.y, al_map_rgb(0, 0, 255));
775 }else if (structure == WorldMap::STRUCTURE_RED_FLAG) {
776 al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
777 //al_draw_filled_rectangle(x*25+5+mapPos.x, y*25+5+mapPos.y, x*25+20+mapPos.x, y*25+20+mapPos.y, al_map_rgb(255, 0, 0));
778 }
779 }
780 }
781
782 for (int x=0; x<12; x++)
783 {
784 for (int y=0; y<12; y++)
785 {
786 vector<WorldMap::Object> vctObjects = gameMap->getObjects(x, y);
787
788 vector<WorldMap::Object>::iterator it;
789 for(it = vctObjects.begin(); it != vctObjects.end(); it++) {
790 switch(it->type) {
791 case WorldMap::OBJECT_BLUE_FLAG:
792 al_draw_filled_rectangle(it->pos.x-8+mapPos.x, it->pos.y-8+mapPos.y, it->pos.x+8+mapPos.x, it->pos.y+8+mapPos.y, al_map_rgb(0, 0, 255));
793 break;
794 case WorldMap::OBJECT_RED_FLAG:
795 al_draw_filled_rectangle(it->pos.x-8+mapPos.x, it->pos.y-8+mapPos.y, it->pos.x+8+mapPos.x, it->pos.y+8+mapPos.y, al_map_rgb(255, 0, 0));
796 break;
797 }
798 }
799 }
800 }
801}
802
803void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId)
804{
805 map<unsigned int, Player>::iterator it;
806
807 Player* p;
808 POSITION pos;
809 ALLEGRO_COLOR color;
810
811 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
812 {
813 p = &it->second;
814
815 if (p->isDead)
816 continue;
817
818 pos = mapToScreen(p->pos);
819
820 if (p->id == curPlayerId)
821 al_draw_filled_circle(pos.x, pos.y, 14, al_map_rgb(0, 0, 0));
822
823 if (p->team == 0)
824 color = al_map_rgb(0, 0, 255);
825 else if (p->team == 1)
826 color = al_map_rgb(255, 0, 0);
827
828 al_draw_filled_circle(pos.x, pos.y, 12, color);
829
830 // draw player class
831 int fontHeight = al_get_font_line_height(font);
832
833 string strClass;
834 switch (p->playerClass) {
835 case Player::CLASS_WARRIOR:
836 strClass = "W";
837 break;
838 case Player::CLASS_RANGER:
839 strClass = "R";
840 break;
841 case Player::CLASS_NONE:
842 strClass = "";
843 break;
844 default:
845 strClass = "";
846 break;
847 }
848 al_draw_text(font, al_map_rgb(0, 0, 0), pos.x, pos.y-fontHeight/2, ALLEGRO_ALIGN_CENTRE, strClass.c_str());
849
850 // draw player health
851 al_draw_filled_rectangle(pos.x-12, pos.y-24, pos.x+12, pos.y-16, al_map_rgb(0, 0, 0));
852 if (it->second.maxHealth != 0)
853 al_draw_filled_rectangle(pos.x-11, pos.y-23, pos.x-11+(22*it->second.health)/it->second.maxHealth, pos.y-17, al_map_rgb(255, 0, 0));
854
855 if (p->hasBlueFlag)
856 al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(0, 0, 255));
857 else if (p->hasRedFlag)
858 al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(255, 0, 0));
859 }
860}
861
862void goToRegisterScreen()
863{
864 wndCurrent = wndRegister;
865
866 txtUsernameRegister->clear();
867 txtPasswordRegister->clear();
868}
869
870void goToLoginScreen()
871{
872 wndCurrent = wndLogin;
873
874 txtUsername->clear();
875 txtPassword->clear();
876}
877
878void registerAccount()
879{
880 string username = txtUsernameRegister->getStr();
881 string password = txtPasswordRegister->getStr();
882
883 txtUsernameRegister->clear();
884 txtPasswordRegister->clear();
885 // maybe clear rblClasses as well (add a method to RadioButtonList to enable this)
886
887 Player::PlayerClass playerClass;
888
889 switch (rblClasses->getSelectedButton()) {
890 case 0:
891 playerClass = Player::CLASS_WARRIOR;
892 break;
893 case 1:
894 playerClass = Player::CLASS_RANGER;
895 break;
896 default:
897 cout << "Invalid class selection" << endl;
898 playerClass = Player::CLASS_NONE;
899 break;
900 }
901
902 msgTo.type = MSG_TYPE_REGISTER;
903
904 strcpy(msgTo.buffer, username.c_str());
905 strcpy(msgTo.buffer+username.size()+1, password.c_str());
906 memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
907
908 sendMessage(&msgTo, sock, &server);
909}
910
911void login()
912{
913 string strUsername = txtUsername->getStr();
914 string strPassword = txtPassword->getStr();
915 username = strUsername;
916
917 txtUsername->clear();
918 txtPassword->clear();
919
920 msgTo.type = MSG_TYPE_LOGIN;
921
922 strcpy(msgTo.buffer, strUsername.c_str());
923 strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
924
925 sendMessage(&msgTo, sock, &server);
926}
927
928void logout()
929{
930 txtChat->clear();
931
932 msgTo.type = MSG_TYPE_LOGOUT;
933
934 strcpy(msgTo.buffer, username.c_str());
935
936 sendMessage(&msgTo, sock, &server);
937}
938
939void quit()
940{
941 doexit = true;
942}
943
944void sendChatMessage()
945{
946 string msg = txtChat->getStr();
947
948 txtChat->clear();
949
950 msgTo.type = MSG_TYPE_CHAT;
951
952 strcpy(msgTo.buffer, msg.c_str());
953
954 sendMessage(&msgTo, sock, &server);
955}
Note: See TracBrowser for help on using the repository browser.