| 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 | #include "infos/HumanPlayer.h" |
|---|
| 36 | #include "worldentities/pawns/Pawn.h" |
|---|
| 37 | #include "interfaces/PlayerTrigger.h" |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | namespace orxonox |
|---|
| 41 | { |
|---|
| 42 | CreateFactory(Dock); |
|---|
| 43 | |
|---|
| 44 | Dock::Dock(BaseObject* creator) : StaticEntity(creator) |
|---|
| 45 | { |
|---|
| 46 | RegisterObject(Dock); |
|---|
| 47 | COUT(0) << "Registering dock..." << std::endl; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | Dock::~Dock() |
|---|
| 51 | { |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | void Dock::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
|---|
| 56 | { |
|---|
| 57 | SUPER(Dock, XMLPort, xmlelement, mode); |
|---|
| 58 | |
|---|
| 59 | XMLPortObject(Dock, DockingEffect, "effects", addEffect, getEffect, xmlelement, mode); |
|---|
| 60 | XMLPortEventSink(Dock, BaseObject, "execute", execute, xmlelement, mode); |
|---|
| 61 | |
|---|
| 62 | COUT(0) << "Dock created.." << std::endl; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | void Dock::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) |
|---|
| 66 | { |
|---|
| 67 | SUPER(Dock, XMLEventPort, xmlelement, mode); |
|---|
| 68 | |
|---|
| 69 | XMLPortEventSink(Dock, BaseObject, "execute", execute, xmlelement, mode); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | bool Dock::execute(bool bTriggered, BaseObject* trigger) |
|---|
| 74 | { |
|---|
| 75 | //TODO: Handle DistanceMultiTrigger |
|---|
| 76 | |
|---|
| 77 | PlayerTrigger* pTrigger = orxonox_cast<PlayerTrigger*>(trigger); |
|---|
| 78 | Pawn* pawn = NULL; |
|---|
| 79 | |
|---|
| 80 | // Check whether it is a player trigger and extract pawn from it |
|---|
| 81 | if(pTrigger != NULL) |
|---|
| 82 | { |
|---|
| 83 | if(!pTrigger->isForPlayer()) { // The PlayerTrigger is not exclusively for Pawns which means we cannot extract one. |
|---|
| 84 | COUT(0) << "Docking:execute PlayerTrigger was not triggered by a player.." << std::endl; |
|---|
| 85 | return false; |
|---|
| 86 | } |
|---|
| 87 | pawn = pTrigger->getTriggeringPlayer(); |
|---|
| 88 | } else { |
|---|
| 89 | COUT(0) << "Docking::execute Not a player trigger, can't extract pawn from it.." << std::endl; |
|---|
| 90 | return false; |
|---|
| 91 | } |
|---|
| 92 | if(pawn == NULL) |
|---|
| 93 | { |
|---|
| 94 | COUT(0) << "Docking::execute Can't retrieve Pawn from Trigger. (" << trigger->getIdentifier()->getName() << ")" << std::endl; |
|---|
| 95 | return false; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | // Extract the PlayerInfo from the Pawn. |
|---|
| 99 | PlayerInfo* player = pawn->getPlayer(); |
|---|
| 100 | if(player == NULL) |
|---|
| 101 | { |
|---|
| 102 | COUT(0) << "The PlayerInfo* is NULL." << std::endl; |
|---|
| 103 | return false; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | // Try to get HumanPlayer |
|---|
| 107 | if(!player->isHumanPlayer()) { |
|---|
| 108 | COUT(0) << "Docking::execute Not triggered by a human." << std::endl; |
|---|
| 109 | return false; |
|---|
| 110 | } |
|---|
| 111 | HumanPlayer* human = orxonox_cast<HumanPlayer*>(player); |
|---|
| 112 | if(human == NULL) { |
|---|
| 113 | COUT(0) << "Docking::execute Player was not as human as expected.." << std::endl; |
|---|
| 114 | } |
|---|
| 115 | COUT(0) << "Dock triggered by player: " << human->getName() << ".." << std::endl; |
|---|
| 116 | |
|---|
| 117 | //TODO: This is waaay too oversimplified |
|---|
| 118 | if(bTriggered) { |
|---|
| 119 | DockingEffect::invokeEffect(docking::DOCKING, player, effects_); |
|---|
| 120 | //DockingEffect::invokeEffect(docking::ATTACH, player, effects_); |
|---|
| 121 | } else { |
|---|
| 122 | DockingEffect::invokeEffect(docking::RELEASE, player, effects_); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | return true; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | bool Dock::addEffect(DockingEffect* effect) { |
|---|
| 130 | assert(effect); |
|---|
| 131 | effects_.push_back(effect); |
|---|
| 132 | return true; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | const DockingEffect* Dock::getEffect(unsigned int index) const { |
|---|
| 136 | int i = index; |
|---|
| 137 | for (std::list<DockingEffect*>::const_iterator effect = this->effects_.begin(); effect != this->effects_.end(); ++effect) |
|---|
| 138 | { |
|---|
| 139 | if(i == 0) |
|---|
| 140 | return *effect; |
|---|
| 141 | |
|---|
| 142 | i--; |
|---|
| 143 | } |
|---|
| 144 | return NULL; |
|---|
| 145 | } |
|---|
| 146 | } |
|---|