Changeset 23559e7 in network-game
- Timestamp:
- May 24, 2013, 1:23:49 AM (12 years ago)
- Branches:
- master
- Children:
- b81cea1
- Parents:
- 45b2750
- git-author:
- dportnoy <dmp1488@…> (05/24/13 01:14:53)
- git-committer:
- dportnoy <dmp1488@…> (05/24/13 01:23:49)
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
common/Player.cpp
r45b2750 r23559e7 89 89 90 90 bool Player::move(WorldMap *map) { 91 int speed = 100; // pixels per second 91 int speed = 100; // pixels per second. should probably be in the constructor 92 92 unsigned long long curTime = getCurrentMillis(); 93 bool moveCanceled = false;94 93 95 94 // if we're at our target, don't move 96 if (pos.x != target.x || pos.y != target.y) { 95 bool moving = (pos.x != target.x || pos.y != target.y); 96 97 if (moving) { 97 98 float pixels = speed * (curTime-timeLastUpdated) / 1000.0; 98 99 double angle = atan2(target.y-pos.y, target.x-pos.x); … … 107 108 newPos.y = pos.y + sin(angle)*pixels; 108 109 } 109 110 switch(map->getElement(newPos.x/25, newPos.y/25)) {111 case WorldMap::TERRAIN_NONE:112 case WorldMap::TERRAIN_OCEAN:113 case WorldMap::TERRAIN_ROCK:114 target.x = pos.x;115 target.y = pos.y;116 moveCanceled = true;117 break;118 default: // if there are no obstacles119 pos.x = newPos.x;120 pos.y = newPos.y;121 break;122 }123 124 // using moveCanceled in a hacky way just to indicate that the server125 // has updated some player info. Should change the variable name126 switch(map->getStructure(newPos.x/25, newPos.y/25)) {127 case WorldMap::STRUCTURE_BLUE_FLAG:128 hasBlueFlag = true;129 moveCanceled = true;130 break;131 case WorldMap::STRUCTURE_RED_FLAG:132 hasRedFlag = true;133 moveCanceled = true;134 break;135 }136 110 } 137 111 138 112 timeLastUpdated = curTime; 139 113 140 return !moveCanceled;114 return moving; 141 115 } -
server/server.cpp
r45b2750 r23559e7 139 139 140 140 // maybe put this in a separate method 141 map<unsigned int, Player>::iterator it, it2; 141 map<unsigned int, Player>::iterator it; 142 FLOAT_POSITION oldPos; 143 bool broadcastMove = false; 142 144 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) { 143 if (!it->second.move(gameMap)) { 144 cout << "Cenceling move" << endl; 145 serverMsg.type = MSG_TYPE_PLAYER; 146 it->second.serialize(serverMsg.buffer); 147 148 cout << "(" << it->second.pos.x << "," << it->second.pos.y << ")" << endl; 149 150 if (it->second.hasBlueFlag) 151 cout << "Got blue flag" << endl; 152 if (it->second.hasRedFlag) 153 cout << "Got red flag" << endl; 154 155 cout << "about to send move cencellation" << endl; 156 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 157 { 158 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 159 error("sendMessage"); 145 oldPos = it->second.pos; 146 if (it->second.move(gameMap)) { 147 148 // check if the move needs to be canceled 149 switch(gameMap->getElement(it->second.pos.x/25, it->second.pos.y/25)) { 150 case WorldMap::TERRAIN_NONE: 151 case WorldMap::TERRAIN_OCEAN: 152 case WorldMap::TERRAIN_ROCK: 153 it->second.pos = oldPos; 154 it->second.target.x = it->second.pos.x; 155 it->second.target.y = it->second.pos.y; 156 broadcastMove = true; 157 break; 158 default: 159 // if there are no obstacles, do nothing 160 break; 161 } 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; 174 } 175 176 if (broadcastMove) { 177 serverMsg.type = MSG_TYPE_PLAYER; 178 it->second.serialize(serverMsg.buffer); 179 180 cout << "about to broadcast move" << endl; 181 map<unsigned int, Player>::iterator it, it2; 182 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 183 { 184 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 185 error("sendMessage"); 186 } 160 187 } 161 188 }
Note:
See TracChangeset
for help on using the changeset viewer.