Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/resource/src/orxonox/objects/pickup/DroppedItem.cc @ 3607

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

Merged pch branch back to trunk.

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