Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Adherence to styleguide.

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