Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/pickup/PickupSpawner.cc @ 3166

Last change on this file since 3166 was 3079, checked in by landauf, 16 years ago

Added forward declarations to OrxonoxPrereqs.h.

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