Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/objects/gametypes/Gametype.cc @ 2086

Last change on this file since 2086 was 2086, checked in by landauf, 15 years ago

added include and removed some debug output

  • Property svn:eol-style set to native
File size: 7.3 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "Gametype.h"
31
32#include <cstdlib>
33#include <ctime>
34
35#include "core/CoreIncludes.h"
36#include "objects/infos/PlayerInfo.h"
37#include "objects/worldentities/pawns/Spectator.h"
38#include "objects/worldentities/SpawnPoint.h"
39
40#include "network/Host.h"
41
42namespace orxonox
43{
44    CreateUnloadableFactory(Gametype);
45
46    Gametype::Gametype(BaseObject* creator) : BaseObject(creator)
47    {
48        RegisterObject(Gametype);
49
50        this->defaultControllableEntity_ = Class(Spectator);
51
52        this->bStarted_ = false;
53        this->bEnded_ = false;
54        this->bAutoStart_ = false;
55        this->bForceSpawn_ = false;
56
57        this->initialStartCountdown_ = 3;
58        this->startCountdown_ = 0;
59        this->bStartCountdownRunning_ = false;
60    }
61
62    void Gametype::tick(float dt)
63    {
64        if (this->bStartCountdownRunning_ && !this->bStarted_)
65            this->startCountdown_ -= dt;
66
67        if (!this->bStarted_)
68            this->checkStart();
69
70        this->assignDefaultPawnsIfNeeded();
71        this->spawnDeadPlayersIfRequested();
72    }
73
74    void Gametype::start()
75    {
76        COUT(0) << "game started" << std::endl;
77        this->bStarted_ = true;
78
79        this->spawnPlayersIfRequested();
80    }
81
82    void Gametype::end()
83    {
84        COUT(0) << "game ended" << std::endl;
85        this->bEnded_ = true;
86    }
87
88    void Gametype::playerEntered(PlayerInfo* player)
89    {
90        this->players_.insert(player);
91
92        std::string message = player->getName() + " entered the game";
93        COUT(0) << message << std::endl;
94        network::Host::Broadcast(message);
95    }
96
97    void Gametype::playerLeft(PlayerInfo* player)
98    {
99        std::set<PlayerInfo*>::iterator it = this->players_.find(player);
100        if (it != this->players_.end())
101        {
102            this->players_.erase(it);
103
104            std::string message = player->getName() + " left the game";
105            COUT(0) << message << std::endl;
106            network::Host::Broadcast(message);
107        }
108    }
109
110    void Gametype::playerSwitched(PlayerInfo* player, Gametype* newgametype)
111    {
112    }
113
114    void Gametype::playerSwitchedBack(PlayerInfo* player, Gametype* oldgametype)
115    {
116    }
117
118    void Gametype::playerChangedName(PlayerInfo* player)
119    {
120        if (this->players_.find(player) != this->players_.end())
121        {
122            if (player->getName() != player->getOldName())
123            {
124                std::string message = player->getOldName() + " changed name to " + player->getName();
125                COUT(0) << message << std::endl;
126                network::Host::Broadcast(message);
127            }
128        }
129    }
130
131    void Gametype::pawnPreSpawn(Pawn* pawn)
132    {
133    }
134
135    void Gametype::pawnPostSpawn(Pawn* pawn)
136    {
137    }
138
139    void Gametype::pawnKilled(Pawn* victim, Pawn* killer)
140    {
141    }
142
143    void Gametype::playerScored(PlayerInfo* player)
144    {
145    }
146
147    SpawnPoint* Gametype::getBestSpawnPoint(PlayerInfo* player) const
148    {
149        if (this->spawnpoints_.size() > 0)
150        {
151            srand(time(0));
152            rnd();
153
154            unsigned int randomspawn = (unsigned int)rnd(this->spawnpoints_.size());
155            unsigned int index = 0;
156            for (std::set<SpawnPoint*>::const_iterator it = this->spawnpoints_.begin(); it != this->spawnpoints_.end(); ++it)
157            {
158                if (index == randomspawn)
159                    return (*it);
160
161                ++index;
162            }
163        }
164        return 0;
165    }
166
167    void Gametype::assignDefaultPawnsIfNeeded() const
168    {
169        for (std::set<PlayerInfo*>::const_iterator it = this->players_.begin(); it != this->players_.end(); ++it)
170        {
171            if (!(*it)->getControllableEntity() && (!(*it)->isReadyToSpawn() || !this->bStarted_))
172            {
173                SpawnPoint* spawn = this->getBestSpawnPoint(*it);
174                if (spawn)
175                {
176                    // force spawn at spawnpoint with default pawn
177                    ControllableEntity* entity = this->defaultControllableEntity_.fabricate(spawn);
178                    spawn->spawn(entity);
179                    (*it)->startControl(entity);
180                }
181                else
182                {
183                    COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl;
184                    abort();
185                }
186            }
187        }
188    }
189
190    void Gametype::checkStart()
191    {
192        if (!this->bStarted_)
193        {
194            if (this->bStartCountdownRunning_)
195            {
196                if (this->startCountdown_ <= 0)
197                {
198                    this->bStartCountdownRunning_ = false;
199                    this->startCountdown_ = 0;
200                    this->start();
201                }
202            }
203            else if (this->players_.size() > 0)
204            {
205                if (this->bAutoStart_)
206                {
207                    this->start();
208                }
209                else
210                {
211                    bool allplayersready = true;
212                    for (std::set<PlayerInfo*>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
213                    {
214                        if (!(*it)->isReadyToSpawn())
215                            allplayersready = false;
216                    }
217                    if (allplayersready)
218                    {
219                        this->startCountdown_ = this->initialStartCountdown_;
220                        this->bStartCountdownRunning_ = true;
221                    }
222                }
223            }
224        }
225    }
226
227    void Gametype::spawnPlayersIfRequested()
228    {
229        for (std::set<PlayerInfo*>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
230            if ((*it)->isReadyToSpawn() || this->bForceSpawn_)
231                this->spawnPlayer(*it);
232    }
233
234    void Gametype::spawnDeadPlayersIfRequested()
235    {
236        for (std::set<PlayerInfo*>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
237            if (!(*it)->getControllableEntity())
238                if ((*it)->isReadyToSpawn() || this->bForceSpawn_)
239                    this->spawnPlayer(*it);
240    }
241
242    void Gametype::spawnPlayer(PlayerInfo* player)
243    {
244        SpawnPoint* spawnpoint = this->getBestSpawnPoint(player);
245        if (spawnpoint)
246        {
247            player->startControl(spawnpoint->spawn());
248        }
249        else
250        {
251            COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl;
252            abort();
253        }
254    }
255}
Note: See TracBrowser for help on using the repository browser.