Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5902 was 5902, checked in by dafrick, 15 years ago

Added comments and a lot of TODO's, many of which probably are obsolete by now, so don't mind them, they're just to help me remember, what I thought at the time I went through that particular code.

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