Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickup2/src/orxonox/pickup/DroppedItem.cc @ 5947

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

Merged pickup branch revisions to pickup2.

  • Property svn:eol-style set to native
File size: 5.7 KB
RevLine 
[3079]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
[2917]29#include "DroppedItem.h"
30
[3196]31#include "util/Math.h"
32#include "core/CoreIncludes.h"
33#include "core/Executor.h"
[2917]34#include "BaseItem.h"
[5737]35#include "graphics/Billboard.h"
36#include "graphics/Model.h"
[5735]37#include "worldentities/pawns/Pawn.h"
[2917]38
39namespace orxonox
40{
41    CreateFactory(DroppedItem);
42
[5947]43    /**
44    @brief
45        Constructor. Registers object and sets default values.
46    */
[2917]47    DroppedItem::DroppedItem(BaseObject* creator) : StaticEntity(creator)
48    {
49        RegisterObject(DroppedItem);
50
51        this->triggerDistance_ = 20.0f;
52        this->timeToLive_ = 0;
[5947]53        this->item_ = NULL;
[2917]54    }
[5947]55
56    /**
57    @brief
58        Default destructor.
59    */
60    //TODO: Destroy something?
[2917]61    DroppedItem::~DroppedItem()
62    {
[5947]63       
[2917]64    }
[5947]65
66    /**
67    @brief
68        Checks whether any pawn is in triggerDistance of the Item and calls this->trigger if so.
69    @param dt
70        The  duration of the last time interval.   
71    */
72    //TODO: Replace this with a DistanceTrigger!
[2917]73    void DroppedItem::tick(float dt)
74    {
75        if (this->item_)
76        {
[5947]77            for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it) //!< Iterate through all Pawns.
[2917]78            {
79                Vector3 distance = it->getWorldPosition() - this->getWorldPosition();
80                if (distance.length() < this->triggerDistance_)
81                    this->trigger(*it);
82            }
83        }
84    }
[5947]85
86    /**
87    @brief
88        Called when the DroppedItem is triggered. Adds the item to the triggering pawn.
89    */
[2917]90    void DroppedItem::trigger(Pawn* pawn)
91    {
[5947]92        if (this->item_->pickedUp(pawn)) //If pickup was successful.
[2917]93        {
94            COUT(3) << "DroppedItem '" << this->item_->getPickupIdentifier() << "' picked up." << std::endl;
[5929]95            this->destroy();
[2917]96        }
97    }
[5947]98
99    /**
100    @brief
101        Creates a timer to call this->timerCallback() at expiration of timeToLive.
102    */
103    //TODO: Better Comments.
[2917]104    void DroppedItem::createTimer()
105    {
106        if (this->timeToLive_ > 0)
107        {
[5929]108            this->timer_.setTimer(this->timeToLive_, false, createExecutor(createFunctor(&DroppedItem::timerCallback, this)), false);
[2917]109        }
110    }
[5947]111   
112    /**
113    @brief
114        Destroys the item. Called by the set timer upon its expiration.
115    */
116    //TODO: Choose better function name if this doesn't create dependency inconsistencies. e.g. this->destroy() or this->timeOut()
117    //Make certain that only one pawn has the same item, because if not, deliting the item would lead to a possible segfault.
118    //If the item is destroyed here, shouldn't it be destroyed in the destructor as well?
[2917]119    void DroppedItem::timerCallback()
120    {
121        if (this->item_)
[2972]122        {
123            COUT(3) << "Delete DroppedItem with '" << this->item_->getPickupIdentifier() << "'" << std::endl;
[5929]124            this->item_->destroy();
[2972]125        }
[2917]126
[5929]127        this->destroy();
[2917]128    }
129
[5947]130    /**
131    @brief
132       
133    */
134    //TODO: Comment.
135    //This is for pawns dropping items they have...
136    //Probably better to create a spawner with only 1 item in it.
137    //Various different thigs are done here, which in my opinion should eighter be done in XML or some where else, preferably in XML.
138    //Each pickup should have a XML template where the Model and Billboard, and so on, is specified.
139    //The position, item and timetoLive should be specified by this Classes XMLPort function.
140    //These adjustments above, will very likely create inkonsistencies in the level files, possibly templates.
141    /*static*/ DroppedItem* DroppedItem::createDefaultDrop(BaseItem* item, const Vector3& position, const ColourValue& flareColour, float timeToLive)
[2917]142    {
143        DroppedItem* drop = new DroppedItem(item);
144        Model* model = new Model(item);
145        Billboard* billboard = new Billboard(item);
[3079]146
[2917]147        model->setMeshSource("sphere.mesh");
148        model->setScale(3.0f);
149
150        billboard->setMaterial("Examples/Flare");
151        billboard->setColour(flareColour);
152        billboard->setScale(0.5f);
153
154        drop->setPosition(position);
155        drop->attach(model);
156        drop->attach(billboard);
157
158        drop->setItem(item);
159
160        drop->setTimeToLive(timeToLive);
161        drop->createTimer();
162
163        COUT(3) << "Created DroppedItem for '" << item->getPickupIdentifier() << "' at (" << position.x << "," << position.y << "," << position.z << ")." << std::endl;
164
165        return drop;
166    }
[5947]167
168    /**
169    @brief
170
171    */
172    //TODO: See one function above.
[2917]173    DroppedItem* DroppedItem::createDefaultDrop(BaseItem* item, Pawn* pawn, const ColourValue& flareColour, float timeToLive)
174    {
175        Vector3 after = pawn->getPosition() + pawn->getOrientation() * Vector3(0.0f, 0.0f, 50.0f);
176        return DroppedItem::createDefaultDrop(item, after, flareColour, timeToLive);
177    }
178}
Note: See TracBrowser for help on using the repository browser.