[2917] | 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 | * Daniel 'Huty' Haggenmueller |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | @file |
---|
| 31 | @brief Implementation of PickupSpawner. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include "PickupSpawner.h" |
---|
[3196] | 35 | |
---|
[2917] | 36 | #include "BaseItem.h" |
---|
| 37 | |
---|
| 38 | #include "core/CoreIncludes.h" |
---|
[3370] | 39 | #include "core/GUIManager.h" // HACK; see below |
---|
[3196] | 40 | #include "core/Template.h" |
---|
[2917] | 41 | #include "core/XMLPort.h" |
---|
[5735] | 42 | #include "worldentities/pawns/Pawn.h" |
---|
[3196] | 43 | #include "PickupInventory.h" // HACK; Only for hack, remove later |
---|
[2917] | 44 | |
---|
[3196] | 45 | |
---|
[2917] | 46 | namespace orxonox |
---|
| 47 | { |
---|
[3046] | 48 | const float PickupSpawner::bounceSpeed_s = 6.0f; |
---|
| 49 | const float PickupSpawner::rotationSpeed_s = 1.0f; |
---|
| 50 | const float PickupSpawner::bounceDistance_s = 4.0f; |
---|
| 51 | |
---|
[2917] | 52 | CreateFactory(PickupSpawner); |
---|
| 53 | |
---|
| 54 | /** |
---|
[5902] | 55 | @brief |
---|
| 56 | Constructor. Registers the PickupSpawner. |
---|
| 57 | @param creator |
---|
| 58 | Pointer to the object which created this item. |
---|
[2917] | 59 | */ |
---|
| 60 | PickupSpawner::PickupSpawner(BaseObject* creator) : StaticEntity(creator) |
---|
| 61 | { |
---|
| 62 | RegisterObject(PickupSpawner); |
---|
| 63 | |
---|
| 64 | this->itemTemplate_ = 0; |
---|
| 65 | this->triggerDistance_ = 20; |
---|
| 66 | this->respawnTime_ = 0.0f; |
---|
[3046] | 67 | this->tickSum_ = 0.0f; |
---|
[2917] | 68 | } |
---|
[5902] | 69 | |
---|
| 70 | /** |
---|
| 71 | @brief |
---|
| 72 | Destructor. |
---|
| 73 | */ |
---|
[2917] | 74 | PickupSpawner::~PickupSpawner() |
---|
| 75 | { |
---|
[5902] | 76 | |
---|
[2917] | 77 | } |
---|
[5902] | 78 | |
---|
[2917] | 79 | /** |
---|
[5902] | 80 | @brief |
---|
| 81 | Method for creating a PickupSpawner through XML. |
---|
| 82 | @param xmlelement |
---|
| 83 | XML element which contains the PickupSpawner. |
---|
| 84 | @param mode |
---|
| 85 | XMLPort mode. |
---|
[2917] | 86 | */ |
---|
| 87 | void PickupSpawner::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 88 | { |
---|
| 89 | SUPER(PickupSpawner, XMLPort, xmlelement, mode); |
---|
| 90 | |
---|
| 91 | XMLPortParam(PickupSpawner, "item", setItemTemplateName, getItemTemplateName, xmlelement, mode); |
---|
| 92 | XMLPortParam(PickupSpawner, "triggerDistance", setTriggerDistance, getTriggerDistance, xmlelement, mode); |
---|
| 93 | XMLPortParam(PickupSpawner, "respawnTime", setRespawnTime, getRespawnTime, xmlelement, mode); |
---|
[3063] | 94 | |
---|
[5902] | 95 | //TODO: Kill hack. |
---|
[3063] | 96 | // HACKs |
---|
| 97 | // Load the GUI image as soon as the PickupSpawner gets loaded |
---|
| 98 | // = less delays while running |
---|
| 99 | BaseObject* newObject = this->itemTemplate_->getBaseclassIdentifier()->fabricate(this); |
---|
[3325] | 100 | BaseItem* asItem = orxonox_cast<BaseItem*>(newObject); |
---|
[3063] | 101 | if (asItem) |
---|
| 102 | { |
---|
| 103 | asItem->addTemplate(this->itemTemplate_); |
---|
| 104 | PickupInventory::getImageForItem(asItem); |
---|
| 105 | delete newObject; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | // & load the GUI itself too, along with some empty windows |
---|
| 109 | // = even less delays |
---|
[3196] | 110 | GUIManager::getInstance().showGUI("PickupInventory"); |
---|
| 111 | GUIManager::getInstance().executeCode("hideGUI(\"PickupInventory\")"); |
---|
[3063] | 112 | PickupInventory::getSingleton(); |
---|
[2917] | 113 | } |
---|
[5902] | 114 | |
---|
[2917] | 115 | /** |
---|
[5902] | 116 | @brief |
---|
| 117 | Invoked when the activity has changed. Sets visibility of attached objects. |
---|
[2917] | 118 | */ |
---|
| 119 | void PickupSpawner::changedActivity() |
---|
| 120 | { |
---|
| 121 | SUPER(PickupSpawner, changedActivity); |
---|
| 122 | |
---|
| 123 | for (std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin(); it != this->getAttachedObjects().end(); it++) |
---|
| 124 | (*it)->setVisible(this->isActive()); |
---|
| 125 | } |
---|
[5902] | 126 | |
---|
[2917] | 127 | /** |
---|
[5902] | 128 | @brief |
---|
| 129 | Set the template name of the item to spawn, also loads the template. |
---|
| 130 | @param name |
---|
| 131 | Name of the new template. |
---|
[2917] | 132 | */ |
---|
| 133 | void PickupSpawner::setItemTemplateName(const std::string& name) |
---|
| 134 | { |
---|
| 135 | this->itemTemplateName_ = name; |
---|
| 136 | this->itemTemplate_ = Template::getTemplate(name); |
---|
| 137 | } |
---|
[5902] | 138 | |
---|
[2917] | 139 | /** |
---|
[5902] | 140 | @brief |
---|
| 141 | Tick, checks if any Pawn is close enough to trigger. |
---|
| 142 | @param dt |
---|
| 143 | Time since last tick. |
---|
[2917] | 144 | */ |
---|
[5902] | 145 | //TODO: Replace this with a real DistanceTrigger. |
---|
[2917] | 146 | void PickupSpawner::tick(float dt) |
---|
| 147 | { |
---|
| 148 | if (this->isActive()) |
---|
| 149 | { |
---|
| 150 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); it++) |
---|
| 151 | { |
---|
| 152 | Vector3 distance = it->getWorldPosition() - this->getWorldPosition(); |
---|
| 153 | if (distance.length() < this->triggerDistance_) |
---|
| 154 | this->trigger(*it); |
---|
| 155 | } |
---|
[3046] | 156 | this->yaw(Radian(rotationSpeed_s*dt)); |
---|
| 157 | this->tickSum_ += bounceSpeed_s*dt; |
---|
| 158 | this->translate(Vector3(0,bounceDistance_s*dt*sin(this->tickSum_),0)); |
---|
| 159 | if (this->tickSum_ > 2*Ogre::Math::PI) |
---|
| 160 | this->tickSum_ -= 2*Ogre::Math::PI; |
---|
[2917] | 161 | } |
---|
| 162 | } |
---|
[5902] | 163 | |
---|
[2917] | 164 | /** |
---|
[5902] | 165 | @brief |
---|
| 166 | Trigger the PickupSpawner. |
---|
[2917] | 167 | |
---|
[5902] | 168 | Adds the pickup to the Pawn that triggered, |
---|
| 169 | sets the timer to re-activate and deactives the PickupSpawner. |
---|
[2917] | 170 | |
---|
[5902] | 171 | @param pawn |
---|
| 172 | Pawn which triggered the PickupSpawner. |
---|
[2917] | 173 | */ |
---|
| 174 | void PickupSpawner::trigger(Pawn* pawn) |
---|
| 175 | { |
---|
| 176 | if (this->isActive() && this->itemTemplate_ && this->itemTemplate_->getBaseclassIdentifier()) |
---|
| 177 | { |
---|
| 178 | BaseObject* newObject = this->itemTemplate_->getBaseclassIdentifier()->fabricate(this); |
---|
[3325] | 179 | BaseItem* asItem = orxonox_cast<BaseItem*>(newObject); |
---|
[2917] | 180 | if (asItem) |
---|
| 181 | { |
---|
| 182 | asItem->setPickupIdentifier(this->itemTemplateName_); |
---|
| 183 | asItem->addTemplate(this->itemTemplate_); |
---|
| 184 | |
---|
| 185 | if (asItem->pickedUp(pawn)) |
---|
| 186 | { |
---|
| 187 | COUT(3) << this->itemTemplateName_ << " got picked up." << std::endl; |
---|
| 188 | |
---|
| 189 | if (this->respawnTime_ > 0.0f) |
---|
| 190 | { |
---|
| 191 | ExecutorMember<PickupSpawner>* executor = createExecutor(createFunctor(&PickupSpawner::respawnTimerCallback)); |
---|
| 192 | this->respawnTimer_.setTimer(this->respawnTime_, false, this, executor); |
---|
| 193 | |
---|
| 194 | this->setActive(false); |
---|
| 195 | this->fireEvent(); |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | else |
---|
| 199 | delete newObject; |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | } |
---|
[5902] | 203 | |
---|
[2917] | 204 | /** |
---|
[5902] | 205 | @brief |
---|
| 206 | Invoked by the timer, re-activates the PickupSpawner. |
---|
[2917] | 207 | */ |
---|
| 208 | void PickupSpawner::respawnTimerCallback() |
---|
| 209 | { |
---|
| 210 | COUT(3) << "PickupSpawner reactivated." << std::endl; |
---|
| 211 | |
---|
| 212 | this->setActive(true); |
---|
| 213 | } |
---|
| 214 | } |
---|