[9218] | 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 | * Nino Weingart |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | @file FlagPickup.cc |
---|
| 31 | @brief Implementation of the FlagPickup class. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include "FlagPickup.h" |
---|
| 35 | |
---|
| 36 | #include <sstream> |
---|
| 37 | #include "core/CoreIncludes.h" |
---|
| 38 | #include "core/XMLPort.h" |
---|
| 39 | |
---|
| 40 | #include "pickup/PickupIdentifier.h" |
---|
| 41 | #include "worldentities/pawns/Pawn.h" |
---|
| 42 | |
---|
| 43 | namespace orxonox |
---|
| 44 | { |
---|
| 45 | |
---|
| 46 | /*static*/ const std::string FlagPickup::flagTypeBlue_s = "blue"; |
---|
| 47 | /*static*/ const std::string FlagPickup::flagTypeRed_s = "red"; |
---|
| 48 | /*static*/ const std::string FlagPickup::flagTypeNeutral_s = "neutral"; |
---|
| 49 | |
---|
| 50 | CreateFactory(FlagPickup); |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | @brief |
---|
| 54 | Constructor. Registers the object and initializes the member variables. |
---|
| 55 | */ |
---|
| 56 | FlagPickup::FlagPickup(BaseObject* creator) : Pickup(creator) |
---|
| 57 | { |
---|
| 58 | RegisterObject(FlagPickup); |
---|
| 59 | |
---|
| 60 | this->initialize(); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | @brief |
---|
| 65 | Destructor. |
---|
| 66 | */ |
---|
| 67 | FlagPickup::~FlagPickup() |
---|
| 68 | { |
---|
| 69 | |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | @brief |
---|
| 74 | Initializes the member variables. |
---|
| 75 | */ |
---|
| 76 | void FlagPickup::initialize(void) |
---|
| 77 | { |
---|
| 78 | this->flagType_ = pickupFlagType::0; |
---|
| 79 | |
---|
| 80 | this->addTarget(ClassIdentifier<Pawn>::getIdentifier()); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | @brief |
---|
| 85 | Initializes the PickupIdentifier of this pickup. |
---|
| 86 | */ |
---|
| 87 | void FlagPickup::initializeIdentifier(void) |
---|
| 88 | { |
---|
| 89 | std::string val1 = this->getFlagType(); |
---|
| 90 | std::string type1 = "flagType"; |
---|
| 91 | this->pickupIdentifier_->addParameter(type1, val1); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | /** |
---|
| 95 | @brief |
---|
| 96 | Method for creating a FlagPickup object through XML. |
---|
| 97 | */ |
---|
| 98 | void FlagPickup::XMLPort(Element& xmlelement, orxonox::XMLPort::Mode mode) |
---|
| 99 | { |
---|
| 100 | SUPER(FlagPickup, XMLPort, xmlelement, mode); |
---|
| 101 | |
---|
| 102 | XMLPortParam(FlagPickup, "flagType", setFlagType, getFlagType, xmlelement, mode); |
---|
| 103 | //XMLPortParam(FlagPickup, "teamNumber", setTeamNumber, getTeamNumber, xmlelement, mode); |
---|
| 104 | |
---|
| 105 | this->initializeIdentifier(); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | /** |
---|
| 110 | @brief |
---|
| 111 | Get the flag type of this pickup. |
---|
| 112 | @return |
---|
| 113 | Returns the falg type as a string. |
---|
| 114 | */ |
---|
| 115 | const std::string& FlagPickup::getFlagType(void) const |
---|
| 116 | { |
---|
| 117 | switch(this->getFlagTypeDirect()) |
---|
| 118 | { |
---|
| 119 | case pickupFlagType::blue: |
---|
| 120 | return FlagPickup::flagTypeBlue_s; |
---|
| 121 | case pickupFlagType::red: |
---|
| 122 | return FlagPickup::flagTypeRed_s; |
---|
| 123 | case pickupFlagType::neutral: |
---|
| 124 | return FlagPickup::flagTypeNeutral_s; |
---|
| 125 | default: |
---|
| 126 | orxout(internal_error, context::pickups) << "Invalid flagType in FlagPickup." << endl; |
---|
| 127 | return BLANKSTRING; |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /** |
---|
| 132 | @brief |
---|
| 133 | Set the type of the HealthPickup. |
---|
| 134 | @param type |
---|
| 135 | The type as a string. |
---|
| 136 | */ |
---|
| 137 | void FlagPickup::setFlagType(std::string type) |
---|
| 138 | { |
---|
| 139 | if(type == FlagPickup::flagTypeRed_s) |
---|
| 140 | this->setFlagTypeDirect(pickupFlagType::2); |
---|
| 141 | else if(type == FlagPickup::flagTypeBlue_s) |
---|
| 142 | this->setFlagTypeDirect(pickupFlagType::1); |
---|
| 143 | else if(type == FlagPickup::flagTypeNeutral_s) |
---|
| 144 | this->setFlagTypeDirect(pickupFlagType::0); |
---|
| 145 | else |
---|
| 146 | orxout(internal_error, context::pickups) << "Invalid flagType '" << type << "' in FlagPickup." << endl; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | /** |
---|
| 150 | @brief |
---|
| 151 | Is called when the pickup has transited from used to unused or the other way around. |
---|
| 152 | */ |
---|
| 153 | void FlagPickup::changedUsed(void) |
---|
| 154 | { |
---|
| 155 | SUPER(FlagPickup, changedUsed); |
---|
| 156 | |
---|
| 157 | Pawn* pawn = this->carrierToPawnHelper(); |
---|
| 158 | |
---|
| 159 | if(pawn == NULL) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed. |
---|
| 160 | this->Pickupable::destroy(); |
---|
| 161 | |
---|
| 162 | // If the pickup has transited to used. |
---|
| 163 | if(this->isUsed()) |
---|
| 164 | { |
---|
| 165 | int team = getTeam(pawn->getPlayer); |
---|
| 166 | |
---|
| 167 | if(this->flagType_ == team){ |
---|
| 168 | if(pawn->hasFlag_){ |
---|
| 169 | teamScore_ = TeamScore_ + 1000; |
---|
| 170 | pawn->hasFlag_ = false; |
---|
| 171 | } |
---|
| 172 | this->Pickupable::destroy(); |
---|
| 173 | }else{ |
---|
| 174 | pawn->hasFlag_ = true; |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | /** |
---|
| 182 | @brief |
---|
| 183 | Helper to transform the PickupCarrier to a Pawn, and throw an error message if the conversion fails. |
---|
| 184 | @return |
---|
| 185 | A pointer to the Pawn, or NULL if the conversion failed. |
---|
| 186 | */ |
---|
| 187 | Pawn* FlagPickup::carrierToPawnHelper(void) |
---|
| 188 | { |
---|
| 189 | PickupCarrier* carrier = this->getCarrier(); |
---|
| 190 | Pawn* pawn = dynamic_cast<Pawn*>(carrier); |
---|
| 191 | |
---|
| 192 | if(pawn == NULL) |
---|
| 193 | orxout(internal_error, context::pickups) << "Invalid PickupCarrier in HealthPickup." << endl; |
---|
| 194 | |
---|
| 195 | return pawn; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | int FlagPickup::getTeam(PlayerInfo* player) |
---|
| 199 | { |
---|
| 200 | std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player); |
---|
| 201 | if (it_player != this->teamnumbers_.end()) |
---|
| 202 | return it_player->second; |
---|
| 203 | else |
---|
| 204 | return -1; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | void FlagPickup::tick(float dt) |
---|
| 210 | { |
---|
| 211 | SUPER(FlagPickup, tick, dt); |
---|
| 212 | |
---|
| 213 | Pawn* pawn = this->carrierToPawnHelper(); |
---|
| 214 | |
---|
| 215 | if(pawn->isAlive() && pawn->hasFlag_){ |
---|
| 216 | this->Pickupable::destroy(); |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | } |
---|