Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2072 was 2072, checked in by landauf, 16 years ago

changed svn:eol-style to native in all new files

  • Property svn:eol-style set to native
File size: 7.4 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        COUT(0) << "created Gametype" << std::endl;
62    }
63
64    void Gametype::tick(float dt)
65    {
66        if (this->bStartCountdownRunning_ && !this->bStarted_)
67            this->startCountdown_ -= dt;
68
69        if (!this->bStarted_)
70            this->checkStart();
71
72        this->assignDefaultPawnsIfNeeded();
73        this->spawnDeadPlayersIfRequested();
74    }
75
76    void Gametype::start()
77    {
78        COUT(0) << "game started" << std::endl;
79        this->bStarted_ = true;
80
81        this->spawnPlayersIfRequested();
82    }
83
84    void Gametype::end()
85    {
86        COUT(0) << "game ended" << std::endl;
87        this->bEnded_ = true;
88    }
89
90    void Gametype::playerEntered(PlayerInfo* player)
91    {
92        this->players_.insert(player);
93
94        std::string message = player->getName() + " entered the game";
95        COUT(0) << message << std::endl;
96        network::Host::Broadcast(message);
97    }
98
99    void Gametype::playerLeft(PlayerInfo* player)
100    {
101        std::set<PlayerInfo*>::iterator it = this->players_.find(player);
102        if (it != this->players_.end())
103        {
104            this->players_.erase(it);
105
106            std::string message = player->getName() + " left the game";
107            COUT(0) << message << std::endl;
108            network::Host::Broadcast(message);
109        }
110    }
111
112    void Gametype::playerSwitched(PlayerInfo* player, Gametype* newgametype)
113    {
114    }
115
116    void Gametype::playerSwitchedBack(PlayerInfo* player, Gametype* oldgametype)
117    {
118    }
119
120    void Gametype::playerChangedName(PlayerInfo* player)
121    {
122        if (this->players_.find(player) != this->players_.end())
123        {
124            if (player->getName() != player->getOldName())
125            {
126                std::string message = player->getOldName() + " changed name to " + player->getName();
127                COUT(0) << message << std::endl;
128                network::Host::Broadcast(message);
129            }
130        }
131    }
132
133    void Gametype::pawnPreSpawn(Pawn* pawn)
134    {
135    }
136
137    void Gametype::pawnPostSpawn(Pawn* pawn)
138    {
139    }
140
141    void Gametype::pawnKilled(Pawn* victim, Pawn* killer)
142    {
143    }
144
145    void Gametype::playerScored(PlayerInfo* player)
146    {
147    }
148
149    SpawnPoint* Gametype::getBestSpawnPoint(PlayerInfo* player) const
150    {
151        if (this->spawnpoints_.size() > 0)
152        {
153            srand(time(0));
154            rnd();
155
156            unsigned int randomspawn = (unsigned int)rnd(this->spawnpoints_.size());
157            unsigned int index = 0;
158            for (std::set<SpawnPoint*>::const_iterator it = this->spawnpoints_.begin(); it != this->spawnpoints_.end(); ++it)
159            {
160                if (index == randomspawn)
161                    return (*it);
162
163                ++index;
164            }
165        }
166        return 0;
167    }
168
169    void Gametype::assignDefaultPawnsIfNeeded() const
170    {
171        for (std::set<PlayerInfo*>::const_iterator it = this->players_.begin(); it != this->players_.end(); ++it)
172        {
173            if (!(*it)->getControllableEntity() && (!(*it)->isReadyToSpawn() || !this->bStarted_))
174            {
175                SpawnPoint* spawn = this->getBestSpawnPoint(*it);
176                if (spawn)
177                {
178                    // force spawn at spawnpoint with default pawn
179                    ControllableEntity* entity = this->defaultControllableEntity_.fabricate(spawn);
180                    spawn->spawn(entity);
181                    (*it)->startControl(entity);
182                }
183                else
184                {
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                else
212                {
213                    bool allplayersready = true;
214                    for (std::set<PlayerInfo*>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
215                    {
216                        if (!(*it)->isReadyToSpawn())
217                            allplayersready = false;
218                    }
219                    if (allplayersready)
220                    {
221                        this->startCountdown_ = this->initialStartCountdown_;
222                        this->bStartCountdownRunning_ = true;
223                    }
224                }
225            }
226        }
227    }
228
229    void Gametype::spawnPlayersIfRequested()
230    {
231        for (std::set<PlayerInfo*>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
232            if ((*it)->isReadyToSpawn() || this->bForceSpawn_)
233                this->spawnPlayer(*it);
234    }
235
236    void Gametype::spawnDeadPlayersIfRequested()
237    {
238        for (std::set<PlayerInfo*>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
239            if (!(*it)->getControllableEntity())
240                if ((*it)->isReadyToSpawn() || this->bForceSpawn_)
241                    this->spawnPlayer(*it);
242    }
243
244    void Gametype::spawnPlayer(PlayerInfo* player)
245    {
246        SpawnPoint* spawnpoint = this->getBestSpawnPoint(player);
247        if (spawnpoint)
248        {
249            player->startControl(spawnpoint->spawn());
250        }
251        else
252        {
253            COUT(1) << "Error: No SpawnPoints in current Gametype" << std::endl;
254            abort();
255        }
256    }
257}
Note: See TracBrowser for help on using the repository browser.