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