Changeset b07eeac in network-game
- Timestamp:
- May 25, 2013, 1:45:54 AM (12 years ago)
- Branches:
- master
- Children:
- 2df63d6
- Parents:
- b81cea1
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
common/Common.cpp
rb81cea1 rb07eeac 2 2 3 3 #include <iostream> 4 using namespace std; 4 #include <cmath> 5 5 6 6 #if defined WINDOWS … … 9 9 #include <time.h> 10 10 #endif 11 12 using namespace std; 13 14 /* 15 FLOAT_POSITION POSITION::toFloat() { 16 FLOAT_POSITION floatPosition; 17 floatPosition.x = x; 18 floatPosition.y = y; 19 20 return floatPosition; 21 } 22 */ 11 23 12 24 void set_nonblock(int sock) … … 38 50 return numMilliseconds; 39 51 } 52 53 float posDistance(FLOAT_POSITION pos1, FLOAT_POSITION pos2) { 54 float xDiff = pos2.x - pos1.x; 55 float yDiff = pos2.y - pos1.y; 56 57 return sqrt( pow(xDiff,2) + pow(yDiff,2) ); 58 } -
common/Common.h
rb81cea1 rb07eeac 12 12 #endif 13 13 14 void set_nonblock(int sock);15 unsigned long long getCurrentMillis();16 17 typedef struct18 {19 int x;20 int y;21 } POSITION;22 23 14 typedef struct 24 15 { … … 27 18 } FLOAT_POSITION; 28 19 20 typedef struct 21 { 22 int x; 23 int y; 24 //FLOAT_POSITION toFloat(); 25 FLOAT_POSITION toFloat() { 26 FLOAT_POSITION floatPosition; 27 floatPosition.x = x; 28 floatPosition.y = y; 29 30 return floatPosition; 31 } 32 } POSITION; 33 34 void set_nonblock(int sock); 35 unsigned long long getCurrentMillis(); 36 float posDistance(FLOAT_POSITION pos1, FLOAT_POSITION pos2); 37 29 38 #endif -
common/Message.cpp
rb81cea1 rb07eeac 19 19 int ret = sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in)); 20 20 21 cout << "Send a message of type " << msg->type << endl; 22 21 23 return ret; 22 24 } -
server/server.cpp
rb81cea1 rb07eeac 161 161 } 162 162 163 switch(gameMap->getStructure(it->second.pos.x/25, it->second.pos.y/25)) { 164 case WorldMap::STRUCTURE_BLUE_FLAG: 165 cout << "Got blue flag" << endl; 166 it->second.hasBlueFlag = true; 167 broadcastMove = true; 168 break; 169 case WorldMap::STRUCTURE_RED_FLAG: 170 cout << "Got red flag" << endl; 171 it->second.hasRedFlag = true; 172 broadcastMove = true; 173 break; 163 vector<WorldMap::Object> vctObjects = gameMap->getObjects(); 164 vector<WorldMap::Object>::iterator itObjects; 165 166 for (itObjects = vctObjects.begin(); itObjects != vctObjects.end(); itObjects++) { 167 POSITION pos = itObjects->pos; 168 if (posDistance(it->second.pos, pos.toFloat()) < 10) { 169 switch (itObjects->type) { 170 case WorldMap::OBJECT_BLUE_FLAG: 171 cout << "Got blue flag" << endl; 172 it->second.hasBlueFlag = true; 173 broadcastMove = true; 174 break; 175 case WorldMap::OBJECT_RED_FLAG: 176 cout << "Got red flag" << endl; 177 it->second.hasRedFlag = true; 178 broadcastMove = true; 179 break; 180 } 181 182 // send a MSG_TYPE_REMOVE_OBJECT message 183 serverMsg.type = MSG_TYPE_REMOVE_OBJECT; 184 memcpy(serverMsg.buffer, &itObjects->id, 4); 185 186 map<unsigned int, Player>::iterator it2; 187 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 188 { 189 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 190 error("sendMessage"); 191 } 192 } 174 193 } 175 194 … … 179 198 180 199 cout << "about to broadcast move" << endl; 181 map<unsigned int, Player>::iterator it , it2;200 map<unsigned int, Player>::iterator it2; 182 201 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 183 202 { … … 415 434 mapPlayers[id].target.x = x; 416 435 mapPlayers[id].target.y = y; 417 int xDiff = mapPlayers[id].target.x - mapPlayers[id].pos.x;418 int yDiff = mapPlayers[id].target.y - mapPlayers[id].pos.y;419 436 420 437 serverMsg.type = MSG_TYPE_PLAYER_MOVE;
Note:
See TracChangeset
for help on using the changeset viewer.