Changeset e6c26b8 in network-game
- Timestamp:
- Oct 1, 2013, 8:08:24 PM (11 years ago)
- Branches:
- master
- Children:
- 95ffe57
- Parents:
- 373089e
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
client/Client/GameRender.cpp
r373089e re6c26b8 52 52 } 53 53 } 54 }55 }56 57 void GameRender::drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId)58 {59 map<unsigned int, Player>::iterator it;60 61 Player* p;62 POSITION pos;63 ALLEGRO_COLOR color;64 65 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)66 {67 p = &it->second;68 69 if (p->isDead)70 continue;71 72 pos = mapToScreen(p->pos.toInt());73 74 if (p->id == curPlayerId)75 al_draw_filled_circle(pos.x, pos.y, 14, al_map_rgb(0, 0, 0));76 77 if (p->team == 0)78 color = al_map_rgb(0, 0, 255);79 else if (p->team == 1)80 color = al_map_rgb(255, 0, 0);81 82 al_draw_filled_circle(pos.x, pos.y, 12, color);83 84 // draw player class85 int fontHeight = al_get_font_line_height(font);86 87 string strClass;88 switch (p->playerClass) {89 case Player::CLASS_WARRIOR:90 strClass = "W";91 break;92 case Player::CLASS_RANGER:93 strClass = "R";94 break;95 case Player::CLASS_NONE:96 strClass = "";97 break;98 default:99 strClass = "";100 break;101 }102 al_draw_text(font, al_map_rgb(0, 0, 0), pos.x, pos.y-fontHeight/2, ALLEGRO_ALIGN_CENTRE, strClass.c_str());103 104 // draw player health105 al_draw_filled_rectangle(pos.x-12, pos.y-24, pos.x+12, pos.y-16, al_map_rgb(0, 0, 0));106 if (p->maxHealth != 0)107 al_draw_filled_rectangle(pos.x-11, pos.y-23, pos.x-11+(22*p->health)/p->maxHealth, pos.y-17, al_map_rgb(255, 0, 0));108 109 if (p->hasBlueFlag)110 al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(0, 0, 255));111 else if (p->hasRedFlag)112 al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(255, 0, 0));113 54 } 114 55 } -
client/Client/GameRender.h
r373089e re6c26b8 24 24 public: 25 25 static void drawMap(WorldMap* gameMap); 26 static void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId);27 26 static void drawPlayers(map<unsigned int, Player*>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId); 28 27 }; -
client/Client/main.cpp
r373089e re6c26b8 54 54 void initWinSock(); 55 55 void shutdownWinSock(); 56 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);56 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); 57 57 int getRefreshRate(int width, int height); 58 58 void drawMessageStatus(ALLEGRO_FONT* font); … … 133 133 ALLEGRO_TIMER *timer = NULL; 134 134 bool key[4] = { false, false, false, false }; 135 map<unsigned int, Player > mapPlayers;135 map<unsigned int, Player*> mapPlayers; 136 136 map<unsigned int, Projectile> mapProjectiles; 137 137 unsigned int curPlayerId = -1; … … 362 362 if (state == STATE_GAME) { 363 363 // find the current player in the player list 364 map<unsigned int, Player >::iterator it;364 map<unsigned int, Player*>::iterator it; 365 365 Player* p = NULL; 366 366 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++) 367 367 { 368 if (it->second .id == curPlayerId)369 p = &it->second;368 if (it->second->id == curPlayerId) 369 p = it->second; 370 370 } 371 371 … … 416 416 cout << "Invalid point: User did not click on the map" << endl; 417 417 }else if (ev.mouse.button == 2) { // right click 418 map<unsigned int, Player >::iterator it;418 map<unsigned int, Player*>::iterator it; 419 419 420 420 cout << "Detected a right-click" << endl; … … 423 423 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++) 424 424 { 425 if (it->second .id == curPlayerId)426 curPlayer = &it->second;425 if (it->second->id == curPlayerId) 426 curPlayer = it->second; 427 427 } 428 428 … … 432 432 // need to check if the right-click was actually on this player 433 433 // right now, this code will target all players other than the current one 434 target = &it->second;434 target = it->second; 435 435 if (target->id != curPlayerId && target->team != curPlayer->team) 436 436 { … … 502 502 503 503 // update players 504 map<unsigned int, Player >::iterator it;504 map<unsigned int, Player*>::iterator it; 505 505 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 506 506 { 507 it->second .updateTarget(mapPlayers);507 it->second->updateTarget(mapPlayers); 508 508 } 509 509 510 510 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 511 511 { 512 it->second .move(gameMap);// ignore return value512 it->second->move(gameMap); // ignore return value 513 513 } 514 514 … … 528 528 Projectile proj = it2->second; 529 529 530 FLOAT_POSITION target = mapPlayers[proj.target] .pos;530 FLOAT_POSITION target = mapPlayers[proj.target]->pos; 531 531 float angle = atan2(target.y-proj.pos.toFloat().y, target.x-proj.pos.toFloat().x); 532 532 … … 572 572 delete game; 573 573 574 map<unsigned int, Player*>::iterator it; 575 576 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) { 577 delete it->second; 578 } 579 574 580 al_destroy_event_queue(event_queue); 575 581 al_destroy_display(display); … … 617 623 } 618 624 619 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)625 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) 620 626 { 621 627 string response = string(msg.buffer); … … 671 677 Player p("", ""); 672 678 p.deserialize(msg.buffer); 673 mapPlayers[p.id] = p; 679 680 if (mapPlayers.find(p.id) != mapPlayers.end()) 681 delete mapPlayers[p.id]; 682 mapPlayers[p.id] = new Player(p); 674 683 curPlayerId = p.id; 675 684 … … 708 717 p.isDead = false; 709 718 710 mapPlayers[p.id] = p; 719 if (mapPlayers.find(p.id) != mapPlayers.end()) 720 delete mapPlayers[p.id]; 721 mapPlayers[p.id] = new Player(p); 711 722 712 723 break; … … 721 732 memcpy(&y, msg.buffer+8, 4); 722 733 723 mapPlayers[id] .target.x = x;724 mapPlayers[id] .target.y = y;734 mapPlayers[id]->target.x = x; 735 mapPlayers[id]->target.y = y; 725 736 726 737 break; … … 781 792 cout << "target id: " << targetID << endl; 782 793 783 Player* source = &mapPlayers[id];794 Player* source = mapPlayers[id]; 784 795 source->targetPlayer = targetID; 785 796 source->isChasing = true; -
common/Common.h
r373089e re6c26b8 1 1 #ifndef _COMMON_H 2 2 #define _COMMON_H 3 4 #include "Compiler.h"5 6 #if defined WINDOWS7 #include <winsock2.h>8 #include <ws2tcpip.h>9 #elif defined LINUX10 #include <fcntl.h>11 #include <assert.h>12 #endif13 3 14 4 #include <string> -
common/MessageContainer.h
r373089e re6c26b8 4 4 #include <string> 5 5 6 #include "Com mon.h"6 #include "Compiler.h" 7 7 8 #if defined LINUX 8 #if defined WINDOWS 9 #include <winsock2.h> 10 #elif defined LINUX 9 11 #include <netinet/in.h> 10 12 #endif -
common/MessageProcessor.cpp
r373089e re6c26b8 3 3 #include <iostream> 4 4 #include <fstream> 5 6 #include "Compiler.h" 7 8 #if defined WINDOWS 9 #include <ws2tcpip.h> 10 #endif 11 12 #include "Common.h" 5 13 6 14 MessageProcessor::MessageProcessor() { -
common/Player.cpp
r373089e re6c26b8 206 206 } 207 207 208 bool Player::updateTarget(map<unsigned int, Player >& mapPlayers) {208 bool Player::updateTarget(map<unsigned int, Player*>& mapPlayers) { 209 209 if (this->isChasing) { 210 this->target.x = mapPlayers[this->targetPlayer] .pos.x;211 this->target.y = mapPlayers[this->targetPlayer] .pos.y;210 this->target.x = mapPlayers[this->targetPlayer]->pos.x; 211 this->target.y = mapPlayers[this->targetPlayer]->pos.y; 212 212 213 213 if (posDistance(this->pos, this->target.toFloat()) <= this->range) { -
common/Player.h
r373089e re6c26b8 5 5 #include <map> 6 6 7 #include "Com mon.h"7 #include "Compiler.h" 8 8 9 #if defined LINUX 9 #if defined WINDOWS 10 #include <winsock2.h> 11 #elif defined LINUX 10 12 #include <netinet/in.h> 11 13 #endif … … 46 48 void deserialize(char* buffer); 47 49 48 bool updateTarget(map<unsigned int, Player >& mapPlayers);50 bool updateTarget(map<unsigned int, Player*>& mapPlayers); 49 51 bool move(WorldMap *map); 50 52 -
common/Projectile.cpp
r373089e re6c26b8 71 71 } 72 72 73 bool Projectile::move(map<unsigned int, Player >& mapPlayers) {73 bool Projectile::move(map<unsigned int, Player*>& mapPlayers) { 74 74 // if the current target logs off, this method will run into problems 75 75 76 76 unsigned long long curTime = getCurrentMillis(); 77 77 78 Player targetP = mapPlayers[target];78 Player* targetP = mapPlayers[target]; 79 79 80 80 if (timeLastUpdated == 0) { … … 85 85 86 86 float pixels = speed * (curTime-timeLastUpdated) / 1000.0; 87 double angle = atan2(targetP .pos.y-pos.y, targetP.pos.x-pos.x);88 float dist = sqrt(pow(targetP .pos.x-pos.x, 2) + pow(targetP.pos.y-pos.y, 2));87 double angle = atan2(targetP->pos.y-pos.y, targetP->pos.x-pos.x); 88 float dist = sqrt(pow(targetP->pos.x-pos.x, 2) + pow(targetP->pos.y-pos.y, 2)); 89 89 90 90 if (dist <= pixels) { 91 pos.x = targetP .pos.x;92 pos.y = targetP .pos.y;91 pos.x = targetP->pos.x; 92 pos.y = targetP->pos.y; 93 93 return true; 94 94 }else { -
common/Projectile.h
r373089e re6c26b8 26 26 27 27 // returns true if it reached the target and should be deleted 28 bool move(map<unsigned int, Player>& mapPlayers); 28 bool move(map<unsigned int, Player*>& mapPlayers); 29 30 /* 31 * target should become a Player*. When this object gets serialized, the player's id should be sent. 32 * Deserialization in this case might be tricky since it will require a playerMap to turn the id into a Plauyer* 33 */ 29 34 30 35 int id;
Note:
See TracChangeset
for help on using the changeset viewer.