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 | |
---|
46 | namespace 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 | COUT(0) << "Registering dock..." << std::endl; |
---|
60 | } |
---|
61 | |
---|
62 | Dock::~Dock() |
---|
63 | { |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | void Dock::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
68 | { |
---|
69 | SUPER(Dock, XMLPort, xmlelement, mode); |
---|
70 | |
---|
71 | XMLPortObject(Dock, DockingEffect, "effects", addEffect, getEffect, xmlelement, mode); |
---|
72 | XMLPortEventSink(Dock, BaseObject, "execute", execute, xmlelement, mode); |
---|
73 | |
---|
74 | COUT(0) << "Dock created.." << std::endl; |
---|
75 | } |
---|
76 | |
---|
77 | void Dock::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) |
---|
78 | { |
---|
79 | SUPER(Dock, XMLEventPort, xmlelement, mode); |
---|
80 | |
---|
81 | XMLPortEventSink(Dock, BaseObject, "execute", execute, xmlelement, mode); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | bool Dock::execute(bool bTriggered, BaseObject* trigger) |
---|
86 | { |
---|
87 | PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger); |
---|
88 | Pawn* pawn = 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(2) << "Docking:execute PlayerTrigger was not triggered by a player.." << std::endl; |
---|
95 | return false; |
---|
96 | } |
---|
97 | pawn = pTrigger->getTriggeringPlayer(); |
---|
98 | } else { |
---|
99 | COUT(2) << "Docking::execute Not a player trigger, can't extract pawn from it.." << std::endl; |
---|
100 | return false; |
---|
101 | } |
---|
102 | if(pawn == NULL) |
---|
103 | { |
---|
104 | COUT(2) << "Docking::execute Can't retrieve Pawn from Trigger. (" << trigger->getIdentifier()->getName() << ")" << std::endl; |
---|
105 | return false; |
---|
106 | } |
---|
107 | |
---|
108 | // Extract the PlayerInfo from the Pawn. |
---|
109 | PlayerInfo* player = pawn->getPlayer(); |
---|
110 | if(player == NULL) |
---|
111 | { |
---|
112 | COUT(2) << "The PlayerInfo* is NULL." << std::endl; |
---|
113 | return false; |
---|
114 | } |
---|
115 | |
---|
116 | COUT(0) << "Dock triggered by player: " << player->getName() << ".." << std::endl; |
---|
117 | |
---|
118 | if(bTriggered) |
---|
119 | { |
---|
120 | // Add player to this Docks candidates |
---|
121 | candidates.insert(player); |
---|
122 | |
---|
123 | // Show docking dialog |
---|
124 | GUIManager::showGUI("DockingDialog"); |
---|
125 | //DockingEffect::invokeEffect(docking::DOCKING, player, effects_); |
---|
126 | } |
---|
127 | else |
---|
128 | { |
---|
129 | // Remove player from candidates list |
---|
130 | candidates.erase(player); |
---|
131 | |
---|
132 | //DockingEffect::invokeEffect(docking::RELEASE, player, effects_); |
---|
133 | } |
---|
134 | |
---|
135 | return true; |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | void Dock::cmdDock() |
---|
140 | { |
---|
141 | PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer(); |
---|
142 | for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it) |
---|
143 | { |
---|
144 | if(it->dock(player)) |
---|
145 | break; |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | void Dock::cmdUndock() |
---|
150 | { |
---|
151 | PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer(); |
---|
152 | for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it) |
---|
153 | { |
---|
154 | if(it->undock(player)) |
---|
155 | break; |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | bool Dock::dock(PlayerInfo* player) |
---|
161 | { |
---|
162 | // Check if player is a candidate |
---|
163 | if(candidates.find(player) == candidates.end()) |
---|
164 | { |
---|
165 | COUT(0) << "Player is not a candidate!" << std::endl; |
---|
166 | return false; |
---|
167 | } |
---|
168 | |
---|
169 | // Remove player from candidates, add to docked players and invoke docking effect |
---|
170 | candidates.erase(player); |
---|
171 | docked.insert(player); |
---|
172 | DockingEffect::invokeEffect(docking::ATTACH, player, effects); |
---|
173 | return true; |
---|
174 | } |
---|
175 | |
---|
176 | bool Dock::undock(PlayerInfo* player) |
---|
177 | { |
---|
178 | // Check if player is docked to this Dock |
---|
179 | if(docked.find(player) == docked.end()) |
---|
180 | { |
---|
181 | COUT(0) << "Player is not docked to this Dock." << std::endl; |
---|
182 | return false; |
---|
183 | } |
---|
184 | |
---|
185 | // Remove player from docked, add to candidates and reverse DockingEffect |
---|
186 | docked.erase(player); |
---|
187 | candidates.insert(player); |
---|
188 | DockingEffect::invokeEffect(docking::RELEASE, player, effects); |
---|
189 | return true; |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | unsigned int Dock::getNumberOfActiveDocks() |
---|
194 | { |
---|
195 | int i = 0; |
---|
196 | PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer(); |
---|
197 | for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it) |
---|
198 | { |
---|
199 | if(it->candidates.find(player) != it->candidates.end()) |
---|
200 | i++; |
---|
201 | } |
---|
202 | return i; |
---|
203 | } |
---|
204 | |
---|
205 | Dock* Dock::getActiveDockAtIndex(unsigned int index) |
---|
206 | { |
---|
207 | PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer(); |
---|
208 | for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it) |
---|
209 | { |
---|
210 | if(it->candidates.find(player) != it->candidates.end()) |
---|
211 | { |
---|
212 | if(index == 0) |
---|
213 | return *it; |
---|
214 | index--; |
---|
215 | } |
---|
216 | } |
---|
217 | return NULL; |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | bool Dock::addEffect(DockingEffect* effect) |
---|
222 | { |
---|
223 | assert(effect); |
---|
224 | effects.push_back(effect); |
---|
225 | return true; |
---|
226 | } |
---|
227 | |
---|
228 | const DockingEffect* Dock::getEffect(unsigned int index) const |
---|
229 | { |
---|
230 | int i = index; |
---|
231 | for (std::list<DockingEffect*>::const_iterator effect = this->effects.begin(); effect != this->effects.end(); ++effect) |
---|
232 | { |
---|
233 | if(i == 0) |
---|
234 | return *effect; |
---|
235 | i--; |
---|
236 | } |
---|
237 | return NULL; |
---|
238 | } |
---|
239 | } |
---|