Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9687 was 9667, checked in by landauf, 11 years ago

merged core6 back to trunk

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