Changeset 2662 for code/trunk/src/orxonox/objects/gametypes/Gametype.cc
- Timestamp:
- Feb 14, 2009, 10:17:35 PM (15 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
-
code/trunk/src/orxonox/objects/gametypes/Gametype.cc
r2171 r2662 34 34 35 35 #include "core/CoreIncludes.h" 36 #include "core/ConfigValueIncludes.h" 37 #include "core/Template.h" 38 #include "core/Core.h" 39 #include "overlays/OverlayGroup.h" 36 40 #include "objects/infos/PlayerInfo.h" 41 #include "objects/infos/Bot.h" 37 42 #include "objects/worldentities/pawns/Spectator.h" 38 43 #include "objects/worldentities/SpawnPoint.h" 44 #include "objects/worldentities/Camera.h" 45 #include "Settings.h" 39 46 40 47 #include "network/Host.h" … … 44 51 CreateUnloadableFactory(Gametype); 45 52 46 Gametype::Gametype(BaseObject* creator) : BaseObject(creator) 53 Gametype::Gametype(BaseObject* creator) : BaseObject(creator), gtinfo_(creator) 47 54 { 48 55 RegisterObject(Gametype); 49 56 57 this->setGametype(this); 58 50 59 this->defaultControllableEntity_ = Class(Spectator); 51 60 52 this->bStarted_ = false;53 this->bEnded_ = false;54 61 this->bAutoStart_ = false; 55 62 this->bForceSpawn_ = false; 63 this->numberOfBots_ = 0; 56 64 57 65 this->initialStartCountdown_ = 3; 58 this->startCountdown_ = 0; 59 this->bStartCountdownRunning_ = false; 66 67 this->setConfigValues(); 68 69 this->addBots(this->numberOfBots_); 70 71 // load the corresponding score board 72 if (Core::showsGraphics() && this->scoreboardTemplate_ != "") 73 { 74 this->scoreboard_ = new OverlayGroup(this); 75 this->scoreboard_->addTemplate(this->scoreboardTemplate_); 76 this->scoreboard_->setGametype(this); 77 } 78 else 79 this->scoreboard_ = 0; 80 } 81 82 void Gametype::setConfigValues() 83 { 84 SetConfigValue(initialStartCountdown_, 3.0f); 85 SetConfigValue(bAutoStart_, false); 86 SetConfigValue(bForceSpawn_, false); 87 SetConfigValue(numberOfBots_, 0); 88 SetConfigValue(scoreboardTemplate_, "defaultScoreboard"); 60 89 } 61 90 62 91 void Gametype::tick(float dt) 63 92 { 64 if (this->bStartCountdownRunning_ && !this->bStarted_) 65 this->startCountdown_ -= dt; 66 67 if (!this->bStarted_) 93 SUPER(Gametype, tick, dt); 94 95 if (this->gtinfo_.bStartCountdownRunning_ && !this->gtinfo_.bStarted_) 96 this->gtinfo_.startCountdown_ -= dt; 97 98 if (!this->gtinfo_.bStarted_) 68 99 this->checkStart(); 69 100 else … … 76 107 { 77 108 COUT(0) << "game started" << std::endl; 78 this-> bStarted_ = true;109 this->gtinfo_.bStarted_ = true; 79 110 80 111 this->spawnPlayersIfRequested(); … … 84 115 { 85 116 COUT(0) << "game ended" << std::endl; 86 this-> bEnded_ = true;117 this->gtinfo_.bEnded_ = true; 87 118 } 88 119 89 120 void Gametype::playerEntered(PlayerInfo* player) 90 121 { 91 this->players_[player] = PlayerState::Joined;122 this->players_[player].state_ = PlayerState::Joined; 92 123 93 124 std::string message = player->getName() + " entered the game"; … … 98 129 void Gametype::playerLeft(PlayerInfo* player) 99 130 { 100 std::map<PlayerInfo*, Player State::Enum>::iterator it = this->players_.find(player);131 std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player); 101 132 if (it != this->players_.end()) 102 133 { … … 140 171 void Gametype::pawnKilled(Pawn* victim, Pawn* killer) 141 172 { 142 } 143 144 void Gametype::playerScored(PlayerInfo* player) 145 { 173 if (victim && victim->getPlayer()) 174 { 175 std::string message; 176 if (killer) 177 { 178 if (killer->getPlayer()) 179 message = victim->getPlayer()->getName() + " was killed by " + killer->getPlayer()->getName(); 180 else 181 message = victim->getPlayer()->getName() + " was killed"; 182 } 183 else 184 message = victim->getPlayer()->getName() + " died"; 185 186 COUT(0) << message << std::endl; 187 Host::Broadcast(message); 188 } 189 190 if (victim && victim->getPlayer()) 191 { 192 std::map<PlayerInfo*, Player>::iterator it = this->players_.find(victim->getPlayer()); 193 if (it != this->players_.end()) 194 { 195 it->second.state_ = PlayerState::Dead; 196 it->second.killed_++; 197 198 // Reward killer 199 if (killer) 200 { 201 std::map<PlayerInfo*, Player>::iterator itKiller = this->players_.find(killer->getPlayer()); 202 if (itKiller != this->players_.end()) 203 { 204 this->playerScored(itKiller->second); 205 } 206 else 207 COUT(2) << "Warning: Killing Pawn was not in the playerlist" << std::endl; 208 } 209 210 ControllableEntity* entity = this->defaultControllableEntity_.fabricate(victim->getCreator()); 211 if (victim->getCamera()) 212 { 213 entity->setPosition(victim->getCamera()->getWorldPosition()); 214 entity->setOrientation(victim->getCamera()->getWorldOrientation()); 215 } 216 else 217 { 218 entity->setPosition(victim->getWorldPosition()); 219 entity->setOrientation(victim->getWorldOrientation()); 220 } 221 it->first->startControl(entity); 222 } 223 else 224 COUT(2) << "Warning: Killed Pawn was not in the playerlist" << std::endl; 225 } 226 } 227 228 void Gametype::playerScored(Player& player) 229 { 230 player.frags_++; 146 231 } 147 232 … … 150 235 if (this->spawnpoints_.size() > 0) 151 236 { 152 srand(time(0));153 rnd();154 155 237 unsigned int randomspawn = (unsigned int)rnd(this->spawnpoints_.size()); 156 238 unsigned int index = 0; … … 168 250 void Gametype::assignDefaultPawnsIfNeeded() 169 251 { 170 for (std::map<PlayerInfo*, PlayerState::Enum>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) 171 { 172 if (!it->first->getControllableEntity() && (!it->first->isReadyToSpawn() || !this->bStarted_)) 173 { 174 SpawnPoint* spawn = this->getBestSpawnPoint(it->first); 175 if (spawn) 176 { 177 // force spawn at spawnpoint with default pawn 178 ControllableEntity* entity = this->defaultControllableEntity_.fabricate(spawn); 179 spawn->spawn(entity); 180 it->first->startControl(entity); 181 it->second = PlayerState::Dead; 252 for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) 253 { 254 if (!it->first->getControllableEntity()) 255 { 256 it->second.state_ = PlayerState::Dead; 257 258 if (!it->first->isReadyToSpawn() || !this->gtinfo_.bStarted_) 259 { 260 SpawnPoint* spawn = this->getBestSpawnPoint(it->first); 261 if (spawn) 262 { 263 // force spawn at spawnpoint with default pawn 264 ControllableEntity* entity = this->defaultControllableEntity_.fabricate(spawn); 265 spawn->spawn(entity); 266 it->first->startControl(entity); 267 it->second.state_ = PlayerState::Dead; 268 } 269 else 270 { 271 COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl; 272 abort(); 273 } 274 } 275 } 276 } 277 } 278 279 void Gametype::checkStart() 280 { 281 if (!this->gtinfo_.bStarted_) 282 { 283 if (this->gtinfo_.bStartCountdownRunning_) 284 { 285 if (this->gtinfo_.startCountdown_ <= 0) 286 { 287 this->gtinfo_.bStartCountdownRunning_ = false; 288 this->gtinfo_.startCountdown_ = 0; 289 this->start(); 290 } 291 } 292 else if (this->players_.size() > 0) 293 { 294 if (this->bAutoStart_) 295 { 296 this->start(); 182 297 } 183 298 else 184 299 { 185 COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl;186 abort();187 }188 }189 }190 }191 192 void Gametype::checkStart()193 {194 if (!this->bStarted_)195 {196 if (this->bStartCountdownRunning_)197 {198 if (this->startCountdown_ <= 0)199 {200 this->bStartCountdownRunning_ = false;201 this->startCountdown_ = 0;202 this->start();203 }204 }205 else if (this->players_.size() > 0)206 {207 if (this->bAutoStart_)208 {209 this->start();210 }211 else212 {213 300 bool allplayersready = true; 214 for (std::map<PlayerInfo*, PlayerState::Enum>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) 301 bool hashumanplayers = false; 302 for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) 215 303 { 216 304 if (!it->first->isReadyToSpawn()) 217 305 allplayersready = false; 218 } 219 if (allplayersready) 220 { 221 this->startCountdown_ = this->initialStartCountdown_; 222 this->bStartCountdownRunning_ = true; 306 if (it->first->isHumanPlayer()) 307 hashumanplayers = true; 308 } 309 if (allplayersready && hashumanplayers) 310 { 311 this->gtinfo_.startCountdown_ = this->initialStartCountdown_; 312 this->gtinfo_.bStartCountdownRunning_ = true; 223 313 } 224 314 } … … 229 319 void Gametype::spawnPlayersIfRequested() 230 320 { 231 for (std::map<PlayerInfo*, Player State::Enum>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)321 for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) 232 322 if (it->first->isReadyToSpawn() || this->bForceSpawn_) 233 323 this->spawnPlayer(it->first); … … 236 326 void Gametype::spawnDeadPlayersIfRequested() 237 327 { 238 for (std::map<PlayerInfo*, Player State::Enum>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)239 if (it->second == PlayerState::Dead)328 for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) 329 if (it->second.state_ == PlayerState::Dead) 240 330 if (it->first->isReadyToSpawn() || this->bForceSpawn_) 241 331 this->spawnPlayer(it->first); … … 248 338 { 249 339 player->startControl(spawnpoint->spawn()); 250 this->players_[player] = PlayerState::Alive;340 this->players_[player].state_ = PlayerState::Alive; 251 341 } 252 342 else … … 256 346 } 257 347 } 348 349 void Gametype::addBots(unsigned int amount) 350 { 351 for (unsigned int i = 0; i < amount; ++i) 352 new Bot(this); 353 } 354 355 void Gametype::killBots(unsigned int amount) 356 { 357 unsigned int i = 0; 358 for (ObjectList<Bot>::iterator it = ObjectList<Bot>::begin(); (it != ObjectList<Bot>::end()) && ((amount == 0) || (i < amount)); ) 359 { 360 if (it->getGametype() == this) 361 { 362 delete (*(it++)); 363 ++i; 364 } 365 } 366 } 258 367 }
Note: See TracChangeset
for help on using the changeset viewer.