- Timestamp:
- Oct 27, 2008, 4:08:51 AM (16 years ago)
- Location:
- code/branches/objecthierarchy/src/orxonox/objects/gametypes
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/objecthierarchy/src/orxonox/objects/gametypes/Gametype.cc
r2006 r2019 30 30 #include "Gametype.h" 31 31 32 #include <cstdlib> 33 #include <ctime> 34 32 35 #include "core/CoreIncludes.h" 33 36 #include "objects/infos/PlayerInfo.h" 34 37 #include "objects/worldentities/pawns/Spectator.h" 38 #include "objects/worldentities/SpawnPoint.h" 35 39 36 40 #include "network/Host.h" … … 40 44 CreateUnloadableFactory(Gametype); 41 45 42 Gametype::Gametype( )46 Gametype::Gametype(BaseObject* creator) : BaseObject(creator) 43 47 { 44 48 RegisterObject(Gametype); 45 49 46 50 this->defaultPawn_ = Class(Spectator); 51 this->bStarted_ = false; 52 this->bEnded_ = false; 53 this->bAutoStart_ = false; 47 54 48 55 COUT(0) << "created Gametype" << std::endl; 49 56 } 50 57 51 void Gametype::addPlayer(PlayerInfo* player) 58 void Gametype::tick(float dt) 59 { 60 if (!this->bStarted_) 61 this->checkStart(); 62 63 this->assignDefaultPawnsIfNeeded(); 64 } 65 66 void Gametype::start() 67 { 68 COUT(0) << "game started" << std::endl; 69 this->bStarted_ = true; 70 71 for (std::set<PlayerInfo*>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) 72 this->spawnPlayer(*it); 73 } 74 75 void Gametype::end() 76 { 77 COUT(0) << "game ended" << std::endl; 78 this->bEnded_ = true; 79 } 80 81 void Gametype::playerEntered(PlayerInfo* player) 52 82 { 53 83 this->players_.insert(player); 54 this->playerJoined(player); 55 56 ControllableEntity* newpawn = this->defaultPawn_.fabricate(); 57 player->startControl(newpawn); 58 } 59 60 void Gametype::removePlayer(PlayerInfo* player) 61 { 62 if (this->players_.find(player) != this->players_.end()) 63 { 64 player->stopControl(); 65 this->players_.erase(player); 66 this->playerLeft(player); 67 } 68 } 69 70 void Gametype::playerJoined(PlayerInfo* player) 71 { 84 72 85 std::string message = player->getName() + " entered the game"; 73 86 COUT(0) << message << std::endl; … … 77 90 void Gametype::playerLeft(PlayerInfo* player) 78 91 { 79 std::string message = player->getName() + " left the game"; 80 COUT(0) << message << std::endl; 81 network::Host::Broadcast(message); 92 std::set<PlayerInfo*>::iterator it = this->players_.find(player); 93 if (it != this->players_.end()) 94 { 95 this->players_.erase(it); 96 97 std::string message = player->getName() + " left the game"; 98 COUT(0) << message << std::endl; 99 network::Host::Broadcast(message); 100 } 101 } 102 103 void Gametype::playerSwitched(PlayerInfo* player, Gametype* newgametype) 104 { 105 } 106 107 void Gametype::playerSwitchedBack(PlayerInfo* player, Gametype* oldgametype) 108 { 82 109 } 83 110 … … 94 121 } 95 122 } 123 124 void Gametype::playerSpawned(PlayerInfo* player) 125 { 126 } 127 128 void Gametype::playerDied(PlayerInfo* player) 129 { 130 } 131 132 void Gametype::playerScored(PlayerInfo* player) 133 { 134 } 135 136 SpawnPoint* Gametype::getBestSpawnPoint(PlayerInfo* player) const 137 { 138 if (this->spawnpoints_.size() > 0) 139 { 140 srand(time(0)); 141 rnd(); 142 143 unsigned int randomspawn = (unsigned int)rnd(this->spawnpoints_.size()); 144 unsigned int index = 0; 145 for (std::set<SpawnPoint*>::iterator it = this->spawnpoints_.begin(); it != this->spawnpoints_.end(); ++it) 146 { 147 if (index == randomspawn) 148 return (*it); 149 150 ++index; 151 } 152 } 153 return 0; 154 } 155 156 void Gametype::assignDefaultPawnsIfNeeded() const 157 { 158 for (std::set<PlayerInfo*>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) 159 { 160 if (!(*it)->getControllableEntity() && (!(*it)->isReadyToSpawn() || !this->bStarted_)) 161 { 162 SpawnPoint* spawn = this->getBestSpawnPoint(*it); 163 if (spawn) 164 { 165 // force spawn at spawnpoint with default pawn 166 ControllableEntity* newpawn = this->defaultPawn_.fabricate(spawn); 167 spawn->spawn(newpawn); 168 (*it)->startControl(newpawn); 169 } 170 else 171 { 172 COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl; 173 abort(); 174 } 175 } 176 } 177 } 178 179 void Gametype::checkStart() 180 { 181 if (!this->bStarted_) 182 { 183 if (this->players_.size() > 0) 184 { 185 if (this->bAutoStart_) 186 { 187 this->start(); 188 } 189 else 190 { 191 bool allplayersready = true; 192 for (std::set<PlayerInfo*>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) 193 { 194 if (!(*it)->isReadyToSpawn()) 195 allplayersready = false; 196 } 197 if (allplayersready) 198 this->start(); 199 } 200 } 201 } 202 } 203 204 void Gametype::spawnPlayer(PlayerInfo* player) 205 { 206 if (player->isReadyToSpawn()) 207 { 208 SpawnPoint* spawnpoint = this->getBestSpawnPoint(player); 209 if (spawnpoint) 210 { 211 player->startControl(spawnpoint->spawn()); 212 } 213 else 214 { 215 COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl; 216 abort(); 217 } 218 } 219 } 96 220 } -
code/branches/objecthierarchy/src/orxonox/objects/gametypes/Gametype.h
r2006 r2019 37 37 #include "core/Identifier.h" 38 38 #include "objects/worldentities/ControllableEntity.h" 39 #include "objects/Tickable.h" 39 40 40 41 namespace orxonox 41 42 { 42 class _OrxonoxExport Gametype : public BaseObject 43 class _OrxonoxExport Gametype : public BaseObject, public Tickable 43 44 { 44 45 friend class PlayerInfo; 45 46 46 47 public: 47 Gametype( );48 Gametype(BaseObject* creator); 48 49 virtual ~Gametype() {} 50 51 virtual void tick(float dt); 52 53 virtual void start(); 54 virtual void end(); 55 virtual void playerEntered(PlayerInfo* player); 56 virtual void playerLeft(PlayerInfo* player); 57 virtual void playerSwitched(PlayerInfo* player, Gametype* newgametype); 58 virtual void playerSwitchedBack(PlayerInfo* player, Gametype* oldgametype); 59 virtual void playerChangedName(PlayerInfo* player); 60 virtual void playerSpawned(PlayerInfo* player); 61 virtual void playerDied(PlayerInfo* player); 62 virtual void playerScored(PlayerInfo* player); 49 63 50 64 inline const std::set<PlayerInfo*>& getPlayers() const 51 65 { return this->players_; } 52 66 53 protected: 54 virtual void playerJoined(PlayerInfo* player); 55 virtual void playerLeft(PlayerInfo* player); 56 57 virtual void playerChangedName(PlayerInfo* player); 67 inline void registerSpawnPoint(SpawnPoint* spawnpoint) 68 { this->spawnpoints_.insert(spawnpoint); } 58 69 59 70 private: 71 virtual SpawnPoint* getBestSpawnPoint(PlayerInfo* player) const; 72 60 73 void addPlayer(PlayerInfo* player); 61 74 void removePlayer(PlayerInfo* player); 62 75 76 void assignDefaultPawnsIfNeeded() const; 77 void checkStart(); 78 void spawnPlayer(PlayerInfo* player); 79 80 bool bStarted_; 81 bool bEnded_; 82 bool bAutoStart_; 63 83 std::set<PlayerInfo*> players_; 84 std::set<SpawnPoint*> spawnpoints_; 64 85 SubclassIdentifier<ControllableEntity> defaultPawn_; 65 86 };
Note: See TracChangeset
for help on using the changeset viewer.