[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 | void Dock::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 66 | { |
---|
| 67 | SUPER(Dock, XMLPort, xmlelement, mode); |
---|
| 68 | |
---|
[8151] | 69 | XMLPortObject(Dock, DockingEffect, "effects", addEffect, getEffect, xmlelement, mode); |
---|
[8493] | 70 | XMLPortObject(Dock, DockingAnimation, "animations", addAnimation, getAnimation, xmlelement, mode); |
---|
[8137] | 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) |
---|
[8185] | 82 | { |
---|
[8186] | 83 | PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger); |
---|
[8667] | 84 | PlayerInfo* player = NULL; |
---|
[8186] | 85 | |
---|
[8188] | 86 | // Check whether it is a player trigger and extract pawn from it |
---|
[8186] | 87 | if(pTrigger != NULL) |
---|
| 88 | { |
---|
[8188] | 89 | if(!pTrigger->isForPlayer()) { // The PlayerTrigger is not exclusively for Pawns which means we cannot extract one. |
---|
[8560] | 90 | COUT(4) << "Docking:execute PlayerTrigger was not triggered by a player.." << std::endl; |
---|
[8186] | 91 | return false; |
---|
[8188] | 92 | } |
---|
[8667] | 93 | player = pTrigger->getTriggeringPlayer(); |
---|
[8493] | 94 | } |
---|
| 95 | else |
---|
| 96 | { |
---|
[8560] | 97 | COUT(4) << "Docking::execute Not a player trigger, can't extract pawn from it.." << std::endl; |
---|
[8188] | 98 | return false; |
---|
[8186] | 99 | } |
---|
| 100 | if(player == NULL) |
---|
| 101 | { |
---|
[8667] | 102 | COUT(4) << "Docking::execute Can't retrieve PlayerInfo from Trigger. (" << trigger->getIdentifier()->getName() << ")" << std::endl; |
---|
[8186] | 103 | return false; |
---|
| 104 | } |
---|
| 105 | |
---|
[8434] | 106 | if(bTriggered) |
---|
| 107 | { |
---|
[8382] | 108 | // Add player to this Docks candidates |
---|
[8700] | 109 | candidates_.insert(player); |
---|
[8382] | 110 | |
---|
[8434] | 111 | // Show docking dialog |
---|
| 112 | GUIManager::showGUI("DockingDialog"); |
---|
| 113 | } |
---|
| 114 | else |
---|
| 115 | { |
---|
[8382] | 116 | // Remove player from candidates list |
---|
[8700] | 117 | candidates_.erase(player); |
---|
[8185] | 118 | } |
---|
| 119 | |
---|
| 120 | return true; |
---|
| 121 | } |
---|
| 122 | |
---|
[8434] | 123 | void Dock::cmdDock() |
---|
| 124 | { |
---|
[8382] | 125 | PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer(); |
---|
[8434] | 126 | for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it) |
---|
| 127 | { |
---|
[8382] | 128 | if(it->dock(player)) |
---|
| 129 | break; |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | |
---|
[8434] | 133 | void Dock::cmdUndock() |
---|
| 134 | { |
---|
[8382] | 135 | PlayerInfo* player = HumanController::getLocalControllerSingleton()->getPlayer(); |
---|
[8434] | 136 | for(ObjectList<Dock>::iterator it = ObjectList<Dock>::begin(); it != ObjectList<Dock>::end(); ++it) |
---|
| 137 | { |
---|
[8382] | 138 | if(it->undock(player)) |
---|
| 139 | break; |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | |
---|
[8434] | 143 | bool Dock::dock(PlayerInfo* player) |
---|
| 144 | { |
---|
[8382] | 145 | // Check if player is a candidate |
---|
[8700] | 146 | if(candidates_.find(player) == candidates_.end()) |
---|
[8434] | 147 | { |
---|
[8560] | 148 | COUT(2) << "Dock::dock Player is not a candidate!" << std::endl; |
---|
[8382] | 149 | return false; |
---|
| 150 | } |
---|
| 151 | |
---|
[8700] | 152 | candidates_.erase(player); |
---|
| 153 | docked_.insert(player); |
---|
[8493] | 154 | |
---|
[8700] | 155 | if (animations_.empty()) |
---|
[8493] | 156 | return dockingAnimationFinished(player); |
---|
| 157 | else |
---|
[8700] | 158 | DockingAnimation::invokeAnimation(true, player, animations_); |
---|
[8493] | 159 | |
---|
[8382] | 160 | return true; |
---|
| 161 | } |
---|
| 162 | |
---|
[8493] | 163 | bool Dock::dockingAnimationFinished(PlayerInfo* player) |
---|
| 164 | { |
---|
[8700] | 165 | if(docked_.find(player) == docked_.end()) |
---|
[8493] | 166 | { |
---|
[8560] | 167 | COUT(2) << "Dock::dockingAnimationFinished Player is not currently docked." << std::endl; |
---|
[8493] | 168 | return false; |
---|
| 169 | } |
---|
| 170 | |
---|
[8700] | 171 | DockingEffect::invokeEffect(true, player, effects_); |
---|
[8493] | 172 | return true; |
---|
| 173 | } |
---|
| 174 | |
---|
[8434] | 175 | bool Dock::undock(PlayerInfo* player) |
---|
| 176 | { |
---|
[8382] | 177 | // Check if player is docked to this Dock |
---|
[8700] | 178 | if(docked_.find(player) == docked_.end()) |
---|
[8434] | 179 | { |
---|
[8560] | 180 | COUT(2) << "Dock::undock Player is not docked to this Dock." << std::endl; |
---|
[8382] | 181 | return false; |
---|
| 182 | } |
---|
| 183 | |
---|
[8700] | 184 | docked_.erase(player); |
---|
| 185 | candidates_.insert(player); |
---|
[8493] | 186 | |
---|
[8700] | 187 | DockingEffect::invokeEffect(false, player, effects_); |
---|
[8493] | 188 | |
---|
[8700] | 189 | if (animations_.empty()) |
---|
[8493] | 190 | return undockingAnimationFinished(player); |
---|
| 191 | else |
---|
[8700] | 192 | DockingAnimation::invokeAnimation(false, player, animations_); |
---|
[8493] | 193 | |
---|
[8382] | 194 | return true; |
---|
| 195 | } |
---|
| 196 | |
---|
[8493] | 197 | bool Dock::undockingAnimationFinished(PlayerInfo* player) { |
---|
[8560] | 198 | COUT(4) << "Dock::undockingAnimationFinished executed" << std::endl; |
---|
[8493] | 199 | return true; |
---|
| 200 | } |
---|
[8382] | 201 | |
---|
[8434] | 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 | { |
---|
[8700] | 208 | if(it->candidates_.find(player) != it->candidates_.end()) |
---|
[8434] | 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 | { |
---|
[8700] | 219 | if(it->candidates_.find(player) != it->candidates_.end()) |
---|
[8434] | 220 | { |
---|
| 221 | if(index == 0) |
---|
| 222 | return *it; |
---|
| 223 | index--; |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | return NULL; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | bool Dock::addEffect(DockingEffect* effect) |
---|
| 230 | { |
---|
[8185] | 231 | assert(effect); |
---|
[8700] | 232 | effects_.push_back(effect); |
---|
[8185] | 233 | return true; |
---|
| 234 | } |
---|
| 235 | |
---|
[8493] | 236 | const DockingEffect* Dock::getEffect(unsigned int i) const |
---|
[8434] | 237 | { |
---|
[8700] | 238 | for (std::list<DockingEffect*>::const_iterator effect = this->effects_.begin(); effect != this->effects_.end(); ++effect) |
---|
[8434] | 239 | { |
---|
[8151] | 240 | if(i == 0) |
---|
| 241 | return *effect; |
---|
| 242 | i--; |
---|
| 243 | } |
---|
| 244 | return NULL; |
---|
[8185] | 245 | } |
---|
[8493] | 246 | |
---|
| 247 | bool Dock::addAnimation(DockingAnimation* animation) |
---|
| 248 | { |
---|
| 249 | assert(animation); |
---|
| 250 | animation->setParent(this); |
---|
[8700] | 251 | animations_.push_back(animation); |
---|
[8493] | 252 | return true; |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | const DockingAnimation* Dock::getAnimation(unsigned int i) const |
---|
| 256 | { |
---|
[8700] | 257 | for (std::list<DockingAnimation*>::const_iterator animation = this->animations_.begin(); animation != this->animations_.end(); ++animation) |
---|
[8493] | 258 | { |
---|
| 259 | if(i == 0) |
---|
| 260 | return *animation; |
---|
| 261 | i--; |
---|
| 262 | } |
---|
| 263 | return NULL; |
---|
| 264 | } |
---|
[8137] | 265 | } |
---|