[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" |
---|
| 35 | #include "BaseItem.h" |
---|
| 36 | |
---|
| 37 | #include "core/CoreIncludes.h" |
---|
| 38 | #include "core/XMLPort.h" |
---|
| 39 | #include "core/Template.h" |
---|
| 40 | |
---|
| 41 | #include "objects/worldentities/pawns/Pawn.h" |
---|
| 42 | #include "objects/worldentities/triggers/DistanceTrigger.h" |
---|
| 43 | |
---|
| 44 | namespace orxonox |
---|
| 45 | { |
---|
[3046] | 46 | const float PickupSpawner::bounceSpeed_s = 6.0f; |
---|
| 47 | const float PickupSpawner::rotationSpeed_s = 1.0f; |
---|
| 48 | const float PickupSpawner::bounceDistance_s = 4.0f; |
---|
| 49 | |
---|
[2917] | 50 | CreateFactory(PickupSpawner); |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | @brief Constructor. Registers the PickupSpawner. |
---|
| 54 | @param creator Pointer to the object which created this item. |
---|
| 55 | */ |
---|
| 56 | PickupSpawner::PickupSpawner(BaseObject* creator) : StaticEntity(creator) |
---|
| 57 | { |
---|
| 58 | RegisterObject(PickupSpawner); |
---|
| 59 | |
---|
| 60 | this->itemTemplate_ = 0; |
---|
| 61 | this->triggerDistance_ = 20; |
---|
| 62 | this->respawnTime_ = 0.0f; |
---|
[3046] | 63 | this->tickSum_ = 0.0f; |
---|
[2917] | 64 | } |
---|
| 65 | //! Deconstructor. |
---|
| 66 | PickupSpawner::~PickupSpawner() |
---|
| 67 | { |
---|
| 68 | } |
---|
| 69 | /** |
---|
| 70 | @brief Method for creating a PickupSpawner through XML. |
---|
| 71 | @param xmlelement XML element which contains the PickupSpawner. |
---|
| 72 | @param mode XMLPort mode. |
---|
| 73 | */ |
---|
| 74 | void PickupSpawner::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 75 | { |
---|
| 76 | SUPER(PickupSpawner, XMLPort, xmlelement, mode); |
---|
| 77 | |
---|
| 78 | XMLPortParam(PickupSpawner, "item", setItemTemplateName, getItemTemplateName, xmlelement, mode); |
---|
| 79 | XMLPortParam(PickupSpawner, "triggerDistance", setTriggerDistance, getTriggerDistance, xmlelement, mode); |
---|
| 80 | XMLPortParam(PickupSpawner, "respawnTime", setRespawnTime, getRespawnTime, xmlelement, mode); |
---|
| 81 | } |
---|
| 82 | /** |
---|
| 83 | @brief Invoked when the activity has changed. Sets visibility of attached objects. |
---|
| 84 | */ |
---|
| 85 | void PickupSpawner::changedActivity() |
---|
| 86 | { |
---|
| 87 | SUPER(PickupSpawner, changedActivity); |
---|
| 88 | |
---|
| 89 | for (std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin(); it != this->getAttachedObjects().end(); it++) |
---|
| 90 | (*it)->setVisible(this->isActive()); |
---|
| 91 | } |
---|
| 92 | /** |
---|
| 93 | @brief Set the template name of the item to spawn, also loads the template. |
---|
| 94 | @param name Name of the new template. |
---|
| 95 | */ |
---|
| 96 | void PickupSpawner::setItemTemplateName(const std::string& name) |
---|
| 97 | { |
---|
| 98 | this->itemTemplateName_ = name; |
---|
| 99 | this->itemTemplate_ = Template::getTemplate(name); |
---|
| 100 | } |
---|
| 101 | /** |
---|
| 102 | @brief Tick, checks if any Pawn is close enough to trigger. |
---|
| 103 | @param dt Time since last tick. |
---|
| 104 | */ |
---|
| 105 | void PickupSpawner::tick(float dt) |
---|
| 106 | { |
---|
| 107 | if (this->isActive()) |
---|
| 108 | { |
---|
| 109 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); it++) |
---|
| 110 | { |
---|
| 111 | Vector3 distance = it->getWorldPosition() - this->getWorldPosition(); |
---|
| 112 | if (distance.length() < this->triggerDistance_) |
---|
| 113 | this->trigger(*it); |
---|
| 114 | } |
---|
[3046] | 115 | this->yaw(Radian(rotationSpeed_s*dt)); |
---|
| 116 | this->tickSum_ += bounceSpeed_s*dt; |
---|
| 117 | this->translate(Vector3(0,bounceDistance_s*dt*sin(this->tickSum_),0)); |
---|
| 118 | if (this->tickSum_ > 2*Ogre::Math::PI) |
---|
| 119 | this->tickSum_ -= 2*Ogre::Math::PI; |
---|
[2917] | 120 | } |
---|
| 121 | } |
---|
| 122 | /** |
---|
| 123 | @brief |
---|
| 124 | Trigger the PickupSpawner. |
---|
| 125 | |
---|
| 126 | Adds the pickup to the Pawn that triggered, |
---|
| 127 | sets the timer to re-activate and deactives the PickupSpawner. |
---|
| 128 | |
---|
| 129 | @param pawn Pawn which triggered the PickupSpawner. |
---|
| 130 | */ |
---|
| 131 | void PickupSpawner::trigger(Pawn* pawn) |
---|
| 132 | { |
---|
| 133 | if (this->isActive() && this->itemTemplate_ && this->itemTemplate_->getBaseclassIdentifier()) |
---|
| 134 | { |
---|
| 135 | BaseObject* newObject = this->itemTemplate_->getBaseclassIdentifier()->fabricate(this); |
---|
| 136 | BaseItem* asItem = dynamic_cast<BaseItem*>(newObject); |
---|
| 137 | if (asItem) |
---|
| 138 | { |
---|
| 139 | asItem->setPickupIdentifier(this->itemTemplateName_); |
---|
| 140 | asItem->addTemplate(this->itemTemplate_); |
---|
| 141 | |
---|
| 142 | if (asItem->pickedUp(pawn)) |
---|
| 143 | { |
---|
| 144 | COUT(3) << this->itemTemplateName_ << " got picked up." << std::endl; |
---|
| 145 | |
---|
| 146 | if (this->respawnTime_ > 0.0f) |
---|
| 147 | { |
---|
| 148 | ExecutorMember<PickupSpawner>* executor = createExecutor(createFunctor(&PickupSpawner::respawnTimerCallback)); |
---|
| 149 | this->respawnTimer_.setTimer(this->respawnTime_, false, this, executor); |
---|
| 150 | |
---|
| 151 | this->setActive(false); |
---|
| 152 | this->fireEvent(); |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | else |
---|
| 156 | delete newObject; |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | /** |
---|
| 161 | @brief Invoked by the timer, re-activates the PickupSpawner. |
---|
| 162 | */ |
---|
| 163 | void PickupSpawner::respawnTimerCallback() |
---|
| 164 | { |
---|
| 165 | COUT(3) << "PickupSpawner reactivated." << std::endl; |
---|
| 166 | |
---|
| 167 | this->setActive(true); |
---|
| 168 | } |
---|
| 169 | } |
---|