[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 |
|
---|
| 45 | using namespace std;
|
---|
| 46 |
|
---|
[0dde5da] | 47 | void initWinSock();
|
---|
| 48 | void shutdownWinSock();
|
---|
[fbcfc35] | 49 | void 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] | 50 | void drawMap(WorldMap* gameMap);
|
---|
[d09fe76] | 51 | void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId);
|
---|
[62ee2ce] | 52 | POSITION screenToMap(POSITION pos);
|
---|
| 53 | POSITION mapToScreen(POSITION pos);
|
---|
[87b3ee2] | 54 |
|
---|
| 55 | // callbacks
|
---|
[5c95436] | 56 | void goToLoginScreen();
|
---|
| 57 | void goToRegisterScreen();
|
---|
[87b3ee2] | 58 | void registerAccount();
|
---|
| 59 | void login();
|
---|
| 60 | void logout();
|
---|
| 61 | void quit();
|
---|
| 62 | void sendChatMessage();
|
---|
[4da5aa3] | 63 |
|
---|
[1912323] | 64 | void error(const char *);
|
---|
[7d7df47] | 65 |
|
---|
[d352805] | 66 | const float FPS = 60;
|
---|
[9b1e12c] | 67 | const int SCREEN_W = 1024;
|
---|
| 68 | const int SCREEN_H = 768;
|
---|
[0cc431d] | 69 |
|
---|
| 70 | enum STATE {
|
---|
| 71 | STATE_START,
|
---|
[87b3ee2] | 72 | STATE_LOGIN // this means you're already logged in
|
---|
[d352805] | 73 | };
|
---|
[87b3ee2] | 74 |
|
---|
| 75 | int state;
|
---|
| 76 |
|
---|
| 77 | bool doexit;
|
---|
| 78 |
|
---|
| 79 | Window* wndLogin;
|
---|
[5c95436] | 80 | Window* wndRegister;
|
---|
[87b3ee2] | 81 | Window* wndMain;
|
---|
| 82 | Window* wndCurrent;
|
---|
| 83 |
|
---|
[5c95436] | 84 | // wndLogin
|
---|
[87b3ee2] | 85 | Textbox* txtUsername;
|
---|
| 86 | Textbox* txtPassword;
|
---|
[5c95436] | 87 |
|
---|
| 88 | // wndRegister
|
---|
| 89 | Textbox* txtUsernameRegister;
|
---|
| 90 | Textbox* txtPasswordRegister;
|
---|
| 91 | RadioButtonList* rblClasses;
|
---|
| 92 |
|
---|
| 93 | // wndMain
|
---|
[87b3ee2] | 94 | Textbox* txtChat;
|
---|
| 95 |
|
---|
| 96 | int sock;
|
---|
| 97 | struct sockaddr_in server, from;
|
---|
| 98 | struct hostent *hp;
|
---|
| 99 | NETWORK_MSG msgTo, msgFrom;
|
---|
| 100 | string username;
|
---|
| 101 | chat chatConsole;
|
---|
[d352805] | 102 |
|
---|
| 103 | int 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 |
|
---|
[9b1e12c] | 162 | display = al_create_display(SCREEN_W, SCREEN_H);
|
---|
[d352805] | 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);
|
---|
[9b1e12c] | 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));
|
---|
[87b3ee2] | 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);
|
---|
[9b1e12c] | 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));
|
---|
[5c95436] | 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);
|
---|
[9b1e12c] | 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));
|
---|
[87b3ee2] | 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
|
---|
[f3cf1a5] | 341 | // right now, this code will target all players other than the current one
|
---|
[e1f78f5] | 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);
|
---|
[88cdae2] | 347 |
|
---|
[e1f78f5] | 348 | sendMessage(&msgTo, sock, &server);
|
---|
| 349 | }
|
---|
| 350 | }
|
---|
[62ee2ce] | 351 | }
|
---|
[ad5d122] | 352 | }
|
---|
[88cdae2] | 353 | }
|
---|
[e607c0f] | 354 |
|
---|
| 355 | if (receiveMessage(&msgFrom, sock, &from) >= 0)
|
---|
[fbcfc35] | 356 | processMessage(msgFrom, state, chatConsole, gameMap, mapPlayers, mapProjectiles, curPlayerId, scoreBlue, scoreRed);
|
---|
[054b50b] | 357 |
|
---|
[a1a3bd5] | 358 | if (redraw)
|
---|
[e607c0f] | 359 | {
|
---|
[d352805] | 360 | redraw = false;
|
---|
[88cdae2] | 361 |
|
---|
[87b3ee2] | 362 | wndCurrent->draw(display);
|
---|
[9a3e6b1] | 363 |
|
---|
[6475138] | 364 | chatConsole.draw(font, al_map_rgb(255,255,255));
|
---|
[9a3e6b1] | 365 |
|
---|
[109e8a3] | 366 | // There should be label gui components that show these or each textbox should have a label
|
---|
| 367 | if(wndCurrent == wndLogin || wndCurrent == wndRegister) {
|
---|
[9b1e12c] | 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:");
|
---|
[87b3ee2] | 370 | }
|
---|
| 371 | else if(wndCurrent == wndMain) {
|
---|
| 372 | al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
|
---|
[62ee2ce] | 373 |
|
---|
[15efb4e] | 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 |
|
---|
[032e550] | 382 | // update players
|
---|
[a1a3bd5] | 383 | map<unsigned int, Player>::iterator it;
|
---|
[032e550] | 384 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 385 | {
|
---|
| 386 | it->second.updateTarget(mapPlayers);
|
---|
| 387 | }
|
---|
| 388 |
|
---|
[a1a3bd5] | 389 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 390 | {
|
---|
[227baaa] | 391 | it->second.move(gameMap); // ignore return value
|
---|
[a1a3bd5] | 392 | }
|
---|
| 393 |
|
---|
[8c74150] | 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 |
|
---|
[62ee2ce] | 401 | drawMap(gameMap);
|
---|
[d09fe76] | 402 | drawPlayers(mapPlayers, font, curPlayerId);
|
---|
[8c74150] | 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 | }
|
---|
[87b3ee2] | 423 | }
|
---|
| 424 |
|
---|
[d352805] | 425 | al_flip_display();
|
---|
| 426 | }
|
---|
| 427 | }
|
---|
[9a3e6b1] | 428 |
|
---|
| 429 | #if defined WINDOWS
|
---|
| 430 | closesocket(sock);
|
---|
| 431 | #elif defined LINUX
|
---|
| 432 | close(sock);
|
---|
| 433 | #endif
|
---|
| 434 |
|
---|
| 435 | shutdownWinSock();
|
---|
[d352805] | 436 |
|
---|
[87b3ee2] | 437 | delete wndLogin;
|
---|
| 438 | delete wndMain;
|
---|
| 439 |
|
---|
[62ee2ce] | 440 | delete gameMap;
|
---|
| 441 |
|
---|
[d352805] | 442 | al_destroy_event_queue(event_queue);
|
---|
| 443 | al_destroy_display(display);
|
---|
| 444 | al_destroy_timer(timer);
|
---|
| 445 |
|
---|
| 446 | return 0;
|
---|
| 447 | }
|
---|
| 448 |
|
---|
[4da5aa3] | 449 | // need to make a function like this that works on windows
|
---|
| 450 | void error(const char *msg)
|
---|
| 451 | {
|
---|
| 452 | perror(msg);
|
---|
| 453 | shutdownWinSock();
|
---|
| 454 | exit(1);
|
---|
| 455 | }
|
---|
| 456 |
|
---|
[0dde5da] | 457 | void 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 |
|
---|
| 475 | void shutdownWinSock()
|
---|
| 476 | {
|
---|
| 477 | #if defined WINDOWS
|
---|
| 478 | WSACleanup();
|
---|
| 479 | #endif
|
---|
[1912323] | 480 | }
|
---|
| 481 |
|
---|
[62ee2ce] | 482 | POSITION 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 |
|
---|
| 496 | POSITION mapToScreen(POSITION pos)
|
---|
| 497 | {
|
---|
| 498 | pos.x = pos.x+300;
|
---|
| 499 | pos.y = pos.y+100;
|
---|
| 500 |
|
---|
| 501 | return pos;
|
---|
| 502 | }
|
---|
| 503 |
|
---|
[ca44f82] | 504 | POSITION 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 |
|
---|
[fbcfc35] | 513 | void 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] | 514 | {
|
---|
[4da5aa3] | 515 | string response = string(msg.buffer);
|
---|
| 516 |
|
---|
| 517 | switch(state)
|
---|
| 518 | {
|
---|
| 519 | case STATE_START:
|
---|
| 520 | {
|
---|
[e607c0f] | 521 | cout << "In STATE_START" << endl;
|
---|
| 522 |
|
---|
[87b3ee2] | 523 | switch(msg.type)
|
---|
[4da5aa3] | 524 | {
|
---|
[87b3ee2] | 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;
|
---|
[88cdae2] | 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;
|
---|
[a1a3bd5] | 553 | cout << "map size: " << mapPlayers.size() << endl;
|
---|
[87b3ee2] | 554 | }
|
---|
[88cdae2] | 555 |
|
---|
[a1a3bd5] | 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 |
|
---|
[5a5f131] | 564 | mapPlayers[p.id] = p;
|
---|
[a1a3bd5] | 565 |
|
---|
[45b2750] | 566 | break;
|
---|
| 567 | }
|
---|
| 568 | case MSG_TYPE_OBJECT:
|
---|
| 569 | {
|
---|
| 570 | WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
|
---|
| 571 | o.deserialize(msg.buffer);
|
---|
[7511a2b] | 572 | cout << "object id: " << o.id << endl;
|
---|
[45b2750] | 573 | gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
|
---|
| 574 |
|
---|
[87b3ee2] | 575 | break;
|
---|
| 576 | }
|
---|
[4da5aa3] | 577 | }
|
---|
| 578 |
|
---|
| 579 | break;
|
---|
| 580 | }
|
---|
| 581 | case STATE_LOGIN:
|
---|
| 582 | {
|
---|
[eb8adb1] | 583 | switch(msg.type)
|
---|
[4da5aa3] | 584 | {
|
---|
[87b3ee2] | 585 | case MSG_TYPE_REGISTER:
|
---|
| 586 | {
|
---|
| 587 | break;
|
---|
| 588 | }
|
---|
| 589 | case MSG_TYPE_LOGIN:
|
---|
| 590 | {
|
---|
[a1a3bd5] | 591 | cout << "Got a login message" << endl;
|
---|
| 592 |
|
---|
[eb8adb1] | 593 | chatConsole.addLine(response);
|
---|
[a1a3bd5] | 594 | cout << "Added new line" << endl;
|
---|
[eb8adb1] | 595 |
|
---|
[a1a3bd5] | 596 | break;
|
---|
| 597 | }
|
---|
| 598 | case MSG_TYPE_LOGOUT:
|
---|
| 599 | {
|
---|
[054b50b] | 600 | cout << "Got a logout message" << endl;
|
---|
[a1a3bd5] | 601 |
|
---|
[87b3ee2] | 602 | if (response.compare("You have successfully logged out.") == 0)
|
---|
| 603 | {
|
---|
| 604 | cout << "Logged out" << endl;
|
---|
| 605 | state = STATE_START;
|
---|
| 606 | wndCurrent = wndLogin;
|
---|
| 607 | }
|
---|
[054b50b] | 608 |
|
---|
| 609 | break;
|
---|
| 610 | }
|
---|
[4c202e0] | 611 | case MSG_TYPE_PLAYER:
|
---|
| 612 | {
|
---|
[60776f2] | 613 | Player p("", "");
|
---|
| 614 | p.deserialize(msg.buffer);
|
---|
[054b50b] | 615 | p.timeLastUpdated = getCurrentMillis();
|
---|
[032e550] | 616 | p.isChasing = false;
|
---|
[5a5f131] | 617 | if (p.health <= 0)
|
---|
| 618 | p.isDead = true;
|
---|
| 619 | else
|
---|
| 620 | p.isDead = false;
|
---|
| 621 |
|
---|
[3a79253] | 622 | mapPlayers[p.id] = p;
|
---|
[4c202e0] | 623 |
|
---|
[eb8adb1] | 624 | break;
|
---|
| 625 | }
|
---|
[a1a3bd5] | 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 | }
|
---|
[eb8adb1] | 640 | case MSG_TYPE_CHAT:
|
---|
| 641 | {
|
---|
| 642 | chatConsole.addLine(response);
|
---|
[4c202e0] | 643 |
|
---|
[87b3ee2] | 644 | break;
|
---|
| 645 | }
|
---|
[45b2750] | 646 | case MSG_TYPE_OBJECT:
|
---|
| 647 | {
|
---|
[7511a2b] | 648 | cout << "Received object message in STATE_LOGIN." << endl;
|
---|
[45b2750] | 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 | }
|
---|
[2df63d6] | 656 | case MSG_TYPE_REMOVE_OBJECT:
|
---|
| 657 | {
|
---|
[7511a2b] | 658 | cout << "Received REMOVE_OBJECT message!" << endl;
|
---|
| 659 |
|
---|
[2df63d6] | 660 | int id;
|
---|
| 661 | memcpy(&id, msg.buffer, 4);
|
---|
[7511a2b] | 662 |
|
---|
| 663 | cout << "Removing object with id " << id << endl;
|
---|
| 664 |
|
---|
[2df63d6] | 665 | if (!gameMap->removeObject(id))
|
---|
| 666 | cout << "Did not remove the object" << endl;
|
---|
[7511a2b] | 667 |
|
---|
| 668 | break;
|
---|
[2df63d6] | 669 | }
|
---|
[15efb4e] | 670 | case MSG_TYPE_SCORE:
|
---|
| 671 | {
|
---|
| 672 | memcpy(&scoreBlue, msg.buffer, 4);
|
---|
| 673 | memcpy(&scoreRed, msg.buffer+4, 4);
|
---|
| 674 |
|
---|
| 675 | break;
|
---|
| 676 | }
|
---|
[b978503] | 677 | case MSG_TYPE_ATTACK:
|
---|
| 678 | {
|
---|
[032e550] | 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 |
|
---|
[b978503] | 698 | break;
|
---|
| 699 | }
|
---|
| 700 | case MSG_TYPE_PROJECTILE:
|
---|
| 701 | {
|
---|
[8c74150] | 702 | cout << "Received a PROJECTILE message" << endl;
|
---|
[fbcfc35] | 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 |
|
---|
[8c74150] | 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;
|
---|
[fbcfc35] | 720 |
|
---|
[b978503] | 721 | break;
|
---|
| 722 | }
|
---|
| 723 | case MSG_TYPE_REMOVE_PROJECTILE:
|
---|
| 724 | {
|
---|
[8c74150] | 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 |
|
---|
[b978503] | 733 | break;
|
---|
| 734 | }
|
---|
[45b2750] | 735 | default:
|
---|
| 736 | {
|
---|
| 737 | cout << "Received an unexpected message type: " << msg.type << endl;
|
---|
| 738 | }
|
---|
[4da5aa3] | 739 | }
|
---|
[eb8adb1] | 740 |
|
---|
[4da5aa3] | 741 | break;
|
---|
| 742 | }
|
---|
| 743 | default:
|
---|
| 744 | {
|
---|
| 745 | cout << "The state has an invalid value: " << state << endl;
|
---|
| 746 |
|
---|
| 747 | break;
|
---|
| 748 | }
|
---|
| 749 | }
|
---|
[0dde5da] | 750 | }
|
---|
[87b3ee2] | 751 |
|
---|
[d436ac4] | 752 | // this should probably be in the WorldMap class
|
---|
[62ee2ce] | 753 | void drawMap(WorldMap* gameMap)
|
---|
| 754 | {
|
---|
| 755 | POSITION mapPos;
|
---|
| 756 | mapPos.x = 0;
|
---|
| 757 | mapPos.y = 0;
|
---|
| 758 | mapPos = mapToScreen(mapPos);
|
---|
[6e66ffd] | 759 |
|
---|
[147f662] | 760 | for (int x=0; x<gameMap->width; x++)
|
---|
[62ee2ce] | 761 | {
|
---|
[147f662] | 762 | for (int y=0; y<gameMap->height; y++)
|
---|
[62ee2ce] | 763 | {
|
---|
| 764 | WorldMap::TerrainType el = gameMap->getElement(x, y);
|
---|
[cc1c6c1] | 765 | WorldMap::StructureType structure = gameMap->getStructure(x, y);
|
---|
[62ee2ce] | 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));
|
---|
[a1a3bd5] | 773 |
|
---|
[6e66ffd] | 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 |
|
---|
[f3cf1a5] | 784 | for (int x=0; x<gameMap->width; x++)
|
---|
[6e66ffd] | 785 | {
|
---|
[f3cf1a5] | 786 | for (int y=0; y<gameMap->height; y++)
|
---|
[6e66ffd] | 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 | }
|
---|
[62ee2ce] | 801 | }
|
---|
| 802 | }
|
---|
| 803 | }
|
---|
| 804 |
|
---|
[d09fe76] | 805 | void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId)
|
---|
[88cdae2] | 806 | {
|
---|
| 807 | map<unsigned int, Player>::iterator it;
|
---|
| 808 |
|
---|
[62ee2ce] | 809 | Player* p;
|
---|
| 810 | POSITION pos;
|
---|
[d09fe76] | 811 | ALLEGRO_COLOR color;
|
---|
[62ee2ce] | 812 |
|
---|
[88cdae2] | 813 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 814 | {
|
---|
[62ee2ce] | 815 | p = &it->second;
|
---|
[5a5f131] | 816 |
|
---|
| 817 | if (p->isDead)
|
---|
| 818 | continue;
|
---|
| 819 |
|
---|
[62ee2ce] | 820 | pos = mapToScreen(p->pos);
|
---|
[88cdae2] | 821 |
|
---|
[7efed11] | 822 | if (p->id == curPlayerId)
|
---|
[a6066e8] | 823 | al_draw_filled_circle(pos.x, pos.y, 14, al_map_rgb(0, 0, 0));
|
---|
| 824 |
|
---|
| 825 | if (p->team == 0)
|
---|
[d09fe76] | 826 | color = al_map_rgb(0, 0, 255);
|
---|
[a6066e8] | 827 | else if (p->team == 1)
|
---|
[d09fe76] | 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)
|
---|
[e1f78f5] | 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));
|
---|
[7efed11] | 856 |
|
---|
[7d91bbe] | 857 | if (p->hasBlueFlag)
|
---|
[7efed11] | 858 | al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(0, 0, 255));
|
---|
[a6066e8] | 859 | else if (p->hasRedFlag)
|
---|
[7efed11] | 860 | al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(255, 0, 0));
|
---|
[88cdae2] | 861 | }
|
---|
| 862 | }
|
---|
| 863 |
|
---|
[5c95436] | 864 | void goToRegisterScreen()
|
---|
| 865 | {
|
---|
| 866 | wndCurrent = wndRegister;
|
---|
| 867 |
|
---|
| 868 | txtUsernameRegister->clear();
|
---|
| 869 | txtPasswordRegister->clear();
|
---|
| 870 | }
|
---|
| 871 |
|
---|
| 872 | void goToLoginScreen()
|
---|
[87b3ee2] | 873 | {
|
---|
[5c95436] | 874 | wndCurrent = wndLogin;
|
---|
[87b3ee2] | 875 |
|
---|
| 876 | txtUsername->clear();
|
---|
| 877 | txtPassword->clear();
|
---|
[5c95436] | 878 | }
|
---|
| 879 |
|
---|
| 880 | void 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 | }
|
---|
[87b3ee2] | 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());
|
---|
[5c95436] | 908 | memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
|
---|
[87b3ee2] | 909 |
|
---|
| 910 | sendMessage(&msgTo, sock, &server);
|
---|
| 911 | }
|
---|
| 912 |
|
---|
| 913 | void 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 |
|
---|
| 930 | void 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 |
|
---|
| 941 | void quit()
|
---|
| 942 | {
|
---|
| 943 | doexit = true;
|
---|
| 944 | }
|
---|
| 945 |
|
---|
| 946 | void 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 | }
|
---|