source: network-game/client/Client/main.cpp@ 9b1e12c

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

Increased the size of the client window to 1024x768 and moved around some gui elements to fit the new size

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