Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/objects/pickup/PickupSpawner.cc @ 3180

Last change on this file since 3180 was 3180, checked in by rgrieder, 15 years ago

Cleanup in pickup and quest (almost only cleaned the header file sections).

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