Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/pickup/DroppedItem.cc @ 3132

Last change on this file since 3132 was 3079, checked in by landauf, 16 years ago

Added forward declarations to OrxonoxPrereqs.h.

  • Property svn:eol-style set to native
File size: 3.8 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 "BaseItem.h"
32#include "objects/worldentities/pawns/Pawn.h"
33#include "objects/worldentities/Model.h"
34#include "objects/worldentities/Billboard.h"
35
36#include "core/CoreIncludes.h"
37
38namespace orxonox
39{
40    CreateFactory(DroppedItem);
41
42    DroppedItem::DroppedItem(BaseObject* creator) : StaticEntity(creator)
43    {
44        RegisterObject(DroppedItem);
45
46        this->triggerDistance_ = 20.0f;
47        this->timeToLive_ = 0;
48        this->item_ = 0;
49    }
50    DroppedItem::~DroppedItem()
51    {
52    }
53    void DroppedItem::tick(float dt)
54    {
55        if (this->item_)
56        {
57            for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); it++)
58            {
59                Vector3 distance = it->getWorldPosition() - this->getWorldPosition();
60                if (distance.length() < this->triggerDistance_)
61                    this->trigger(*it);
62            }
63        }
64    }
65    void DroppedItem::trigger(Pawn* pawn)
66    {
67        if (this->item_->pickedUp(pawn))
68        {
69            COUT(3) << "DroppedItem '" << this->item_->getPickupIdentifier() << "' picked up." << std::endl;
70            delete this;
71        }
72    }
73    void DroppedItem::createTimer()
74    {
75        if (this->timeToLive_ > 0)
76        {
77            ExecutorMember<DroppedItem>* exec = createExecutor(createFunctor(&DroppedItem::timerCallback));
78            this->timer_.setTimer(this->timeToLive_, false, this, exec, false);
79        }
80    }
81    void DroppedItem::timerCallback()
82    {
83        if (this->item_)
84        {
85            COUT(3) << "Delete DroppedItem with '" << this->item_->getPickupIdentifier() << "'" << std::endl;
86            delete this->item_;
87        }
88
89        delete this;
90    }
91
92    DroppedItem* DroppedItem::createDefaultDrop(BaseItem* item, const Vector3& position, const ColourValue& flareColour, float timeToLive)
93    {
94        DroppedItem* drop = new DroppedItem(item);
95        Model* model = new Model(item);
96        Billboard* billboard = new Billboard(item);
97
98        model->setMeshSource("sphere.mesh");
99        model->setScale(3.0f);
100
101        billboard->setMaterial("Examples/Flare");
102        billboard->setColour(flareColour);
103        billboard->setScale(0.5f);
104
105        drop->setPosition(position);
106        drop->attach(model);
107        drop->attach(billboard);
108
109        drop->setItem(item);
110
111        drop->setTimeToLive(timeToLive);
112        drop->createTimer();
113
114        COUT(3) << "Created DroppedItem for '" << item->getPickupIdentifier() << "' at (" << position.x << "," << position.y << "," << position.z << ")." << std::endl;
115
116        return drop;
117    }
118    DroppedItem* DroppedItem::createDefaultDrop(BaseItem* item, Pawn* pawn, const ColourValue& flareColour, float timeToLive)
119    {
120        Vector3 after = pawn->getPosition() + pawn->getOrientation() * Vector3(0.0f, 0.0f, 50.0f);
121        return DroppedItem::createDefaultDrop(item, after, flareColour, timeToLive);
122    }
123}
Note: See TracBrowser for help on using the repository browser.