Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/dockingsystem2/src/modules/docking/Dock.cc @ 8383

Last change on this file since 8383 was 8382, checked in by sven, 14 years ago

Added dock/undock console commands.

File size: 5.9 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 "infos/HumanPlayer.h"
37#include "worldentities/pawns/Pawn.h"
38#include "interfaces/PlayerTrigger.h"
39#include "controllers/HumanController.h"
40#include "core/command/ConsoleCommand.h"
41
42
43
44namespace orxonox
45{
46    CreateFactory(Dock);
47
48    SetConsoleCommand("Dock", "dock",    &Dock::cmdDock).addShortcut().setAsInputCommand();
49    SetConsoleCommand("Dock", "undock",  &Dock::cmdUndock).addShortcut().setAsInputCommand();
50
51    Dock::Dock(BaseObject* creator) : StaticEntity(creator)
52    {
53        RegisterObject(Dock);
54        COUT(0) << "Registering dock..." << std::endl;
55    }
56
57    Dock::~Dock()
58    {
59    }
60
61
62    void Dock::XMLPort(Element& xmlelement, XMLPort::Mode mode)
63    {
64        SUPER(Dock, XMLPort, xmlelement, mode);
65
66        XMLPortObject(Dock, DockingEffect, "effects", addEffect, getEffect, xmlelement, mode);
67        XMLPortEventSink(Dock, BaseObject, "execute", execute, xmlelement, mode);
68
69        COUT(0) << "Dock created.." << std::endl;
70    }
71
72    void Dock::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
73    {
74        SUPER(Dock, XMLEventPort, xmlelement, mode);
75
76        XMLPortEventSink(Dock, BaseObject, "execute", execute, xmlelement, mode);
77    }
78
79
80    bool Dock::execute(bool bTriggered, BaseObject* trigger)
81    {
82        PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger);
83        Pawn* pawn = NULL;
84
85        // Check whether it is a player trigger and extract pawn from it
86        if(pTrigger != NULL)
87        {
88            if(!pTrigger->isForPlayer()) {  // The PlayerTrigger is not exclusively for Pawns which means we cannot extract one.
89                COUT(2) << "Docking:execute PlayerTrigger was not triggered by a player.." << std::endl;
90                return false;
91            }
92            pawn = pTrigger->getTriggeringPlayer();
93        } else {
94            COUT(2) << "Docking::execute Not a player trigger, can't extract pawn from it.." << std::endl;
95            return false;
96        }
97        if(pawn == NULL)
98        {
99            COUT(2) << "Docking::execute Can't retrieve Pawn from Trigger. (" << trigger->getIdentifier()->getName() << ")" << std::endl;
100            return false;
101        }
102
103        // Extract the PlayerInfo from the Pawn.
104        PlayerInfo* player = pawn->getPlayer();
105        if(player == NULL)
106        {
107            COUT(2) << "The PlayerInfo* is NULL." << std::endl;
108            return false;
109        }
110
111        COUT(0) << "Dock triggered by player: " << player->getName() << ".." << std::endl;
112
113        if(bTriggered) {
114            // Add player to this Docks candidates
115            candidates.insert(player);
116
117            //DockingEffect::invokeEffect(docking::DOCKING, player, effects_);
118        } else {
119            // Remove player from candidates list
120            candidates.erase(player);
121
122            //DockingEffect::invokeEffect(docking::RELEASE, player, effects_);
123        }
124
125        return true;
126    }
127
128
129    void Dock::cmdDock() {
130        PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer();
131        for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it) {
132            if(it->dock(player))
133                break;
134        }
135    }
136
137    void Dock::cmdUndock() {
138        PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer();
139        for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it) {
140            if(it->undock(player))
141                break;
142        }
143    }
144
145
146    bool Dock::dock(PlayerInfo* player) {
147        // Check if player is a candidate
148        if(candidates.find(player) == candidates.end()) {
149            COUT(0) << "Player is not a candidate!";
150            return false;
151        }
152
153        // Remove player from candidates, add to docked players and invoke docking effect
154        candidates.erase(player);
155        docked.insert(player);
156        DockingEffect::invokeEffect(docking::ATTACH, player, effects);
157        return true;
158    }
159
160    bool Dock::undock(PlayerInfo* player) {
161        // Check if player is docked to this Dock
162        if(docked.find(player) == docked.end()) {
163            COUT(0) << "Player is not docked to this Dock." << std::endl;
164            return false;
165        }
166
167        // Remove player from docked, add to candidates and reverse DockingEffect
168        docked.erase(player);
169        candidates.insert(player);
170        DockingEffect::invokeEffect(docking::RELEASE, player, effects);
171        return true;
172    }
173
174
175    bool Dock::addEffect(DockingEffect* effect) {
176        assert(effect);
177        effects.push_back(effect);
178        return true;
179    }
180
181    const DockingEffect* Dock::getEffect(unsigned int index) const {
182        int i = index;
183        for (std::list<DockingEffect*>::const_iterator effect = this->effects.begin(); effect != this->effects.end(); ++effect) {
184            if(i == 0)
185               return *effect;
186            i--;
187        }
188        return NULL;
189    }
190}
Note: See TracBrowser for help on using the repository browser.