Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/spacestationentry/src/modules/docking/Dock.cc @ 9797

Last change on this file since 9797 was 9789, checked in by agermann, 12 years ago

Erstellung einer neuen Funktion execute2 in Dock.cc um ein Ausdocken zu realisieren.

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