Changeset a1a3bd5 in network-game
- Timestamp:
- Apr 23, 2013, 1:31:54 AM (12 years ago)
- Branches:
- master
- Children:
- 227baaa
- Parents:
- 054b50b
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
client/Client/main.cpp
r054b50b ra1a3bd5 75 75 bool doexit; 76 76 77 map<unsigned int, Player> mapPlayers;78 79 77 Window* wndLogin; 80 78 Window* wndMain; … … 226 224 } 227 225 else if(ev.type == ALLEGRO_EVENT_TIMER) { 228 redraw = true; 226 redraw = true; // seems like we should just call a draw function here instead 229 227 } 230 228 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 231 229 doexit = true; 230 231 // perform a check to see if it's time to send an update to the server 232 // need to store num ticks since the lst update for this 233 // also check how often each update actually happens and how much it deviates from 60 times per second 232 234 } 233 235 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { … … 264 266 if (receiveMessage(&msgFrom, sock, &from) >= 0) 265 267 processMessage(msgFrom, state, chatConsole, mapPlayers, curPlayerId); 266 267 // update player positions 268 map<unsigned int, Player>::iterator it; 269 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 270 { 271 it->second.move(); 272 } 273 274 if (redraw && al_is_event_queue_empty(event_queue)) 268 269 if (redraw) 275 270 { 276 271 redraw = false; … … 286 281 else if(wndCurrent == wndMain) { 287 282 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:"); 283 284 // update player positions 285 map<unsigned int, Player>::iterator it; 286 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 287 { 288 it->second.move(); // ignore return value 289 } 288 290 289 291 drawMap(gameMap); … … 382 384 { 383 385 string response = string(msg.buffer); 386 387 cout << "Processing message" << endl; 388 cout << response << endl; 384 389 385 390 switch(state) … … 419 424 cout << "Got a valid login response with the player" << endl; 420 425 cout << "Player id: " << curPlayerId << endl; 426 cout << "map size: " << mapPlayers.size() << endl; 421 427 } 428 429 break; 430 } 431 case MSG_TYPE_PLAYER: // kind of hacky to put this here 432 { 433 cout << "Got MSG_TYPE_PLAYER message in Start" << endl; 434 435 Player p("", ""); 436 p.deserialize(msg.buffer); 437 p.timeLastUpdated = getCurrentMillis(); 438 mapPlayers[p.id] = p; 439 440 cout << "new player id: " << p.id << endl; 441 cout << "map size: " << mapPlayers.size() << endl; 422 442 423 443 break; … … 437 457 case MSG_TYPE_LOGIN: 438 458 { 459 cout << "Got a login message" << endl; 460 439 461 chatConsole.addLine(response); 440 462 cout << "Added new line" << endl; 463 464 break; 465 } 466 case MSG_TYPE_LOGOUT: 467 { 441 468 cout << "Got a logout message" << endl; 469 442 470 if (response.compare("You have successfully logged out.") == 0) 443 471 { … … 446 474 wndCurrent = wndLogin; 447 475 } 448 else449 {450 cout << "Added new line" << endl;451 }452 453 break;454 }455 case MSG_TYPE_LOGOUT:456 {457 cout << "Got a logout message, but we don't process it" << endl;458 476 459 477 break; … … 461 479 case MSG_TYPE_PLAYER: 462 480 { 481 cout << "Got MSG_TYPE_PLAYER message in Login" << endl; 482 463 483 Player p("", ""); 464 484 p.deserialize(msg.buffer); 465 485 p.timeLastUpdated = getCurrentMillis(); 466 486 mapPlayers[p.id] = p; 487 488 break; 489 } 490 case MSG_TYPE_PLAYER_MOVE: 491 { 492 cout << "Got a player move message" << endl; 493 494 unsigned int id; 495 int x, y; 496 497 memcpy(&id, msg.buffer, 4); 498 memcpy(&x, msg.buffer+4, 4); 499 memcpy(&y, msg.buffer+8, 4); 500 501 mapPlayers[id].target.x = x; 502 mapPlayers[id].target.y = y; 467 503 468 504 break; … … 498 534 { 499 535 WorldMap::TerrainType el = gameMap->getElement(x, y); 536 WorldMap::ObjectType obj = gameMap->getObject(x, y); 500 537 501 538 if (el == WorldMap::TERRAIN_GRASS) … … 505 542 else if (el == WorldMap::TERRAIN_ROCK) 506 543 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)); 544 545 if (obj == WorldMap::OBJECT_RED_FLAG) 546 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)); 547 else if (obj == WorldMap::OBJECT_BLUE_FLAG) 548 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)); 507 549 } 508 550 } -
common/Player.cpp
r054b50b ra1a3bd5 83 83 } 84 84 85 void Player::move(void) {85 bool Player::move(WorldMap map) { 86 86 int speed = 100; // pixels per second 87 87 unsigned long long curTime = getCurrentMillis(); 88 bool moveCanceled = false; 88 89 89 90 // if we're at our target, don't move 90 91 if (pos.x != target.x || pos.y != target.y) { 91 92 float pixels = speed * (curTime-timeLastUpdated) / 1000.0; 93 double angle = atan2(target.y-pos.y, target.x-pos.x); 94 float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2)); 95 POSITION newPos; 92 96 93 double angle = atan2(target.y-pos.y, target.x-pos.x);94 95 float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));96 97 if (dist <= pixels) { 97 98 pos.x = target.x; 98 99 pos.y = target.y; 99 100 }else { 100 pos.x += cos(angle)*pixels; 101 pos.y += sin(angle)*pixels; 101 newPos.x = int(pos.x + cos(angle)*pixels); 102 newPos.y = int(pos.y + sin(angle)*pixels); 103 104 switch(map.getElement(newPos.x/25, newPos.y/25)) { 105 case WorldMap.TerrainType.TERRAIN_OCEAN: 106 case WorldMap.TerrainType.TERRAIN_ROCK: 107 target.x = pos.x; 108 target.y = pos.y; 109 moveCanceled = true; 110 break; 111 } 102 112 } 103 113 } 104 114 105 115 timeLastUpdated = curTime; 116 return !moveCanceled; 106 117 } -
common/Player.h
r054b50b ra1a3bd5 14 14 15 15 #include "Common.h" 16 #include "WorldMap.h" 16 17 17 18 using namespace std; … … 32 33 void setAddr(sockaddr_in addr); 33 34 34 void move();35 bool move(WorldMap map); 35 36 36 37 int id; -
common/WorldMap.cpp
r054b50b ra1a3bd5 15 15 16 16 vctMap = new vector<vector<TerrainType>*>(width); 17 vctObjects = new vector<vector<ObjectType>*>(width); 17 18 18 19 for (int x=0; x<width; x++) { 19 vector<TerrainType>* newVector = new vector<TerrainType>(height); 20 vector<TerrainType>* newMapVector = new vector<TerrainType>(height); 21 vector<ObjectType>* newObjectVector = new vector<ObjectType>(height); 20 22 21 for (int y=0; y<height; y++) 22 (*newVector)[y] = TERRAIN_NONE; 23 for (int y=0; y<height; y++) { 24 (*newMapVector)[y] = TERRAIN_NONE; 25 (*newObjectVector)[y] = OBJECT_NONE; 26 } 23 27 24 (*vctMap)[x] = newVector; 28 (*vctMap)[x] = newMapVector; 29 (*vctObjects)[x] = newObjectVector; 25 30 } 26 31 } … … 28 33 WorldMap::~WorldMap() 29 34 { 30 for (int x=0; x<width; x++) 35 for (int x=0; x<width; x++) { 31 36 delete (*vctMap)[x]; 37 delete (*vctObjects)[x]; 38 } 32 39 33 40 delete vctMap; 41 delete vctObjects; 34 42 } 35 43 … … 43 51 cout << "getting element" << endl; 44 52 (*(*vctMap)[x])[y] = t; 53 } 54 55 WorldMap::ObjectType WorldMap::getObject(int x, int y) 56 { 57 return (*(*vctObjects)[x])[y]; 58 } 59 60 void WorldMap::setObject(int x, int y, ObjectType t) 61 { 62 cout << "getting object" << endl; 63 (*(*vctObjects)[x])[y] = t; 45 64 } 46 65 … … 57 76 else 58 77 m->setElement(x, y, TERRAIN_GRASS); 78 79 m->setObject(x, y, OBJECT_NONE); 59 80 } 60 81 } … … 102 123 istringstream iss(line); 103 124 string token; 104 int type;105 TerrainType terrain;106 125 107 for(int x=0; x<width; x++) 108 { 126 if (row < height) { 127 // load terrain 128 129 int type; 130 TerrainType terrain; 131 132 for(int x=0; x<width; x++) 133 { 134 getline(iss, token, ','); 135 cout << "token: " << token << endl; 136 type = atoi(token.c_str()); 137 cout << "type: " << type << endl; 138 139 switch(type) { 140 case 1: 141 terrain = TERRAIN_GRASS; 142 break; 143 case 2: 144 terrain = TERRAIN_OCEAN; 145 break; 146 case 3: 147 terrain = TERRAIN_ROCK; 148 break; 149 } 150 151 cout << "About to set element" << endl; 152 cout << "x: " << x << endl; 153 cout << "row: " << row << endl; 154 m->setElement(x, row, terrain); 155 } 156 }else { 157 // load objects 158 159 int x, y, type; 160 ObjectType object; 161 109 162 getline(iss, token, ','); 110 cout << "token: " << token << endl; 163 cout << "token(x): " << token << endl; 164 x = atoi(token.c_str()); 165 166 getline(iss, token, ','); 167 cout << "token(y): " << token << endl; 168 y = atoi(token.c_str()); 169 170 getline(iss, token, ','); 171 cout << "token(type): " << token << endl; 111 172 type = atoi(token.c_str()); 112 cout << "type: " << type << endl;113 173 114 174 switch(type) { 175 case 0: 176 object = OBJECT_NONE; 177 break; 115 178 case 1: 116 terrain = TERRAIN_GRASS;179 object = OBJECT_RED_FLAG; 117 180 break; 118 181 case 2: 119 terrain = TERRAIN_OCEAN; 120 break; 121 case 3: 122 terrain = TERRAIN_ROCK; 182 object = OBJECT_BLUE_FLAG; 123 183 break; 124 184 } 125 185 126 cout << "About to set element" << endl; 127 cout << "x: " << x << endl; 128 cout << "row: " << row << endl; 129 m->setElement(x, row, terrain); 186 m->setObject(x, y, object); 130 187 } 131 188 } -
common/WorldMap.h
r054b50b ra1a3bd5 17 17 }; 18 18 19 enum ObjectType { 20 OBJECT_NONE, 21 OBJECT_RED_FLAG, 22 OBJECT_BLUE_FLAG 23 }; 24 19 25 int width, height; 20 26 vector<vector<TerrainType>*>* vctMap; 27 vector<vector<ObjectType>*>* vctObjects; 21 28 22 29 WorldMap(int width, int height); … … 27 34 void setElement(int x, int y, TerrainType type); 28 35 36 ObjectType getObject(int x, int y); 37 void setObject(int x, int y, ObjectType type); 38 29 39 static WorldMap* createDefaultMap(); 30 40 static WorldMap* loadMapFromFile(string filename); -
data/map.txt
r054b50b ra1a3bd5 12 12 2,1,1,1,1,1,1,1,1,1,1,2 13 13 2,2,2,2,2,2,2,2,2,2,2,2 14 6,1,1 15 6,10,2
Note:
See TracChangeset
for help on using the changeset viewer.