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
RevLine 
[4c202e0]1#include "../../common/Compiler.h"
2
[e08572c]3#if defined WINDOWS
[0dde5da]4 #include <winsock2.h>
5 #include <WS2tcpip.h>
[e08572c]6#elif defined LINUX
[0dde5da]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>
[a845faf]13#endif
[1912323]14
[88cdae2]15#include <cstdio>
16#include <cstdlib>
[8c74150]17#include <cmath>
18#include <sys/types.h>
[a845faf]19#include <string>
[1912323]20#include <iostream>
[88cdae2]21#include <sstream>
[3a79253]22#include <map>
23
[d352805]24#include <allegro5/allegro.h>
25#include <allegro5/allegro_font.h>
26#include <allegro5/allegro_ttf.h>
[88cdae2]27#include <allegro5/allegro_primitives.h>
[7d7df47]28
[b53c6b3]29#include "../../common/Message.h"
[e607c0f]30#include "../../common/Common.h"
[62ee2ce]31#include "../../common/WorldMap.h"
[4c202e0]32#include "../../common/Player.h"
[fbcfc35]33#include "../../common/Projectile.h"
[7d7df47]34
[87b3ee2]35#include "Window.h"
36#include "Textbox.h"
37#include "Button.h"
[5c95436]38#include "RadioButtonList.h"
[6475138]39#include "chat.h"
40
[a845faf]41#ifdef WINDOWS
[6475138]42 #pragma comment(lib, "ws2_32.lib")
[a845faf]43#endif
[1912323]44
45using namespace std;
46
[0dde5da]47void initWinSock();
48void shutdownWinSock();
[fbcfc35]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);
[62ee2ce]50void drawMap(WorldMap* gameMap);
[d09fe76]51void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId);
[62ee2ce]52POSITION screenToMap(POSITION pos);
53POSITION mapToScreen(POSITION pos);
[87b3ee2]54
55// callbacks
[5c95436]56void goToLoginScreen();
57void goToRegisterScreen();
[87b3ee2]58void registerAccount();
59void login();
60void logout();
61void quit();
62void sendChatMessage();
[4da5aa3]63
[1912323]64void error(const char *);
[7d7df47]65
[d352805]66const float FPS = 60;
67const int SCREEN_W = 640;
68const int SCREEN_H = 480;
[0cc431d]69
70enum STATE {
71 STATE_START,
[87b3ee2]72 STATE_LOGIN // this means you're already logged in
[d352805]73};
[87b3ee2]74
75int state;
76
77bool doexit;
78
79Window* wndLogin;
[5c95436]80Window* wndRegister;
[87b3ee2]81Window* wndMain;
82Window* wndCurrent;
83
[5c95436]84// wndLogin
[87b3ee2]85Textbox* txtUsername;
86Textbox* txtPassword;
[5c95436]87
88// wndRegister
89Textbox* txtUsernameRegister;
90Textbox* txtPasswordRegister;
91RadioButtonList* rblClasses;
92
93// wndMain
[87b3ee2]94Textbox* txtChat;
95
96int sock;
97struct sockaddr_in server, from;
98struct hostent *hp;
99NETWORK_MSG msgTo, msgFrom;
100string username;
101chat chatConsole;
[d352805]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;
[87b3ee2]110 doexit = false;
[eb8adb1]111 map<unsigned int, Player> mapPlayers;
[fbcfc35]112 map<unsigned int, Projectile> mapProjectiles;
[88cdae2]113 unsigned int curPlayerId = -1;
[15efb4e]114 int scoreBlue, scoreRed;
115
116 scoreBlue = 0;
117 scoreRed = 0;
[9a3e6b1]118
[87b3ee2]119 state = STATE_START;
[9a3e6b1]120
[d352805]121 if(!al_init()) {
122 fprintf(stderr, "failed to initialize allegro!\n");
123 return -1;
124 }
125
[88cdae2]126 if (al_init_primitives_addon())
127 cout << "Primitives initialized" << endl;
128 else
129 cout << "Primitives not initialized" << endl;
130
[d352805]131 al_init_font_addon();
132 al_init_ttf_addon();
133
[88cdae2]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
[d352805]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 }
[87b3ee2]150
151 if(!al_install_mouse()) {
152 fprintf(stderr, "failed to initialize the mouse!\n");
153 return -1;
154 }
[d352805]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 }
[87b3ee2]168
[384b7e0]169 WorldMap* gameMap = WorldMap::loadMapFromFile("../../data/map.txt");
[62ee2ce]170
[87b3ee2]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));
[5c95436]174 wndLogin->addComponent(new Button(22, 100, 90, 20, font, "Create an Account", goToRegisterScreen));
[87b3ee2]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
[5c95436]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
[87b3ee2]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;
[d352805]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());
[87b3ee2]218 al_register_event_source(event_queue, al_get_mouse_event_source());
[d352805]219
220 al_clear_to_color(al_map_rgb(0,0,0));
221
222 al_flip_display();
[9a3e6b1]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
[e607c0f]235 set_nonblock(sock);
236
[9a3e6b1]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
[d352805]245 al_start_timer(timer);
[e607c0f]246
[d352805]247 while(!doexit)
248 {
249 ALLEGRO_EVENT ev;
[3d81c0d]250
[d352805]251 al_wait_for_event(event_queue, &ev);
[87b3ee2]252
253 if(wndCurrent->handleEvent(ev)) {
254 // do nothing
255 }
256 else if(ev.type == ALLEGRO_EVENT_TIMER) {
[a1a3bd5]257 redraw = true; // seems like we should just call a draw function here instead
[d352805]258 }
259 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
[9a3e6b1]260 doexit = true;
[d352805]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;
[4926168]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;
[626e5b0]276 case ALLEGRO_KEY_D: // drop the current item
277 if (state == STATE_LOGIN) {
[4926168]278 // find the current player in the player list
[626e5b0]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;
[d352805]303 }
304 }
[88cdae2]305 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
[ad5d122]306 if(wndCurrent == wndMain) {
[e1f78f5]307 if (ev.mouse.button == 1) { // left click
308 msgTo.type = MSG_TYPE_PLAYER_MOVE;
[88cdae2]309
[e1f78f5]310 POSITION pos;
311 pos.x = ev.mouse.x;
312 pos.y = ev.mouse.y;
313 pos = screenToMap(pos);
[62ee2ce]314
[e1f78f5]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
[fbcfc35]328 cout << "Detected a right-click" << endl;
329
[e1f78f5]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 {
[fbcfc35]340 // need to check if the right-click was actually on this player
[e1f78f5]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);
[88cdae2]346
[e1f78f5]347 sendMessage(&msgTo, sock, &server);
348 }
349 }
[62ee2ce]350 }
[ad5d122]351 }
[88cdae2]352 }
[e607c0f]353
354 if (receiveMessage(&msgFrom, sock, &from) >= 0)
[fbcfc35]355 processMessage(msgFrom, state, chatConsole, gameMap, mapPlayers, mapProjectiles, curPlayerId, scoreBlue, scoreRed);
[054b50b]356
[a1a3bd5]357 if (redraw)
[e607c0f]358 {
[d352805]359 redraw = false;
[88cdae2]360
[87b3ee2]361 wndCurrent->draw(display);
[9a3e6b1]362
[6475138]363 chatConsole.draw(font, al_map_rgb(255,255,255));
[9a3e6b1]364
[87b3ee2]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:");
[62ee2ce]371
[15efb4e]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
[032e550]380 // update players
[a1a3bd5]381 map<unsigned int, Player>::iterator it;
[032e550]382 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
383 {
384 it->second.updateTarget(mapPlayers);
385 }
386
[a1a3bd5]387 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
388 {
[227baaa]389 it->second.move(gameMap); // ignore return value
[a1a3bd5]390 }
391
[8c74150]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
[62ee2ce]399 drawMap(gameMap);
[d09fe76]400 drawPlayers(mapPlayers, font, curPlayerId);
[8c74150]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 }
[87b3ee2]421 }
422
[d352805]423 al_flip_display();
424 }
425 }
[9a3e6b1]426
427 #if defined WINDOWS
428 closesocket(sock);
429 #elif defined LINUX
430 close(sock);
431 #endif
432
433 shutdownWinSock();
[d352805]434
[87b3ee2]435 delete wndLogin;
436 delete wndMain;
437
[62ee2ce]438 delete gameMap;
439
[d352805]440 al_destroy_event_queue(event_queue);
441 al_destroy_display(display);
442 al_destroy_timer(timer);
443
444 return 0;
445}
446
[4da5aa3]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
[0dde5da]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
[1912323]478}
479
[62ee2ce]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
[ca44f82]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
[fbcfc35]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)
[1912323]512{
[4da5aa3]513 string response = string(msg.buffer);
514
515 switch(state)
516 {
517 case STATE_START:
518 {
[e607c0f]519 cout << "In STATE_START" << endl;
520
[87b3ee2]521 switch(msg.type)
[4da5aa3]522 {
[87b3ee2]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;
[88cdae2]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;
[a1a3bd5]551 cout << "map size: " << mapPlayers.size() << endl;
[87b3ee2]552 }
[88cdae2]553
[a1a3bd5]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
[5a5f131]562 mapPlayers[p.id] = p;
[a1a3bd5]563
[45b2750]564 break;
565 }
566 case MSG_TYPE_OBJECT:
567 {
568 WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
569 o.deserialize(msg.buffer);
[7511a2b]570 cout << "object id: " << o.id << endl;
[45b2750]571 gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
572
[87b3ee2]573 break;
574 }
[4da5aa3]575 }
576
577 break;
578 }
579 case STATE_LOGIN:
580 {
[eb8adb1]581 switch(msg.type)
[4da5aa3]582 {
[87b3ee2]583 case MSG_TYPE_REGISTER:
584 {
585 break;
586 }
587 case MSG_TYPE_LOGIN:
588 {
[a1a3bd5]589 cout << "Got a login message" << endl;
590
[eb8adb1]591 chatConsole.addLine(response);
[a1a3bd5]592 cout << "Added new line" << endl;
[eb8adb1]593
[a1a3bd5]594 break;
595 }
596 case MSG_TYPE_LOGOUT:
597 {
[054b50b]598 cout << "Got a logout message" << endl;
[a1a3bd5]599
[87b3ee2]600 if (response.compare("You have successfully logged out.") == 0)
601 {
602 cout << "Logged out" << endl;
603 state = STATE_START;
604 wndCurrent = wndLogin;
605 }
[054b50b]606
607 break;
608 }
[4c202e0]609 case MSG_TYPE_PLAYER:
610 {
[60776f2]611 Player p("", "");
612 p.deserialize(msg.buffer);
[054b50b]613 p.timeLastUpdated = getCurrentMillis();
[032e550]614 p.isChasing = false;
[5a5f131]615 if (p.health <= 0)
616 p.isDead = true;
617 else
618 p.isDead = false;
619
[3a79253]620 mapPlayers[p.id] = p;
[4c202e0]621
[eb8adb1]622 break;
623 }
[a1a3bd5]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 }
[eb8adb1]638 case MSG_TYPE_CHAT:
639 {
640 chatConsole.addLine(response);
[4c202e0]641
[87b3ee2]642 break;
643 }
[45b2750]644 case MSG_TYPE_OBJECT:
645 {
[7511a2b]646 cout << "Received object message in STATE_LOGIN." << endl;
[45b2750]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 }
[2df63d6]654 case MSG_TYPE_REMOVE_OBJECT:
655 {
[7511a2b]656 cout << "Received REMOVE_OBJECT message!" << endl;
657
[2df63d6]658 int id;
659 memcpy(&id, msg.buffer, 4);
[7511a2b]660
661 cout << "Removing object with id " << id << endl;
662
[2df63d6]663 if (!gameMap->removeObject(id))
664 cout << "Did not remove the object" << endl;
[7511a2b]665
666 break;
[2df63d6]667 }
[15efb4e]668 case MSG_TYPE_SCORE:
669 {
670 memcpy(&scoreBlue, msg.buffer, 4);
671 memcpy(&scoreRed, msg.buffer+4, 4);
672
673 break;
674 }
[b978503]675 case MSG_TYPE_ATTACK:
676 {
[032e550]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
[b978503]696 break;
697 }
698 case MSG_TYPE_PROJECTILE:
699 {
[8c74150]700 cout << "Received a PROJECTILE message" << endl;
[fbcfc35]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
[8c74150]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;
[fbcfc35]718
[b978503]719 break;
720 }
721 case MSG_TYPE_REMOVE_PROJECTILE:
722 {
[8c74150]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
[b978503]731 break;
732 }
[45b2750]733 default:
734 {
735 cout << "Received an unexpected message type: " << msg.type << endl;
736 }
[4da5aa3]737 }
[eb8adb1]738
[4da5aa3]739 break;
740 }
741 default:
742 {
743 cout << "The state has an invalid value: " << state << endl;
744
745 break;
746 }
747 }
[0dde5da]748}
[87b3ee2]749
[d436ac4]750// this should probably be in the WorldMap class
[62ee2ce]751void drawMap(WorldMap* gameMap)
752{
753 POSITION mapPos;
754 mapPos.x = 0;
755 mapPos.y = 0;
756 mapPos = mapToScreen(mapPos);
[6e66ffd]757
[62ee2ce]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);
[cc1c6c1]763 WorldMap::StructureType structure = gameMap->getStructure(x, y);
[62ee2ce]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));
[a1a3bd5]771
[6e66ffd]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 }
[62ee2ce]799 }
800 }
801}
802
[d09fe76]803void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId)
[88cdae2]804{
805 map<unsigned int, Player>::iterator it;
806
[62ee2ce]807 Player* p;
808 POSITION pos;
[d09fe76]809 ALLEGRO_COLOR color;
[62ee2ce]810
[88cdae2]811 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
812 {
[62ee2ce]813 p = &it->second;
[5a5f131]814
815 if (p->isDead)
816 continue;
817
[62ee2ce]818 pos = mapToScreen(p->pos);
[88cdae2]819
[7efed11]820 if (p->id == curPlayerId)
[a6066e8]821 al_draw_filled_circle(pos.x, pos.y, 14, al_map_rgb(0, 0, 0));
822
823 if (p->team == 0)
[d09fe76]824 color = al_map_rgb(0, 0, 255);
[a6066e8]825 else if (p->team == 1)
[d09fe76]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)
[e1f78f5]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));
[7efed11]854
[7d91bbe]855 if (p->hasBlueFlag)
[7efed11]856 al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(0, 0, 255));
[a6066e8]857 else if (p->hasRedFlag)
[7efed11]858 al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(255, 0, 0));
[88cdae2]859 }
860}
861
[5c95436]862void goToRegisterScreen()
863{
864 wndCurrent = wndRegister;
865
866 txtUsernameRegister->clear();
867 txtPasswordRegister->clear();
868}
869
870void goToLoginScreen()
[87b3ee2]871{
[5c95436]872 wndCurrent = wndLogin;
[87b3ee2]873
874 txtUsername->clear();
875 txtPassword->clear();
[5c95436]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 }
[87b3ee2]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());
[5c95436]906 memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
[87b3ee2]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.