Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

many changes, most important: BaseObject takes now a pointer to it's creator which is needed to build a level hierarchy (with different scenes)

File size: 6.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->defaultPawn_ = Class(Spectator);
51        this->bStarted_ = false;
52        this->bEnded_ = false;
53        this->bAutoStart_ = false;
54
55        COUT(0) << "created Gametype" << std::endl;
56    }
57
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)
82    {
83        this->players_.insert(player);
84
85        std::string message = player->getName() + " entered the game";
86        COUT(0) << message << std::endl;
87        network::Host::Broadcast(message);
88    }
89
90    void Gametype::playerLeft(PlayerInfo* player)
91    {
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    {
109    }
110
111    void Gametype::playerChangedName(PlayerInfo* player)
112    {
113        if (this->players_.find(player) != this->players_.end())
114        {
115            if (player->getName() != player->getOldName())
116            {
117                std::string message = player->getOldName() + " changed name to " + player->getName();
118                COUT(0) << message << std::endl;
119                network::Host::Broadcast(message);
120            }
121        }
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    }
220}
Note: See TracBrowser for help on using the repository browser.