Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9215 was 8705, checked in by dafrick, 14 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
Line 
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
31    @brief Docking system main class
32*/
33
34#include "Dock.h"
35
36#include "core/CoreIncludes.h"
37#include "core/LuaState.h"
38#include "core/GUIManager.h"
39#include "core/command/ConsoleCommand.h"
40#include "network/NetworkFunction.h"
41
42#include "infos/HumanPlayer.h"
43#include "interfaces/PlayerTrigger.h"
44#include "worldentities/pawns/Pawn.h"
45
46#include "ToluaBindDocking.h"
47
48namespace orxonox
49{
50    // Register tolua_open function when loading the library
51    DeclareToluaInterface(Docking);
52
53    CreateFactory(Dock);
54
55    SetConsoleCommand("Dock", "dock",    &Dock::cmdDock).addShortcut().setAsInputCommand();
56    SetConsoleCommand("Dock", "undock",  &Dock::cmdUndock).addShortcut().setAsInputCommand();
57
58    registerStaticNetworkFunction(Dock::showDockingDialog);
59
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
73        XMLPortObject(Dock, DockingEffect, "effects", addEffect, getEffect, xmlelement, mode);
74        XMLPortObject(Dock, DockingAnimation, "animations", addAnimation, getAnimation, xmlelement, mode);
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)
86    {
87        PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger);
88        PlayerInfo* player = NULL;
89
90        // Check whether it is a player trigger and extract pawn from it
91        if(pTrigger != NULL)
92        {
93            if(!pTrigger->isForPlayer()) {  // The PlayerTrigger is not exclusively for Pawns which means we cannot extract one.
94                COUT(4) << "Docking:execute PlayerTrigger was not triggered by a player.." << std::endl;
95                return false;
96            }
97            player = pTrigger->getTriggeringPlayer();
98        }
99        else
100        {
101            COUT(4) << "Docking::execute Not a player trigger, can't extract pawn from it.." << std::endl;
102            return false;
103        }
104        if(player == NULL)
105        {
106            COUT(4) << "Docking::execute Can't retrieve PlayerInfo from Trigger. (" << trigger->getIdentifier()->getName() << ")" << std::endl;
107            return false;
108        }
109
110        if(bTriggered)
111        {
112            // Add player to this Docks candidates
113            candidates_.insert(player);
114
115            // Show docking dialog
116            this->showDockingDialogHelper(player);
117        }
118        else
119        {
120            // Remove player from candidates list
121            candidates_.erase(player);
122        }
123
124        return true;
125    }
126
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
150    void Dock::cmdDock()
151    {
152        PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer();
153        for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it)
154        {
155            if(it->dock(player))
156                break;
157        }
158    }
159
160    void Dock::cmdUndock()
161    {
162        PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer();
163        for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it)
164        {
165            if(it->undock(player))
166                break;
167        }
168    }
169
170    bool Dock::dock(PlayerInfo* player)
171    {
172        // Check if player is a candidate
173        if(candidates_.find(player) == candidates_.end())
174        {
175            COUT(2) << "Dock::dock Player is not a candidate!" << std::endl;
176            return false;
177        }
178
179        candidates_.erase(player);
180        docked_.insert(player);
181
182        if (animations_.empty())
183            return dockingAnimationFinished(player);
184        else
185            DockingAnimation::invokeAnimation(true, player, animations_);
186
187        return true;
188    }
189
190    bool Dock::dockingAnimationFinished(PlayerInfo* player)
191    {
192        if(docked_.find(player) == docked_.end())
193        {
194            COUT(2) << "Dock::dockingAnimationFinished Player is not currently docked." << std::endl;
195            return false;
196        }
197
198        DockingEffect::invokeEffect(true, player, effects_);
199        return true;
200    }
201
202    bool Dock::undock(PlayerInfo* player)
203    {
204        // Check if player is docked to this Dock
205        if(docked_.find(player) == docked_.end())
206        {
207            COUT(2) << "Dock::undock Player is not docked to this Dock." << std::endl;
208            return false;
209        }
210
211        docked_.erase(player);
212        candidates_.insert(player);
213
214        DockingEffect::invokeEffect(false, player, effects_);
215
216        if (animations_.empty())
217            return undockingAnimationFinished(player);
218        else
219            DockingAnimation::invokeAnimation(false, player, animations_);
220
221        return true;
222    }
223
224    bool Dock::undockingAnimationFinished(PlayerInfo* player) {
225        COUT(4) << "Dock::undockingAnimationFinished executed" << std::endl;
226        return true;
227    }
228
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        {
235            if(it->candidates_.find(player) != it->candidates_.end())
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        {
246            if(it->candidates_.find(player) != it->candidates_.end())
247            {
248                if(index == 0)
249                    return *it;
250                index--;
251            }
252        }
253        return NULL;
254    }
255
256    bool Dock::addEffect(DockingEffect* effect)
257    {
258        assert(effect);
259        effects_.push_back(effect);
260        return true;
261    }
262
263    const DockingEffect* Dock::getEffect(unsigned int i) const
264    {
265        for (std::list<DockingEffect*>::const_iterator effect = this->effects_.begin(); effect != this->effects_.end(); ++effect)
266        {
267            if(i == 0)
268               return *effect;
269            i--;
270        }
271        return NULL;
272    }
273
274    bool Dock::addAnimation(DockingAnimation* animation)
275    {
276        assert(animation);
277        animation->setParent(this);
278        animations_.push_back(animation);
279        return true;
280    }
281
282    const DockingAnimation* Dock::getAnimation(unsigned int i) const
283    {
284        for (std::list<DockingAnimation*>::const_iterator animation = this->animations_.begin(); animation != this->animations_.end(); ++animation)
285        {
286            if(i == 0)
287               return *animation;
288            i--;
289        }
290        return NULL;
291    }
292}
Note: See TracBrowser for help on using the repository browser.