Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/pickup/PickupSpawner.cc @ 7331

Last change on this file since 7331 was 7163, checked in by dafrick, 14 years ago

Merged presentation3 branch into trunk.

  • Property svn:eol-style set to native
File size: 10.9 KB
RevLine 
[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:
[6405]25 *      Damian 'Mozork' Frick
[2917]26 *
27 */
28
29/**
[6540]30    @file PickupSpawner.cc
31    @brief Implementation of the PickupSpawner class.
[2917]32*/
33
34#include "PickupSpawner.h"
[3196]35
[2917]36#include "core/CoreIncludes.h"
[3196]37#include "core/Template.h"
[2917]38#include "core/XMLPort.h"
[5735]39#include "worldentities/pawns/Pawn.h"
[6466]40#include "PickupManager.h"
41#include "PickupRepresentation.h"
[2917]42
43namespace orxonox
44{
[5953]45
[2917]46    CreateFactory(PickupSpawner);
47
48    /**
[5947]49    @brief
[6421]50        Constructor. Creates a blank PickupSpawner.
[5947]51    @param creator
52        Pointer to the object which created this item.
[2917]53    */
[6540]54    PickupSpawner::PickupSpawner(BaseObject* creator) : StaticEntity(creator), pickup_(NULL)
[2917]55    {
[6540]56        RegisterObject(PickupSpawner);
[7163]57
[6514]58        this->initialize();
[5953]59    }
60
[6421]61    /**
62    @brief
63        Constructor, Creates a fully functional PickupSpawner.
64    @param creator
65        The creator of this PickupSpawner.
66    @param pickup
67        The Pickupable to be spawned by this PickupSpawner.
68    @param triggerDistance
69        The distance at which the PickupSpawner will trigger.
70    @param respawnTime
71        The minimum time between two spawns.
72    @param maySpawnedItems
73        The maximum number of items spawned by this PickupSpawner.
74    */
[6540]75    PickupSpawner::PickupSpawner(BaseObject* creator, Pickupable* pickup, float triggerDistance, float respawnTime, int maxSpawnedItems) : StaticEntity(creator), pickup_(NULL)
[5953]76    {
[6466]77        RegisterObject(PickupSpawner);
[7163]78
[5953]79        this->initialize();
[7163]80
[6405]81        this->pickup_ = pickup;
[5953]82
83        this->triggerDistance_ = triggerDistance;
84        this->respawnTime_ = respawnTime;
85        this->setMaxSpawnedItems(maxSpawnedItems);
[7163]86
[6475]87        if(this->pickup_ == NULL)
88        {
89            COUT(2) << "A PickupSpawner was created without a valid Pickupable. This won't work." << std::endl;
90            this->setActive(false);
91        }
92        else
93        {
94            PickupRepresentation* representation = PickupManager::getInstance().getRepresentation(this->pickup_->getPickupIdentifier());
95            this->attach(representation->getSpawnerRepresentation(this));
96        }
[5953]97    }
98
[6421]99    /**
100    @brief
101        Registers the object and sets some default values.
102    */
[5953]103    void PickupSpawner::initialize(void)
104    {
[6563]105        this->triggerDistance_ = 10;
[6405]106        this->respawnTime_ = 0;
[5953]107        this->maxSpawnedItems_ = INF;
108        this->spawnsRemaining_ = INF;
[7163]109        this->selfDestruct_ = false;
[2917]110    }
[5947]111
112    /**
113    @brief
114        Destructor.
115    */
[2917]116    PickupSpawner::~PickupSpawner()
117    {
[7163]118        if(this->selfDestruct_ && this->pickup_ != NULL)
[6478]119            this->pickup_->destroy();
[2917]120    }
[5947]121
[2917]122    /**
[5947]123    @brief
124        Method for creating a PickupSpawner through XML.
125    @param xmlelement
126        XML element which contains the PickupSpawner.
127    @param mode
128        XMLPort mode.
[2917]129    */
130    void PickupSpawner::XMLPort(Element& xmlelement, XMLPort::Mode mode)
131    {
132        SUPER(PickupSpawner, XMLPort, xmlelement, mode);
133
[6421]134        XMLPortObject(PickupSpawner, Pickupable, "pickup", setPickupable, getPickupable, xmlelement, mode);
[7163]135
[2917]136        XMLPortParam(PickupSpawner, "triggerDistance", setTriggerDistance, getTriggerDistance, xmlelement, mode);
137        XMLPortParam(PickupSpawner, "respawnTime", setRespawnTime, getRespawnTime, xmlelement, mode);
[5953]138        XMLPortParam(PickupSpawner, "maxSpawnedItems", setMaxSpawnedItems, getMaxSpawnedItems, xmlelement, mode);
[7163]139
[6475]140        if(this->pickup_ == NULL)
[6405]141        {
[6475]142            COUT(2) << "A PickupSpawner was created without a valid Pickupable. This won't work." << std::endl;
143            this->setActive(false);
[6405]144        }
[6475]145        else
[6405]146        {
[6475]147            PickupRepresentation* representation = PickupManager::getInstance().getRepresentation(this->pickup_->getPickupIdentifier());
[6484]148            representation->setVisible(this->isActive());
[6475]149            this->attach(representation->getSpawnerRepresentation(this));
[6484]150            this->setActive(true);
[6405]151        }
[2917]152    }
[7163]153
[6421]154    /**
155    @brief
[6475]156        Invoked when the activity has changed. Sets visibility of attached objects.
[6421]157    */
[6475]158    void PickupSpawner::changedActivity()
[6405]159    {
[6475]160        SUPER(PickupSpawner, changedActivity);
[5947]161
[6486]162        this->setVisible(this->isActive());
[5953]163    }
[7163]164
[2917]165    /**
[5947]166    @brief
167        Tick, checks if any Pawn is close enough to trigger.
168    @param dt
169        Time since last tick.
[2917]170    */
[6540]171    //TODO: Replace with collisions.
[2917]172    void PickupSpawner::tick(float dt)
173    {
[6540]174        SUPER(PickupSpawner, tick, dt);
[7163]175
[6405]176        //! If the PickupSpawner is active.
[2917]177        if (this->isActive())
178        {
[7163]179            SmartPtr<PickupSpawner> temp = this; //Create a smart pointer to keep the PickupSpawner alive until we iterated through all Pawns (in case a Pawn takes the last pickup)
180
[6405]181            //! Iterate trough all Pawns.
[5929]182            for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it)
[2917]183            {
184                Vector3 distance = it->getWorldPosition() - this->getWorldPosition();
[6711]185                PickupCarrier* carrier = dynamic_cast<PickupCarrier*>(*it);
[6405]186                //! If a Pawn, that fits the target-range of the item spawned by this Pickup, is in trigger-distance.
[6711]187                if (distance.length() < this->triggerDistance_ && carrier != NULL && carrier->isTarget(this->pickup_))
[6405]188                {
[2917]189                    this->trigger(*it);
[6405]190                }
[2917]191            }
192        }
193    }
[7163]194
[2917]195    /**
[5947]196    @brief
[6475]197        Sets the maximum number of spawned items.
198    @param items
199        The maximum number of spawned items to be set.
[2917]200    */
[6475]201    void PickupSpawner::setMaxSpawnedItems(int items)
[2917]202    {
[6475]203        this->maxSpawnedItems_ = items;
204        this->spawnsRemaining_ = items;
[6405]205    }
[7163]206
[6421]207    /**
208    @brief
209        Decrements the number of remaining spawns.
210        Sets the PickupSpawner to inactive for the duration of the respawnTime.
211        Destroys the PickupSpawner if the number of remaining spawns has reached zero.
212    */
[6405]213    void PickupSpawner::decrementSpawnsRemaining(void)
214    {
215        if(this->spawnsRemaining_ != INF)
216        {
217            this->spawnsRemaining_--;
218        }
219        if(this->spawnsRemaining_ != 0 && this->respawnTime_ > 0)
220        {
[6512]221            this->startRespawnTimer();
[5953]222
[6405]223            this->setActive(false);
224            this->fireEvent();
225        }
226        else
[5953]227        {
[7163]228            COUT(4) << "PickupSpawner (&" << this << ") empty, selfdestruct initialized." << std::endl;
[5953]229            this->setActive(false);
[6475]230            this->destroy();
[5953]231        }
[2917]232    }
[7163]233
[6475]234    /**
235    @brief
[6512]236        Starts the respawn timer.
237    */
238    void PickupSpawner::startRespawnTimer(void)
239    {
240        this->respawnTimer_.setTimer(this->respawnTime_, false, createExecutor(createFunctor(&PickupSpawner::respawnTimerCallback, this)));
241    }
[7163]242
[6512]243    /**
244    @brief
[6475]245        Sets a Pickupable for the PickupSpawner to spawn.
246    @param pickup
247        The Pickupable to be set.
248    */
249    void PickupSpawner::setPickupable(Pickupable* pickup)
250    {
251        if(this->pickup_ != NULL)
252        {
[7163]253            COUT(1) << "In PickupSpawner (&" << this << "): setPickupable called, with this->pickup_ already set." << std::endl;
[6475]254            return;
255        }
256        if(pickup == NULL)
257        {
[7163]258            COUT(1) << "In PickupSpawner (&" << this << "): Argument of setPickupable is NULL." << std::endl;
[6475]259            return;
260        }
[7163]261
[6475]262        this->pickup_ = pickup;
263    }
[7163]264
[6475]265    /**
266    @brief
267        Get the Pickupable that is spawned by this PickupSpawner.
268    @return
269        Returns the Pickupable that is spawned by this PickupSpawner.
270    */
271    const Pickupable* PickupSpawner::getPickupable(void)
272    {
273        return this->pickup_;
274    }
[5947]275
[2917]276    /**
[5947]277    @brief
[6475]278        Trigger the PickupSpawner.
279        Adds the pickup to the Pawn that triggered, sets the timer to re-activate and deactives the PickupSpawner.
280    @param pawn
281        Pawn which triggered the PickupSpawner.
282    */
283    void PickupSpawner::trigger(Pawn* pawn)
284    {
[6484]285        if (this->isActive()) //!< Checks whether PickupSpawner is active.
[6475]286        {
[7163]287            COUT(4) << "PickupSpawner (&" << this << ") triggered and active." << std::endl;
288
[6475]289            PickupCarrier* carrier = dynamic_cast<PickupCarrier*>(pawn);
290            if(carrier == NULL)
291            {
292                COUT(1) << "This is bad. Pawn isn't PickupCarrier." << std::endl;
293                return;
294            }
[7163]295
[6475]296            if(!carrier->isTarget(this->pickup_))
297            {
[7163]298                COUT(4) << "PickupSpawner (&" << this << ") triggered but Pawn wasn't a target of the Pickupable." << std::endl;
[6475]299                return;
300            }
[7163]301
[6475]302            PickupCarrier* target = carrier->getTarget(this->pickup_);
303            Pickupable* pickup = this->getPickup();
[7163]304
[6492]305            if(target != NULL && pickup != NULL)
[6475]306            {
[7163]307                if(pickup->pickup(target))
[6475]308                    this->decrementSpawnsRemaining();
309                else
310                {
[7163]311                    this->selfDestruct_ = true;
[6475]312                    pickup->destroy();
313                }
314            }
315            else
316            {
317                if(target == NULL)
[7163]318                    COUT(1) << "PickupSpawner (&" << this << "): Pickupable has no target." << std::endl;
319
[6475]320                if(pickup == NULL)
[7163]321                    COUT(1) << "PickupSpawner (&" << this << "): getPickup produced an error, no Pickupable created." << std::endl;
[6475]322                else
323                {
[7163]324                    this->selfDestruct_ = true;
[6475]325                    pickup->destroy();
326                }
327            }
328        }
329    }
330
331    /**
332    @brief
[6405]333        Creates a new Pickupable.
[5953]334    @return
[6405]335        The Pickupable created.
[7163]336    */
[6405]337    Pickupable* PickupSpawner::getPickup(void)
[5953]338    {
[6405]339        if(this->spawnsRemaining_ == 0)
340        {
341            COUT(1) << "Massive Error: PickupSpawner still alive until having spawned last item." << std::endl;
342            return NULL;
343        }
[7163]344
[6405]345        Pickupable* pickup = this->pickup_->clone();
346        return pickup;
[5953]347    }
348
349    /**
350    @brief
[5947]351        Invoked by the timer, re-activates the PickupSpawner.
[2917]352    */
353    void PickupSpawner::respawnTimerCallback()
354    {
[7163]355        COUT(4) << "PickupSpawner (&" << this << ") reactivated." << std::endl;
[2917]356
357        this->setActive(true);
358    }
359}
Note: See TracBrowser for help on using the repository browser.