Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/lastmanstanding/src/orxonox/gametypes/LastManStanding.cc @ 7481

Last change on this file since 7481 was 7481, checked in by jo, 14 years ago

Die erste Arbeitsversion. Zum Testen muss man zuerst Bots hinzufügen, gleich nachdem man das Level geöffnet hat. Problem: Wie kann ich Spieler aus dem Level ausschliessen, wenn sie alle Leben verlohren haben?

File size: 9.7 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 *      Johannes Ritz
24 *   Co-authors:
25 *      ...
26 *
27 */
28//Haupt-Problem: Wie kann ich Spieler vom Spiel ausschliessen, wenn sie alle Leben verlohren haben? (Kann man respawn unterbinden?)
29//Aktuelle Notloesung: Spieler wird unsichtbar und kann keinen Schaden austeilen, aber: setradarinvisibility funktioniert scheinbar nicht
30//Lösungsidee2: Spieler werden als passive Drohnen respawned, wenn sie keine Leben mehr haben (noch nicht implementiert)
31//
32#include "LastManStanding.h"
33
34#include "core/CoreIncludes.h"
35#include "network/Host.h"
36#include "infos/PlayerInfo.h"
37#include "worldentities/pawns/Pawn.h"
38#include "core/ConfigValueIncludes.h"
39
40namespace orxonox
41{
42    CreateUnloadableFactory(LastManStanding);
43
44    LastManStanding::LastManStanding(BaseObject* creator) : Gametype(creator)
45    {
46        RegisterObject(LastManStanding);
47        this->bForceSpawn_=true;
48        this->lives=2;
49        this->playersAlive=0;
50        this->timeRemaining=20.0f;
51    }
52
53    void LastManStanding::setConfigValues()
54    {
55         SetConfigValue(lives, 2);
56    }
57
58    bool LastManStanding::allowPawnHit(Pawn* victim, Pawn* originator)
59    {
60        if (originator && originator->getPlayer())// only for safety
61        {
62            if(playerLives_[originator->getPlayer()]< 0) //dead players shouldn't be able to hit any pawn
63                return false;
64        }
65        return true;
66    }
67
68    bool LastManStanding::allowPawnDamage(Pawn* victim, Pawn* originator)
69    {
70        if (originator && originator->getPlayer())// only for safety
71        {
72            this->timeToAct_[originator->getPlayer()]=timeRemaining;
73            if(playerLives_[originator->getPlayer()]< 0) //dead players shouldn't be able to damage any pawn
74                return false;
75        }
76
77        return true;
78    }
79
80    bool LastManStanding::allowPawnDeath(Pawn* victim, Pawn* originator)
81    {
82        if (!victim)// only for safety
83            return true;
84        playerLives_[victim->getPlayer()]--;
85        if (!playerLives_[victim->getPlayer()])//if player lost all lives
86        {
87            this->playersAlive--;
88            const std::string& message = victim->getPlayer()->getName() + " has lost all lives";
89            COUT(0) << message << std::endl;
90            Host::Broadcast(message);
91        }
92        return true;
93    }
94
95    void LastManStanding::start()
96    {
97        Gametype::start();
98
99        std::string message("Try to survive!");
100        COUT(0) << message << std::endl;
101        Host::Broadcast(message);
102    }
103
104    void LastManStanding::end()
105    {
106        Gametype::end();
107       
108        for (std::map<PlayerInfo*, int>::iterator it = this->playerLives_.begin(); it != this->playerLives_.end(); ++it)
109        {
110            if (it->first->getClientID() == CLIENTID_UNKNOWN)
111                continue;
112
113            if (it->second > 0)
114                this->gtinfo_->sendAnnounceMessage("You have won the match!", it->first->getClientID());
115            else
116                this->gtinfo_->sendAnnounceMessage("You have lost the match!", it->first->getClientID());
117        }
118    }
119
120    void LastManStanding::playerEntered(PlayerInfo* player)
121    {
122        if (!player)// only for safety
123            return;
124        Gametype::playerEntered(player);
125
126        playerLives_[player]=lives;
127        this->playersAlive++;
128        this->timeToAct_[player]=timeRemaining;
129        const std::string& message = player->getName() + " entered the game";
130        COUT(0) << message << std::endl;
131        Host::Broadcast(message);
132    }
133
134    bool LastManStanding::playerLeft(PlayerInfo* player)
135    {
136        bool valid_player = Gametype::playerLeft(player);
137
138        if (valid_player)
139        {
140            this->playersAlive--;
141            //this->playerLives_[player].erase (player); not necessary?
142            //
143            const std::string& message = player->getName() + " left the game";
144            COUT(0) << message << std::endl;
145            Host::Broadcast(message);
146        }
147
148        return valid_player;
149    }
150
151    bool LastManStanding::playerChangedName(PlayerInfo* player)
152    {
153        bool valid_player = Gametype::playerChangedName(player);
154
155        if (valid_player)
156        {
157            const std::string& message = player->getOldName() + " changed name to " + player->getName();
158            COUT(0) << message << std::endl;
159            Host::Broadcast(message);
160        }
161
162        return valid_player;
163    }
164
165    void LastManStanding::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn)//makes dead players invisible
166    {
167        if (playerLives_[pawn->getPlayer()]<=0)//if player is dead
168        {
169            pawn->setVisible(false);
170            pawn->setRadarVisibility(false);
171            //removePlayer(pawn->getPlayer());
172        }
173    }
174    void LastManStanding::pawnPostSpawn(Pawn* pawn)
175    {/*
176        if (playerLives_[pawn->getPlayer()]<=0)//if player is dead
177        {
178            pawn->setVisible(false);  --->Seltsames Verhalten, wenn diese Zeile aktiv ist
179            pawn->setRadarVisibility(false);
180        }  */
181    }
182
183    void LastManStanding::pawnKilled(Pawn* victim, Pawn* killer)
184    {
185        if (victim && victim->getPlayer())
186        {
187            std::string message;
188            if (killer)
189            {
190                if (killer->getPlayer())
191                    message = victim->getPlayer()->getName() + " was killed by " + killer->getPlayer()->getName();
192                else
193                    message = victim->getPlayer()->getName() + " was killed";
194            }
195            else
196                message = victim->getPlayer()->getName() + " died";
197
198            COUT(0) << message << std::endl;
199            Host::Broadcast(message);
200        }
201
202        Gametype::pawnKilled(victim, killer);
203    }
204
205    const int LastManStanding::playerGetLives(PlayerInfo* player)
206    {
207        if (player)
208            return  playerLives_[player];
209        else
210            return 0;
211    }
212
213    void LastManStanding::killPlayer(PlayerInfo* player)
214    {
215        if(!player)
216            return;
217        std::map<PlayerInfo*, Player>::iterator it = this->players_.find(player);
218        if (it != this->players_.end())
219        {
220            it->second.state_ = PlayerState::Dead;//-------------killpart
221            it->second.killed_++;
222
223            playerLives_[player]--;//-----------datapart
224            if (!playerLives_[player])//if player lost all lives
225            {
226                this->playersAlive--;
227                const std::string& message = player->getName() + " has lost all lives";
228                COUT(0) << message << std::endl;
229                Host::Broadcast(message);
230            }
231            /*
232                ControllableEntity* entity = this->defaultControllableEntity_.fabricate(victim->getCreator());
233                if (victim->getCamera())
234                {
235                    entity->setPosition(victim->getCamera()->getWorldPosition());
236                    entity->setOrientation(victim->getCamera()->getWorldOrientation());
237                }
238                else
239                {
240                    entity->setPosition(victim->getWorldPosition());
241                    entity->setOrientation(victim->getWorldOrientation());
242                }
243                it->first->startControl(entity);*/
244        }
245    }
246   
247
248    void LastManStanding::tick(float dt)
249    {
250        SUPER(LastManStanding, tick, dt);
251        if(!this->hasEnded())
252        {
253            if ((this->hasStarted()&&(playersAlive==1)))//last player remaining
254            {
255            this->end();
256            }
257            for (std::map<PlayerInfo*, float>::iterator it = this->timeToAct_.begin(); it != this->timeToAct_.end(); ++it)
258            {       
259                it->second-=dt;
260                if (it->second<0.0f)
261                {
262                    it->second=timeRemaining+3.0f;
263                    this->killPlayer(it->first);
264                }
265            }
266        }
267    }
268
269    /*void LastManStanding::removePlayer(PlayerInfo* player) //----------> Dieser Versuch führt zu einem Programmabsturz
270    {
271        std::map<PlayerInfo*, Player>::const_iterator it = this->players_.find(player);
272        if (it != this->players_.end())
273        {
274            if (it->first->getControllableEntity())
275            {
276                ControllableEntity* oldentity = it->first->getControllableEntity();
277
278                ControllableEntity* entity = this->defaultControllableEntity_.fabricate(oldentity);
279                if (oldentity->getCamera())
280                {
281                    entity->setPosition(oldentity->getCamera()->getWorldPosition());
282                    entity->setOrientation(oldentity->getCamera()->getWorldOrientation());
283                }
284                else
285                {
286                    entity->setPosition(oldentity->getWorldPosition());
287                    entity->setOrientation(oldentity->getWorldOrientation());
288                }
289
290                it->first->startControl(entity);
291            }
292            else
293                this->spawnPlayerAsDefaultPawn(it->first);
294        }
295
296    }*/
297
298}
Note: See TracBrowser for help on using the repository browser.