Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/objects/pickup/DroppedItem.cc @ 3182

Last change on this file since 3182 was 3182, checked in by rgrieder, 16 years ago

Found out that that Debug.h is officially included in CoreIncludes.h ;)

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