| [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 |
|---|
| 31 | @brief Docking system main class |
|---|
| 32 | */ |
|---|
| 33 | |
|---|
| 34 | #include "Dock.h" |
|---|
| [8382] | 35 | |
|---|
| [8192] | 36 | #include "infos/HumanPlayer.h" |
|---|
| [8186] | 37 | #include "worldentities/pawns/Pawn.h" |
|---|
| 38 | #include "interfaces/PlayerTrigger.h" |
|---|
| [8382] | 39 | #include "controllers/HumanController.h" |
|---|
| 40 | #include "core/command/ConsoleCommand.h" |
|---|
| [8137] | 41 | |
|---|
| 42 | |
|---|
| [8382] | 43 | |
|---|
| [8137] | 44 | namespace orxonox |
|---|
| 45 | { |
|---|
| [8185] | 46 | CreateFactory(Dock); |
|---|
| [8137] | 47 | |
|---|
| [8382] | 48 | SetConsoleCommand("Dock", "dock", &Dock::cmdDock).addShortcut().setAsInputCommand(); |
|---|
| 49 | SetConsoleCommand("Dock", "undock", &Dock::cmdUndock).addShortcut().setAsInputCommand(); |
|---|
| 50 | |
|---|
| [8137] | 51 | Dock::Dock(BaseObject* creator) : StaticEntity(creator) |
|---|
| 52 | { |
|---|
| 53 | RegisterObject(Dock); |
|---|
| [8185] | 54 | COUT(0) << "Registering dock..." << std::endl; |
|---|
| [8137] | 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 | |
|---|
| [8151] | 66 | XMLPortObject(Dock, DockingEffect, "effects", addEffect, getEffect, xmlelement, mode); |
|---|
| [8137] | 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) |
|---|
| [8185] | 81 | { |
|---|
| [8186] | 82 | PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger); |
|---|
| 83 | Pawn* pawn = NULL; |
|---|
| 84 | |
|---|
| [8188] | 85 | // Check whether it is a player trigger and extract pawn from it |
|---|
| [8186] | 86 | if(pTrigger != NULL) |
|---|
| 87 | { |
|---|
| [8188] | 88 | if(!pTrigger->isForPlayer()) { // The PlayerTrigger is not exclusively for Pawns which means we cannot extract one. |
|---|
| [8382] | 89 | COUT(2) << "Docking:execute PlayerTrigger was not triggered by a player.." << std::endl; |
|---|
| [8186] | 90 | return false; |
|---|
| [8188] | 91 | } |
|---|
| 92 | pawn = pTrigger->getTriggeringPlayer(); |
|---|
| 93 | } else { |
|---|
| [8382] | 94 | COUT(2) << "Docking::execute Not a player trigger, can't extract pawn from it.." << std::endl; |
|---|
| [8188] | 95 | return false; |
|---|
| [8186] | 96 | } |
|---|
| 97 | if(pawn == NULL) |
|---|
| 98 | { |
|---|
| [8382] | 99 | COUT(2) << "Docking::execute Can't retrieve Pawn from Trigger. (" << trigger->getIdentifier()->getName() << ")" << std::endl; |
|---|
| [8186] | 100 | return false; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | // Extract the PlayerInfo from the Pawn. |
|---|
| 104 | PlayerInfo* player = pawn->getPlayer(); |
|---|
| 105 | if(player == NULL) |
|---|
| 106 | { |
|---|
| [8382] | 107 | COUT(2) << "The PlayerInfo* is NULL." << std::endl; |
|---|
| [8186] | 108 | return false; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| [8257] | 111 | COUT(0) << "Dock triggered by player: " << player->getName() << ".." << std::endl; |
|---|
| [8188] | 112 | |
|---|
| [8185] | 113 | if(bTriggered) { |
|---|
| [8382] | 114 | // Add player to this Docks candidates |
|---|
| 115 | candidates.insert(player); |
|---|
| 116 | |
|---|
| 117 | //DockingEffect::invokeEffect(docking::DOCKING, player, effects_); |
|---|
| [8185] | 118 | } else { |
|---|
| [8382] | 119 | // Remove player from candidates list |
|---|
| 120 | candidates.erase(player); |
|---|
| 121 | |
|---|
| 122 | //DockingEffect::invokeEffect(docking::RELEASE, player, effects_); |
|---|
| [8185] | 123 | } |
|---|
| 124 | |
|---|
| 125 | return true; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| [8382] | 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 | |
|---|
| [8185] | 175 | bool Dock::addEffect(DockingEffect* effect) { |
|---|
| 176 | assert(effect); |
|---|
| [8382] | 177 | effects.push_back(effect); |
|---|
| [8185] | 178 | return true; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | const DockingEffect* Dock::getEffect(unsigned int index) const { |
|---|
| 182 | int i = index; |
|---|
| [8382] | 183 | for (std::list<DockingEffect*>::const_iterator effect = this->effects.begin(); effect != this->effects.end(); ++effect) { |
|---|
| [8151] | 184 | if(i == 0) |
|---|
| 185 | return *effect; |
|---|
| 186 | i--; |
|---|
| 187 | } |
|---|
| 188 | return NULL; |
|---|
| [8185] | 189 | } |
|---|
| [8137] | 190 | } |
|---|