Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Cleanup in pickup and quest (almost only cleaned the header file sections).

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