Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/docking/Dock.cc @ 10562

Last change on this file since 10562 was 9945, checked in by landauf, 11 years ago

replaced tabs with spaces. no changes in code

  • Property svn:eol-style set to native
File size: 10.5 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);
[9939]71        XMLPortEventSink(Dock, BaseObject, "undocking", undocking, xmlelement, mode);
72
[8137]73    }
74
75    void Dock::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
76    {
77        SUPER(Dock, XMLEventPort, xmlelement, mode);
78
79        XMLPortEventSink(Dock, BaseObject, "execute", execute, xmlelement, mode);
[9939]80
81        XMLPortEventSink(Dock, BaseObject, "undocking", undocking, xmlelement, mode);
[8137]82    }
83
[9939]84
85    bool Dock::undocking(bool bTriggered, BaseObject* trigger)
86    {
87
[9945]88        PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger);
89        PlayerInfo* player = NULL;
[9939]90
[9945]91        // Check whether it is a player trigger and extract pawn from it
92        if(pTrigger != NULL)
93        {
94            if(!pTrigger->isForPlayer()) {  // The PlayerTrigger is not exclusively for Pawns which means we cannot extract one.
95            orxout(verbose, context::docking) << "Docking:execute PlayerTrigger was not triggered by a player.." << endl;
96            return false;
97            }
98            player = pTrigger->getTriggeringPlayer();
99        }
100        else
101        {
102            orxout(verbose, context::docking) << "Docking::execute Not a player trigger, can't extract pawn from it.." << endl;
103            return false;
104        }
105        if(player == NULL)
106        {
107            orxout(verbose, context::docking) << "Docking::execute Can't retrieve PlayerInfo from Trigger. (" << trigger->getIdentifier()->getName() << ")" << endl;
108            return false;
109        }
[9939]110
[9945]111        if(bTriggered)
112        {
113            // Add player to this Docks candidates
114            candidates_.insert(player);
[9939]115
[9945]116            // Show docking dialog
117            this->showUndockingDialogHelper(player);
118        }
119        else
120        {
121            // Remove player from candidates list
122            candidates_.erase(player);
123        }
[9939]124
[9945]125        return true;
[9939]126    }
127
128
129
[8137]130    bool Dock::execute(bool bTriggered, BaseObject* trigger)
[8185]131    {
[8186]132        PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger);
[8667]133        PlayerInfo* player = NULL;
[8186]134
[8188]135        // Check whether it is a player trigger and extract pawn from it
[8186]136        if(pTrigger != NULL)
137        {
[8188]138            if(!pTrigger->isForPlayer()) {  // The PlayerTrigger is not exclusively for Pawns which means we cannot extract one.
[8858]139                orxout(verbose, context::docking) << "Docking:execute PlayerTrigger was not triggered by a player.." << endl;
[8186]140                return false;
[8188]141            }
[8667]142            player = pTrigger->getTriggeringPlayer();
[8493]143        }
144        else
145        {
[8858]146            orxout(verbose, context::docking) << "Docking::execute Not a player trigger, can't extract pawn from it.." << endl;
[8188]147            return false;
[8186]148        }
149        if(player == NULL)
150        {
[8858]151            orxout(verbose, context::docking) << "Docking::execute Can't retrieve PlayerInfo from Trigger. (" << trigger->getIdentifier()->getName() << ")" << endl;
[8186]152            return false;
153        }
154
[8434]155        if(bTriggered)
156        {
[8382]157            // Add player to this Docks candidates
[8700]158            candidates_.insert(player);
[8382]159
[8434]160            // Show docking dialog
[8705]161            this->showDockingDialogHelper(player);
[8434]162        }
163        else
164        {
[8382]165            // Remove player from candidates list
[8700]166            candidates_.erase(player);
[8185]167        }
168
169        return true;
170    }
171
[9939]172
173    void Dock::showUndockingDialogHelper(PlayerInfo* player)
174        {
175            assert(player);
176
177            if(!player->isHumanPlayer())
178                return;
179
180            if(GameMode::isMaster())
181            {
182                if(GameMode::showsGraphics())
183                    GUIManager::showGUI("UndockingDialog");
184            }
185            else
186                callStaticNetworkFunction(Dock::showDockingDialog, player->getClientID());
187
188        }
189
[8705]190    void Dock::showDockingDialogHelper(PlayerInfo* player)
191    {
192        assert(player);
193       
194        if(!player->isHumanPlayer())
195            return;
196       
197        if(GameMode::isMaster())
198        {
199            if(GameMode::showsGraphics())
200                GUIManager::showGUI("DockingDialog");
201        }
202        else
203            callStaticNetworkFunction(Dock::showDockingDialog, player->getClientID());
204
205    }
206
207    /*static*/ void Dock::showDockingDialog()
208    {
209        if(GameMode::showsGraphics())
210            GUIManager::showGUI("DockingDialog");
211    }
212
[8434]213    void Dock::cmdDock()
214    {
[8382]215        PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer();
[8434]216        for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it)
217        {
[8382]218            if(it->dock(player))
219                break;
220        }
221    }
222
[8434]223    void Dock::cmdUndock()
224    {
[8382]225        PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer();
[8434]226        for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it)
227        {
[8382]228            if(it->undock(player))
229                break;
230        }
231    }
232
[8434]233    bool Dock::dock(PlayerInfo* player)
234    {
[8382]235        // Check if player is a candidate
[8700]236        if(candidates_.find(player) == candidates_.end())
[8434]237        {
[8858]238            orxout(internal_warning, context::docking) << "Dock::dock Player is not a candidate!" << endl;
[8382]239            return false;
240        }
241
[8700]242        candidates_.erase(player);
243        docked_.insert(player);
[8493]244
[8700]245        if (animations_.empty())
[8493]246            return dockingAnimationFinished(player);
[9939]247
[8493]248        else
[8700]249            DockingAnimation::invokeAnimation(true, player, animations_);
[8493]250
[8382]251        return true;
252    }
253
[8493]254    bool Dock::dockingAnimationFinished(PlayerInfo* player)
255    {
[8700]256        if(docked_.find(player) == docked_.end())
[8493]257        {
[8858]258            orxout(internal_warning, context::docking) << "Dock::dockingAnimationFinished Player is not currently docked." << endl;
[8493]259            return false;
260        }
261
[8700]262        DockingEffect::invokeEffect(true, player, effects_);
[8493]263        return true;
264    }
265
[8434]266    bool Dock::undock(PlayerInfo* player)
267    {
[8382]268        // Check if player is docked to this Dock
[8700]269        if(docked_.find(player) == docked_.end())
[8434]270        {
[8858]271            orxout(internal_warning, context::docking) << "Dock::undock Player is not docked to this Dock." << endl;
[8382]272            return false;
273        }
274
[8700]275        docked_.erase(player);
276        candidates_.insert(player);
[8493]277
[8700]278        DockingEffect::invokeEffect(false, player, effects_);
[8493]279
[8700]280        if (animations_.empty())
[8493]281            return undockingAnimationFinished(player);
282        else
[8700]283            DockingAnimation::invokeAnimation(false, player, animations_);
[8493]284
[8382]285        return true;
286    }
287
[8493]288    bool Dock::undockingAnimationFinished(PlayerInfo* player) {
[8858]289        orxout(verbose, context::docking) << "Dock::undockingAnimationFinished executed" << endl;
[8493]290        return true;
291    }
[8382]292
[8434]293    unsigned int Dock::getNumberOfActiveDocks()
294    {
295        int i = 0;
296        PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer();
297        for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it)
298        {
[8700]299            if(it->candidates_.find(player) != it->candidates_.end())
[8434]300                i++;
301        }
302        return i;
303    }
304
305    Dock* Dock::getActiveDockAtIndex(unsigned int index)
306    {
307        PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer();
308        for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it)
309        {
[8700]310            if(it->candidates_.find(player) != it->candidates_.end())
[8434]311            {
312                if(index == 0)
313                    return *it;
314                index--;
315            }
316        }
317        return NULL;
318    }
319
320    bool Dock::addEffect(DockingEffect* effect)
321    {
[8185]322        assert(effect);
[8700]323        effects_.push_back(effect);
[8185]324        return true;
325    }
326
[8493]327    const DockingEffect* Dock::getEffect(unsigned int i) const
[8434]328    {
[8700]329        for (std::list<DockingEffect*>::const_iterator effect = this->effects_.begin(); effect != this->effects_.end(); ++effect)
[8434]330        {
[8151]331            if(i == 0)
332               return *effect;
333            i--;
334        }
335        return NULL;
[8185]336    }
[8493]337
338    bool Dock::addAnimation(DockingAnimation* animation)
339    {
340        assert(animation);
341        animation->setParent(this);
[8700]342        animations_.push_back(animation);
[8493]343        return true;
344    }
345
346    const DockingAnimation* Dock::getAnimation(unsigned int i) const
347    {
[8700]348        for (std::list<DockingAnimation*>::const_iterator animation = this->animations_.begin(); animation != this->animations_.end(); ++animation)
[8493]349        {
350            if(i == 0)
351               return *animation;
352            i--;
353        }
354        return NULL;
355    }
[8137]356}
Note: See TracBrowser for help on using the repository browser.