source: network-game/client/Client/main.cpp@ 1a3c42d

Last change on this file since 1a3c42d was f3cf1a5, checked in by dportnoy <dmp1488@…>, 11 years ago

The client draws all map objects instead of some of them

  • Property mode set to 100644
File size: 26.8 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 = 1024;
68const int SCREEN_H = 768;
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(516, 40, 100, 20, font));
173 wndLogin->addComponent(new Textbox(516, 70, 100, 20, font));
174 wndLogin->addComponent(new Button(330, 100, 194, 20, font, "Create an Account", goToRegisterScreen));
175 wndLogin->addComponent(new Button(534, 100, 60, 20, font, "Login", login));
176 wndLogin->addComponent(new Button(920, 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(516, 40, 100, 20, font));
183 wndRegister->addComponent(new Textbox(516, 70, 100, 20, font));
184 wndRegister->addComponent(new Button(468, 100, 56, 20, font, "Back", goToLoginScreen));
185 wndRegister->addComponent(new Button(534, 100, 70, 20, font, "Submit", registerAccount));
186 wndRegister->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit));
187 wndRegister->addComponent(new RadioButtonList(432, 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, 300, 20, font));
198 wndMain->addComponent(new Button(95, 70, 60, 20, font, "Send", sendChatMessage));
199 wndMain->addComponent(new Button(920, 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 // right now, this code will target all players other than the current one
342 target = &it->second;
343 if (target->id != curPlayerId && target->team != curPlayer->team) {
344 msgTo.type = MSG_TYPE_START_ATTACK;
345 memcpy(msgTo.buffer, &curPlayerId, 4);
346 memcpy(msgTo.buffer+4, &target->id, 4);
347
348 sendMessage(&msgTo, sock, &server);
349 }
350 }
351 }
352 }
353 }
354
355 if (receiveMessage(&msgFrom, sock, &from) >= 0)
356 processMessage(msgFrom, state, chatConsole, gameMap, mapPlayers, mapProjectiles, curPlayerId, scoreBlue, scoreRed);
357
358 if (redraw)
359 {
360 redraw = false;
361
362 wndCurrent->draw(display);
363
364 chatConsole.draw(font, al_map_rgb(255,255,255));
365
366 // There should be label gui components that show these or each textbox should have a label
367 if(wndCurrent == wndLogin || wndCurrent == wndRegister) {
368 al_draw_text(font, al_map_rgb(0, 255, 0), 416, 43, ALLEGRO_ALIGN_LEFT, "Username:");
369 al_draw_text(font, al_map_rgb(0, 255, 0), 413, 73, ALLEGRO_ALIGN_LEFT, "Password:");
370 }
371 else if(wndCurrent == wndMain) {
372 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
373
374 ostringstream ossScoreBlue, ossScoreRed;
375
376 ossScoreBlue << "Blue: " << scoreBlue << endl;
377 ossScoreRed << "Red: " << scoreRed << endl;
378
379 al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
380 al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
381
382 // update players
383 map<unsigned int, Player>::iterator it;
384 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
385 {
386 it->second.updateTarget(mapPlayers);
387 }
388
389 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
390 {
391 it->second.move(gameMap); // ignore return value
392 }
393
394 // update projectile positions
395 map<unsigned int, Projectile>::iterator it2;
396 for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
397 {
398 it2->second.move(mapPlayers);
399 }
400
401 drawMap(gameMap);
402 drawPlayers(mapPlayers, font, curPlayerId);
403
404 // draw projectiles
405 for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
406 {
407 Projectile proj = it2->second;
408
409 FLOAT_POSITION target = mapPlayers[proj.target].pos;
410 float angle = atan2(target.y-proj.pos.toFloat().y, target.x-proj.pos.toFloat().x);
411
412 POSITION start, end;
413 start.x = cos(angle)*15+proj.pos.x;
414 start.y = sin(angle)*15+proj.pos.y;
415 end.x = proj.pos.x;
416 end.y = proj.pos.y;
417
418 start = mapToScreen(start);
419 end = mapToScreen(end);
420
421 al_draw_line(start.x, start.y, end.x, end.y, al_map_rgb(0, 0, 0), 4);
422 }
423 }
424
425 al_flip_display();
426 }
427 }
428
429 #if defined WINDOWS
430 closesocket(sock);
431 #elif defined LINUX
432 close(sock);
433 #endif
434
435 shutdownWinSock();
436
437 delete wndLogin;
438 delete wndMain;
439
440 delete gameMap;
441
442 al_destroy_event_queue(event_queue);
443 al_destroy_display(display);
444 al_destroy_timer(timer);
445
446 return 0;
447}
448
449// need to make a function like this that works on windows
450void error(const char *msg)
451{
452 perror(msg);
453 shutdownWinSock();
454 exit(1);
455}
456
457void initWinSock()
458{
459#if defined WINDOWS
460 WORD wVersionRequested;
461 WSADATA wsaData;
462 int wsaerr;
463
464 wVersionRequested = MAKEWORD(2, 2);
465 wsaerr = WSAStartup(wVersionRequested, &wsaData);
466
467 if (wsaerr != 0) {
468 cout << "The Winsock dll not found." << endl;
469 exit(1);
470 }else
471 cout << "The Winsock dll was found." << endl;
472#endif
473}
474
475void shutdownWinSock()
476{
477#if defined WINDOWS
478 WSACleanup();
479#endif
480}
481
482POSITION screenToMap(POSITION pos)
483{
484 pos.x = pos.x-300;
485 pos.y = pos.y-100;
486
487 if (pos.x < 0 || pos.y < 0)
488 {
489 pos.x = -1;
490 pos.y = -1;
491 }
492
493 return pos;
494}
495
496POSITION mapToScreen(POSITION pos)
497{
498 pos.x = pos.x+300;
499 pos.y = pos.y+100;
500
501 return pos;
502}
503
504POSITION mapToScreen(FLOAT_POSITION pos)
505{
506 POSITION p;
507 p.x = pos.x+300;
508 p.y = pos.y+100;
509
510 return p;
511}
512
513void 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)
514{
515 string response = string(msg.buffer);
516
517 switch(state)
518 {
519 case STATE_START:
520 {
521 cout << "In STATE_START" << endl;
522
523 switch(msg.type)
524 {
525 case MSG_TYPE_REGISTER:
526 {
527 break;
528 }
529 case MSG_TYPE_LOGIN:
530 {
531 if (response.compare("Player has already logged in.") == 0)
532 {
533 username.clear();
534 cout << "User login failed" << endl;
535 }
536 else if (response.compare("Incorrect username or password") == 0)
537 {
538 username.clear();
539 cout << "User login failed" << endl;
540 }
541 else
542 {
543 state = STATE_LOGIN;
544 wndCurrent = wndMain;
545
546 Player p("", "");
547 p.deserialize(msg.buffer);
548 mapPlayers[p.id] = p;
549 curPlayerId = p.id;
550
551 cout << "Got a valid login response with the player" << endl;
552 cout << "Player id: " << curPlayerId << endl;
553 cout << "map size: " << mapPlayers.size() << endl;
554 }
555
556 break;
557 }
558 case MSG_TYPE_PLAYER: // kind of hacky to put this here
559 {
560 Player p("", "");
561 p.deserialize(msg.buffer);
562 p.timeLastUpdated = getCurrentMillis();
563
564 mapPlayers[p.id] = p;
565
566 break;
567 }
568 case MSG_TYPE_OBJECT:
569 {
570 WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
571 o.deserialize(msg.buffer);
572 cout << "object id: " << o.id << endl;
573 gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
574
575 break;
576 }
577 }
578
579 break;
580 }
581 case STATE_LOGIN:
582 {
583 switch(msg.type)
584 {
585 case MSG_TYPE_REGISTER:
586 {
587 break;
588 }
589 case MSG_TYPE_LOGIN:
590 {
591 cout << "Got a login message" << endl;
592
593 chatConsole.addLine(response);
594 cout << "Added new line" << endl;
595
596 break;
597 }
598 case MSG_TYPE_LOGOUT:
599 {
600 cout << "Got a logout message" << endl;
601
602 if (response.compare("You have successfully logged out.") == 0)
603 {
604 cout << "Logged out" << endl;
605 state = STATE_START;
606 wndCurrent = wndLogin;
607 }
608
609 break;
610 }
611 case MSG_TYPE_PLAYER:
612 {
613 Player p("", "");
614 p.deserialize(msg.buffer);
615 p.timeLastUpdated = getCurrentMillis();
616 p.isChasing = false;
617 if (p.health <= 0)
618 p.isDead = true;
619 else
620 p.isDead = false;
621
622 mapPlayers[p.id] = p;
623
624 break;
625 }
626 case MSG_TYPE_PLAYER_MOVE:
627 {
628 unsigned int id;
629 int x, y;
630
631 memcpy(&id, msg.buffer, 4);
632 memcpy(&x, msg.buffer+4, 4);
633 memcpy(&y, msg.buffer+8, 4);
634
635 mapPlayers[id].target.x = x;
636 mapPlayers[id].target.y = y;
637
638 break;
639 }
640 case MSG_TYPE_CHAT:
641 {
642 chatConsole.addLine(response);
643
644 break;
645 }
646 case MSG_TYPE_OBJECT:
647 {
648 cout << "Received object message in STATE_LOGIN." << endl;
649
650 WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
651 o.deserialize(msg.buffer);
652 gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
653
654 break;
655 }
656 case MSG_TYPE_REMOVE_OBJECT:
657 {
658 cout << "Received REMOVE_OBJECT message!" << endl;
659
660 int id;
661 memcpy(&id, msg.buffer, 4);
662
663 cout << "Removing object with id " << id << endl;
664
665 if (!gameMap->removeObject(id))
666 cout << "Did not remove the object" << endl;
667
668 break;
669 }
670 case MSG_TYPE_SCORE:
671 {
672 memcpy(&scoreBlue, msg.buffer, 4);
673 memcpy(&scoreRed, msg.buffer+4, 4);
674
675 break;
676 }
677 case MSG_TYPE_ATTACK:
678 {
679 cout << "Received ATTACK message" << endl;
680
681 break;
682 }
683 case MSG_TYPE_START_ATTACK:
684 {
685 cout << "Received START_ATTACK message" << endl;
686
687 unsigned int id, targetID;
688 memcpy(&id, msg.buffer, 4);
689 memcpy(&targetID, msg.buffer+4, 4);
690
691 cout << "source id: " << id << endl;
692 cout << "target id: " << targetID << endl;
693
694 Player* source = &mapPlayers[id];
695 source->targetPlayer = targetID;
696 source->isChasing = true;
697
698 break;
699 }
700 case MSG_TYPE_PROJECTILE:
701 {
702 cout << "Received a PROJECTILE message" << endl;
703
704 int id, x, y, targetId;
705
706 memcpy(&id, msg.buffer, 4);
707 memcpy(&x, msg.buffer+4, 4);
708 memcpy(&y, msg.buffer+8, 4);
709 memcpy(&targetId, msg.buffer+12, 4);
710
711 cout << "id: " << id << endl;
712 cout << "x: " << x << endl;
713 cout << "y: " << y << endl;
714 cout << "Target: " << targetId << endl;
715
716 Projectile proj(x, y, targetId, 0);
717 proj.setId(id);
718
719 mapProjectiles[id] = proj;
720
721 break;
722 }
723 case MSG_TYPE_REMOVE_PROJECTILE:
724 {
725 cout << "Received a REMOVE_PROJECTILE message" << endl;
726
727 int id;
728
729 memcpy(&id, msg.buffer, 4);
730
731 mapProjectiles.erase(id);
732
733 break;
734 }
735 default:
736 {
737 cout << "Received an unexpected message type: " << msg.type << endl;
738 }
739 }
740
741 break;
742 }
743 default:
744 {
745 cout << "The state has an invalid value: " << state << endl;
746
747 break;
748 }
749 }
750}
751
752// this should probably be in the WorldMap class
753void drawMap(WorldMap* gameMap)
754{
755 POSITION mapPos;
756 mapPos.x = 0;
757 mapPos.y = 0;
758 mapPos = mapToScreen(mapPos);
759
760 for (int x=0; x<gameMap->width; x++)
761 {
762 for (int y=0; y<gameMap->height; y++)
763 {
764 WorldMap::TerrainType el = gameMap->getElement(x, y);
765 WorldMap::StructureType structure = gameMap->getStructure(x, y);
766
767 if (el == WorldMap::TERRAIN_GRASS)
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, 255, 0));
769 else if (el == WorldMap::TERRAIN_OCEAN)
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(0, 0, 255));
771 else if (el == WorldMap::TERRAIN_ROCK)
772 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));
773
774 if (structure == WorldMap::STRUCTURE_BLUE_FLAG) {
775 al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
776 //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));
777 }else if (structure == WorldMap::STRUCTURE_RED_FLAG) {
778 al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
779 //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));
780 }
781 }
782 }
783
784 for (int x=0; x<gameMap->width; x++)
785 {
786 for (int y=0; y<gameMap->height; y++)
787 {
788 vector<WorldMap::Object> vctObjects = gameMap->getObjects(x, y);
789
790 vector<WorldMap::Object>::iterator it;
791 for(it = vctObjects.begin(); it != vctObjects.end(); it++) {
792 switch(it->type) {
793 case WorldMap::OBJECT_BLUE_FLAG:
794 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));
795 break;
796 case WorldMap::OBJECT_RED_FLAG:
797 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));
798 break;
799 }
800 }
801 }
802 }
803}
804
805void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId)
806{
807 map<unsigned int, Player>::iterator it;
808
809 Player* p;
810 POSITION pos;
811 ALLEGRO_COLOR color;
812
813 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
814 {
815 p = &it->second;
816
817 if (p->isDead)
818 continue;
819
820 pos = mapToScreen(p->pos);
821
822 if (p->id == curPlayerId)
823 al_draw_filled_circle(pos.x, pos.y, 14, al_map_rgb(0, 0, 0));
824
825 if (p->team == 0)
826 color = al_map_rgb(0, 0, 255);
827 else if (p->team == 1)
828 color = al_map_rgb(255, 0, 0);
829
830 al_draw_filled_circle(pos.x, pos.y, 12, color);
831
832 // draw player class
833 int fontHeight = al_get_font_line_height(font);
834
835 string strClass;
836 switch (p->playerClass) {
837 case Player::CLASS_WARRIOR:
838 strClass = "W";
839 break;
840 case Player::CLASS_RANGER:
841 strClass = "R";
842 break;
843 case Player::CLASS_NONE:
844 strClass = "";
845 break;
846 default:
847 strClass = "";
848 break;
849 }
850 al_draw_text(font, al_map_rgb(0, 0, 0), pos.x, pos.y-fontHeight/2, ALLEGRO_ALIGN_CENTRE, strClass.c_str());
851
852 // draw player health
853 al_draw_filled_rectangle(pos.x-12, pos.y-24, pos.x+12, pos.y-16, al_map_rgb(0, 0, 0));
854 if (it->second.maxHealth != 0)
855 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));
856
857 if (p->hasBlueFlag)
858 al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(0, 0, 255));
859 else if (p->hasRedFlag)
860 al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(255, 0, 0));
861 }
862}
863
864void goToRegisterScreen()
865{
866 wndCurrent = wndRegister;
867
868 txtUsernameRegister->clear();
869 txtPasswordRegister->clear();
870}
871
872void goToLoginScreen()
873{
874 wndCurrent = wndLogin;
875
876 txtUsername->clear();
877 txtPassword->clear();
878}
879
880void registerAccount()
881{
882 string username = txtUsernameRegister->getStr();
883 string password = txtPasswordRegister->getStr();
884
885 txtUsernameRegister->clear();
886 txtPasswordRegister->clear();
887 // maybe clear rblClasses as well (add a method to RadioButtonList to enable this)
888
889 Player::PlayerClass playerClass;
890
891 switch (rblClasses->getSelectedButton()) {
892 case 0:
893 playerClass = Player::CLASS_WARRIOR;
894 break;
895 case 1:
896 playerClass = Player::CLASS_RANGER;
897 break;
898 default:
899 cout << "Invalid class selection" << endl;
900 playerClass = Player::CLASS_NONE;
901 break;
902 }
903
904 msgTo.type = MSG_TYPE_REGISTER;
905
906 strcpy(msgTo.buffer, username.c_str());
907 strcpy(msgTo.buffer+username.size()+1, password.c_str());
908 memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
909
910 sendMessage(&msgTo, sock, &server);
911}
912
913void login()
914{
915 string strUsername = txtUsername->getStr();
916 string strPassword = txtPassword->getStr();
917 username = strUsername;
918
919 txtUsername->clear();
920 txtPassword->clear();
921
922 msgTo.type = MSG_TYPE_LOGIN;
923
924 strcpy(msgTo.buffer, strUsername.c_str());
925 strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
926
927 sendMessage(&msgTo, sock, &server);
928}
929
930void logout()
931{
932 txtChat->clear();
933
934 msgTo.type = MSG_TYPE_LOGOUT;
935
936 strcpy(msgTo.buffer, username.c_str());
937
938 sendMessage(&msgTo, sock, &server);
939}
940
941void quit()
942{
943 doexit = true;
944}
945
946void sendChatMessage()
947{
948 string msg = txtChat->getStr();
949
950 txtChat->clear();
951
952 msgTo.type = MSG_TYPE_CHAT;
953
954 strcpy(msgTo.buffer, msg.c_str());
955
956 sendMessage(&msgTo, sock, &server);
957}
Note: See TracBrowser for help on using the repository browser.