Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickups/src/orxonox/objects/pickup/PickupSpawner.cc @ 2950

Last change on this file since 2950 was 2864, checked in by danielh, 16 years ago

first commit of pickup system

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