Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11919 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • 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"
[10624]39#include "core/command/ConsoleCommandIncludes.h"
40#include "network/NetworkFunctionIncludes.h"
[8705]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);
[11071]89        PlayerInfo* player = nullptr;
[9939]90
[9945]91        // Check whether it is a player trigger and extract pawn from it
[11071]92        if(pTrigger != nullptr)
[9945]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        }
[11071]105        if(player == nullptr)
[9945]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);
[11071]133        PlayerInfo* player = nullptr;
[8186]134
[8188]135        // Check whether it is a player trigger and extract pawn from it
[11071]136        if(pTrigger != nullptr)
[8186]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        }
[11071]149        if(player == nullptr)
[8186]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
[10624]186                callStaticNetworkFunction(&Dock::showDockingDialog, player->getClientID());
[9939]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
[10624]203            callStaticNetworkFunction(&Dock::showDockingDialog, player->getClientID());
[8705]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();
[11071]216        for(Dock* dock : ObjectList<Dock>())
[8434]217        {
[11071]218            if(dock->dock(player))
[8382]219                break;
220        }
221    }
222
[8434]223    void Dock::cmdUndock()
224    {
[8382]225        PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer();
[11071]226        for(Dock* dock : ObjectList<Dock>())
[8434]227        {
[11071]228            if(dock->undock(player))
[8382]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();
[11071]297        for(Dock* dock : ObjectList<Dock>())
[8434]298        {
[11071]299            if(dock->candidates_.find(player) != dock->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();
[11071]308        for(Dock* dock : ObjectList<Dock>())
[8434]309        {
[11071]310            if(dock->candidates_.find(player) != dock->candidates_.end())
[8434]311            {
312                if(index == 0)
[11071]313                    return dock;
[8434]314                index--;
315            }
316        }
[11071]317        return nullptr;
[8434]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    {
[11071]329        for (DockingEffect* effect : this->effects_)
[8434]330        {
[8151]331            if(i == 0)
[11071]332               return effect;
[8151]333            i--;
334        }
[11071]335        return nullptr;
[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    {
[11071]348        for (DockingAnimation* animation : this->animations_)
[8493]349        {
350            if(i == 0)
[11071]351               return animation;
[8493]352            i--;
353        }
[11071]354        return nullptr;
[8493]355    }
[8137]356}
Note: See TracBrowser for help on using the repository browser.