Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/spacestationentry/src/modules/docking/Dock.cc @ 9797

Last change on this file since 9797 was 9789, checked in by agermann, 12 years ago

Erstellung einer neuen Funktion execute2 in Dock.cc um ein Ausdocken zu realisieren.

  • Property svn:eol-style set to native
File size: 10.2 KB
RevLine 
[8137]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 *      Sven Stucki
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file Dock.cc
[8560]31    @brief Docking system main class
[8137]32*/
33
34#include "Dock.h"
[8382]35
[8434]36#include "core/CoreIncludes.h"
37#include "core/LuaState.h"
38#include "core/GUIManager.h"
[8705]39#include "core/command/ConsoleCommand.h"
40#include "network/NetworkFunction.h"
41
[8192]42#include "infos/HumanPlayer.h"
[8705]43#include "interfaces/PlayerTrigger.h"
[8186]44#include "worldentities/pawns/Pawn.h"
[8137]45
46namespace orxonox
47{
[9667]48    RegisterClass(Dock);
[8137]49
[8382]50    SetConsoleCommand("Dock", "dock",    &Dock::cmdDock).addShortcut().setAsInputCommand();
51    SetConsoleCommand("Dock", "undock",  &Dock::cmdUndock).addShortcut().setAsInputCommand();
52
[8705]53    registerStaticNetworkFunction(Dock::showDockingDialog);
54
[9667]55    Dock::Dock(Context* context) : StaticEntity(context)
[8137]56    {
57        RegisterObject(Dock);
58    }
59
60    Dock::~Dock()
61    {
62    }
63
64    void Dock::XMLPort(Element& xmlelement, XMLPort::Mode mode)
65    {
66        SUPER(Dock, XMLPort, xmlelement, mode);
67
[8151]68        XMLPortObject(Dock, DockingEffect, "effects", addEffect, getEffect, xmlelement, mode);
[8493]69        XMLPortObject(Dock, DockingAnimation, "animations", addAnimation, getAnimation, xmlelement, mode);
[8137]70        XMLPortEventSink(Dock, BaseObject, "execute", execute, xmlelement, mode);
[9789]71
[8137]72    }
73
74    void Dock::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
75    {
76        SUPER(Dock, XMLEventPort, xmlelement, mode);
77
78        XMLPortEventSink(Dock, BaseObject, "execute", execute, xmlelement, mode);
[9789]79
80   //     XMLPortEventSink(Dock, BaseObject, "execute2", execute2, xmlelement, mode);
[8137]81    }
82
83    bool Dock::execute(bool bTriggered, BaseObject* trigger)
[8185]84    {
[8186]85        PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger);
[8667]86        PlayerInfo* player = NULL;
[8186]87
[8188]88        // Check whether it is a player trigger and extract pawn from it
[8186]89        if(pTrigger != NULL)
90        {
[8188]91            if(!pTrigger->isForPlayer()) {  // The PlayerTrigger is not exclusively for Pawns which means we cannot extract one.
[8858]92                orxout(verbose, context::docking) << "Docking:execute PlayerTrigger was not triggered by a player.." << endl;
[8186]93                return false;
[8188]94            }
[8667]95            player = pTrigger->getTriggeringPlayer();
[8493]96        }
97        else
98        {
[8858]99            orxout(verbose, context::docking) << "Docking::execute Not a player trigger, can't extract pawn from it.." << endl;
[8188]100            return false;
[8186]101        }
102        if(player == NULL)
103        {
[8858]104            orxout(verbose, context::docking) << "Docking::execute Can't retrieve PlayerInfo from Trigger. (" << trigger->getIdentifier()->getName() << ")" << endl;
[8186]105            return false;
106        }
107
[8434]108        if(bTriggered)
109        {
[8382]110            // Add player to this Docks candidates
[8700]111            candidates_.insert(player);
[8382]112
[8434]113            // Show docking dialog
[8705]114            this->showDockingDialogHelper(player);
[8434]115        }
116        else
117        {
[8382]118            // Remove player from candidates list
[8700]119            candidates_.erase(player);
[8185]120        }
121
122        return true;
123    }
124
[9789]125    /*bool Dock::execute2(bool bTriggered, BaseObject* trigger)
126        {
127        orxout(user_warning)<<"execute2"<<endl;
128            PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger);
129            PlayerInfo* player = NULL;
130
131            // Check whether it is a player trigger and extract pawn from it
132            if(pTrigger != NULL)
133            {
134                if(!pTrigger->isForPlayer()) {  // The PlayerTrigger is not exclusively for Pawns which means we cannot extract one.
135                    orxout(verbose, context::docking) << "Docking:execute PlayerTrigger was not triggered by a player.." << endl;
136                    return false;
137                }
138                player = pTrigger->getTriggeringPlayer();
139            }
140            else
141            {
142                orxout(verbose, context::docking) << "Docking::execute Not a player trigger, can't extract pawn from it.." << endl;
143                return false;
144            }
145            if(player == NULL)
146            {
147                orxout(verbose, context::docking) << "Docking::execute Can't retrieve PlayerInfo from Trigger. (" << trigger->getIdentifier()->getName() << ")" << endl;
148                return false;
149            }
150
151            if(bTriggered)
152            {
153                // Add player to this Docks candidates
154                docked_.insert(player);
155
156                // Show docking dialog
157                this->showDockingDialogHelper(player);
158            }
159            else
160            {
161                // Remove player from candidates list
162                docked_.erase(player);
163            }
164
165            return true;
166        }
167
168*/
[8705]169    void Dock::showDockingDialogHelper(PlayerInfo* player)
170    {
171        assert(player);
172       
173        if(!player->isHumanPlayer())
174            return;
175       
176        if(GameMode::isMaster())
177        {
178            if(GameMode::showsGraphics())
179                GUIManager::showGUI("DockingDialog");
180        }
181        else
182            callStaticNetworkFunction(Dock::showDockingDialog, player->getClientID());
183
184    }
185
186    /*static*/ void Dock::showDockingDialog()
187    {
188        if(GameMode::showsGraphics())
189            GUIManager::showGUI("DockingDialog");
190    }
191
[8434]192    void Dock::cmdDock()
193    {
[8382]194        PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer();
[8434]195        for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it)
196        {
[8382]197            if(it->dock(player))
198                break;
199        }
200    }
201
[8434]202    void Dock::cmdUndock()
203    {
[8382]204        PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer();
[8434]205        for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it)
206        {
[8382]207            if(it->undock(player))
208                break;
209        }
210    }
211
[8434]212    bool Dock::dock(PlayerInfo* player)
213    {
[8382]214        // Check if player is a candidate
[8700]215        if(candidates_.find(player) == candidates_.end())
[8434]216        {
[8858]217            orxout(internal_warning, context::docking) << "Dock::dock Player is not a candidate!" << endl;
[8382]218            return false;
219        }
220
[8700]221        candidates_.erase(player);
222        docked_.insert(player);
[8493]223
[8700]224        if (animations_.empty())
[8493]225            return dockingAnimationFinished(player);
[9789]226
[8493]227        else
[8700]228            DockingAnimation::invokeAnimation(true, player, animations_);
[8493]229
[8382]230        return true;
231    }
232
[8493]233    bool Dock::dockingAnimationFinished(PlayerInfo* player)
234    {
[8700]235        if(docked_.find(player) == docked_.end())
[8493]236        {
[8858]237            orxout(internal_warning, context::docking) << "Dock::dockingAnimationFinished Player is not currently docked." << endl;
[8493]238            return false;
239        }
240
[8700]241        DockingEffect::invokeEffect(true, player, effects_);
[8493]242        return true;
243    }
244
[8434]245    bool Dock::undock(PlayerInfo* player)
246    {
[8382]247        // Check if player is docked to this Dock
[8700]248        if(docked_.find(player) == docked_.end())
[8434]249        {
[8858]250            orxout(internal_warning, context::docking) << "Dock::undock Player is not docked to this Dock." << endl;
[8382]251            return false;
252        }
253
[8700]254        docked_.erase(player);
255        candidates_.insert(player);
[8493]256
[8700]257        DockingEffect::invokeEffect(false, player, effects_);
[8493]258
[8700]259        if (animations_.empty())
[8493]260            return undockingAnimationFinished(player);
261        else
[8700]262            DockingAnimation::invokeAnimation(false, player, animations_);
[8493]263
[8382]264        return true;
265    }
266
[8493]267    bool Dock::undockingAnimationFinished(PlayerInfo* player) {
[8858]268        orxout(verbose, context::docking) << "Dock::undockingAnimationFinished executed" << endl;
[8493]269        return true;
270    }
[8382]271
[8434]272    unsigned int Dock::getNumberOfActiveDocks()
273    {
274        int i = 0;
275        PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer();
276        for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it)
277        {
[8700]278            if(it->candidates_.find(player) != it->candidates_.end())
[8434]279                i++;
280        }
281        return i;
282    }
283
284    Dock* Dock::getActiveDockAtIndex(unsigned int index)
285    {
286        PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer();
287        for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it)
288        {
[8700]289            if(it->candidates_.find(player) != it->candidates_.end())
[8434]290            {
291                if(index == 0)
292                    return *it;
293                index--;
294            }
295        }
296        return NULL;
297    }
298
299    bool Dock::addEffect(DockingEffect* effect)
300    {
[8185]301        assert(effect);
[8700]302        effects_.push_back(effect);
[8185]303        return true;
304    }
305
[8493]306    const DockingEffect* Dock::getEffect(unsigned int i) const
[8434]307    {
[8700]308        for (std::list<DockingEffect*>::const_iterator effect = this->effects_.begin(); effect != this->effects_.end(); ++effect)
[8434]309        {
[8151]310            if(i == 0)
311               return *effect;
312            i--;
313        }
314        return NULL;
[8185]315    }
[8493]316
317    bool Dock::addAnimation(DockingAnimation* animation)
318    {
319        assert(animation);
320        animation->setParent(this);
[8700]321        animations_.push_back(animation);
[8493]322        return true;
323    }
324
325    const DockingAnimation* Dock::getAnimation(unsigned int i) const
326    {
[8700]327        for (std::list<DockingAnimation*>::const_iterator animation = this->animations_.begin(); animation != this->animations_.end(); ++animation)
[8493]328        {
329            if(i == 0)
330               return *animation;
331            i--;
332        }
333        return NULL;
334    }
[8137]335}
Note: See TracBrowser for help on using the repository browser.