Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/modules/docking/Dock.cc @ 8974

Last change on this file since 8974 was 8705, checked in by dafrick, 13 years ago

The calling of tthe docking dialog is now synchronized (but not the docking).

  • Property svn:eol-style set to native
File size: 8.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
[8434]46#include "ToluaBindDocking.h"
[8137]47
48namespace orxonox
49{
[8434]50    // Register tolua_open function when loading the library
51    DeclareToluaInterface(Docking);
52
[8185]53    CreateFactory(Dock);
[8137]54
[8382]55    SetConsoleCommand("Dock", "dock",    &Dock::cmdDock).addShortcut().setAsInputCommand();
56    SetConsoleCommand("Dock", "undock",  &Dock::cmdUndock).addShortcut().setAsInputCommand();
57
[8705]58    registerStaticNetworkFunction(Dock::showDockingDialog);
59
[8137]60    Dock::Dock(BaseObject* creator) : StaticEntity(creator)
61    {
62        RegisterObject(Dock);
63    }
64
65    Dock::~Dock()
66    {
67    }
68
69    void Dock::XMLPort(Element& xmlelement, XMLPort::Mode mode)
70    {
71        SUPER(Dock, XMLPort, xmlelement, mode);
72
[8151]73        XMLPortObject(Dock, DockingEffect, "effects", addEffect, getEffect, xmlelement, mode);
[8493]74        XMLPortObject(Dock, DockingAnimation, "animations", addAnimation, getAnimation, xmlelement, mode);
[8137]75        XMLPortEventSink(Dock, BaseObject, "execute", execute, xmlelement, mode);
76    }
77
78    void Dock::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
79    {
80        SUPER(Dock, XMLEventPort, xmlelement, mode);
81
82        XMLPortEventSink(Dock, BaseObject, "execute", execute, xmlelement, mode);
83    }
84
85    bool Dock::execute(bool bTriggered, BaseObject* trigger)
[8185]86    {
[8186]87        PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger);
[8667]88        PlayerInfo* player = NULL;
[8186]89
[8188]90        // Check whether it is a player trigger and extract pawn from it
[8186]91        if(pTrigger != NULL)
92        {
[8188]93            if(!pTrigger->isForPlayer()) {  // The PlayerTrigger is not exclusively for Pawns which means we cannot extract one.
[8560]94                COUT(4) << "Docking:execute PlayerTrigger was not triggered by a player.." << std::endl;
[8186]95                return false;
[8188]96            }
[8667]97            player = pTrigger->getTriggeringPlayer();
[8493]98        }
99        else
100        {
[8560]101            COUT(4) << "Docking::execute Not a player trigger, can't extract pawn from it.." << std::endl;
[8188]102            return false;
[8186]103        }
104        if(player == NULL)
105        {
[8667]106            COUT(4) << "Docking::execute Can't retrieve PlayerInfo from Trigger. (" << trigger->getIdentifier()->getName() << ")" << std::endl;
[8186]107            return false;
108        }
109
[8434]110        if(bTriggered)
111        {
[8382]112            // Add player to this Docks candidates
[8700]113            candidates_.insert(player);
[8382]114
[8434]115            // Show docking dialog
[8705]116            this->showDockingDialogHelper(player);
[8434]117        }
118        else
119        {
[8382]120            // Remove player from candidates list
[8700]121            candidates_.erase(player);
[8185]122        }
123
124        return true;
125    }
126
[8705]127    void Dock::showDockingDialogHelper(PlayerInfo* player)
128    {
129        assert(player);
130       
131        if(!player->isHumanPlayer())
132            return;
133       
134        if(GameMode::isMaster())
135        {
136            if(GameMode::showsGraphics())
137                GUIManager::showGUI("DockingDialog");
138        }
139        else
140            callStaticNetworkFunction(Dock::showDockingDialog, player->getClientID());
141
142    }
143
144    /*static*/ void Dock::showDockingDialog()
145    {
146        if(GameMode::showsGraphics())
147            GUIManager::showGUI("DockingDialog");
148    }
149
[8434]150    void Dock::cmdDock()
151    {
[8382]152        PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer();
[8434]153        for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it)
154        {
[8382]155            if(it->dock(player))
156                break;
157        }
158    }
159
[8434]160    void Dock::cmdUndock()
161    {
[8382]162        PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer();
[8434]163        for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it)
164        {
[8382]165            if(it->undock(player))
166                break;
167        }
168    }
169
[8434]170    bool Dock::dock(PlayerInfo* player)
171    {
[8382]172        // Check if player is a candidate
[8700]173        if(candidates_.find(player) == candidates_.end())
[8434]174        {
[8560]175            COUT(2) << "Dock::dock Player is not a candidate!" << std::endl;
[8382]176            return false;
177        }
178
[8700]179        candidates_.erase(player);
180        docked_.insert(player);
[8493]181
[8700]182        if (animations_.empty())
[8493]183            return dockingAnimationFinished(player);
184        else
[8700]185            DockingAnimation::invokeAnimation(true, player, animations_);
[8493]186
[8382]187        return true;
188    }
189
[8493]190    bool Dock::dockingAnimationFinished(PlayerInfo* player)
191    {
[8700]192        if(docked_.find(player) == docked_.end())
[8493]193        {
[8560]194            COUT(2) << "Dock::dockingAnimationFinished Player is not currently docked." << std::endl;
[8493]195            return false;
196        }
197
[8700]198        DockingEffect::invokeEffect(true, player, effects_);
[8493]199        return true;
200    }
201
[8434]202    bool Dock::undock(PlayerInfo* player)
203    {
[8382]204        // Check if player is docked to this Dock
[8700]205        if(docked_.find(player) == docked_.end())
[8434]206        {
[8560]207            COUT(2) << "Dock::undock Player is not docked to this Dock." << std::endl;
[8382]208            return false;
209        }
210
[8700]211        docked_.erase(player);
212        candidates_.insert(player);
[8493]213
[8700]214        DockingEffect::invokeEffect(false, player, effects_);
[8493]215
[8700]216        if (animations_.empty())
[8493]217            return undockingAnimationFinished(player);
218        else
[8700]219            DockingAnimation::invokeAnimation(false, player, animations_);
[8493]220
[8382]221        return true;
222    }
223
[8493]224    bool Dock::undockingAnimationFinished(PlayerInfo* player) {
[8560]225        COUT(4) << "Dock::undockingAnimationFinished executed" << std::endl;
[8493]226        return true;
227    }
[8382]228
[8434]229    unsigned int Dock::getNumberOfActiveDocks()
230    {
231        int i = 0;
232        PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer();
233        for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it)
234        {
[8700]235            if(it->candidates_.find(player) != it->candidates_.end())
[8434]236                i++;
237        }
238        return i;
239    }
240
241    Dock* Dock::getActiveDockAtIndex(unsigned int index)
242    {
243        PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer();
244        for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it)
245        {
[8700]246            if(it->candidates_.find(player) != it->candidates_.end())
[8434]247            {
248                if(index == 0)
249                    return *it;
250                index--;
251            }
252        }
253        return NULL;
254    }
255
256    bool Dock::addEffect(DockingEffect* effect)
257    {
[8185]258        assert(effect);
[8700]259        effects_.push_back(effect);
[8185]260        return true;
261    }
262
[8493]263    const DockingEffect* Dock::getEffect(unsigned int i) const
[8434]264    {
[8700]265        for (std::list<DockingEffect*>::const_iterator effect = this->effects_.begin(); effect != this->effects_.end(); ++effect)
[8434]266        {
[8151]267            if(i == 0)
268               return *effect;
269            i--;
270        }
271        return NULL;
[8185]272    }
[8493]273
274    bool Dock::addAnimation(DockingAnimation* animation)
275    {
276        assert(animation);
277        animation->setParent(this);
[8700]278        animations_.push_back(animation);
[8493]279        return true;
280    }
281
282    const DockingAnimation* Dock::getAnimation(unsigned int i) const
283    {
[8700]284        for (std::list<DockingAnimation*>::const_iterator animation = this->animations_.begin(); animation != this->animations_.end(); ++animation)
[8493]285        {
286            if(i == 0)
287               return *animation;
288            i--;
289        }
290        return NULL;
291    }
[8137]292}
Note: See TracBrowser for help on using the repository browser.