[Orxonox-commit 1235] r5953 - in code/branches/pickup2: . src/orxonox/pickup
dafrick at orxonox.net
dafrick at orxonox.net
Wed Oct 14 16:36:56 CEST 2009
Author: dafrick
Date: 2009-10-14 16:36:56 +0200 (Wed, 14 Oct 2009)
New Revision: 5953
Modified:
code/branches/pickup2/
code/branches/pickup2/src/orxonox/pickup/DroppedItem.cc
code/branches/pickup2/src/orxonox/pickup/DroppedItem.h
code/branches/pickup2/src/orxonox/pickup/PickupSpawner.cc
code/branches/pickup2/src/orxonox/pickup/PickupSpawner.h
Log:
Made DroppedItem inherit from PickupSpawner. Also minor changes in PickupSpawner.
Property changes on: code/branches/pickup2
___________________________________________________________________
Modified: svn:mergeinfo
- /code/branches/core5:5768-5928
/code/branches/libraries:5612-5692
/code/branches/libraries2:5703-5737
/code/branches/resource2:3372-5694
code/branches/buildsystem:1874-2276,2278-2400
code/branches/buildsystem2:2506-2658
code/branches/buildsystem3:2662-2708
code/branches/ceguilua:1802-1808
code/branches/core3:1572-1739
code/branches/core4:3221-3224,3227,3234-3238,3242,3244-3250,3252-3254,3256,3259-3261,3264-3265,3268-3275,3277-3278,3280,3284-3285,3287,3289-3294,3305,3309-3310
code/branches/gametypes:2826-3031
code/branches/gcc43:1580
code/branches/gui:1635-1723,2795-2894
code/branches/input:1629-1636
code/branches/lodfinal:2372-2411
code/branches/map:2801-3086,3089
code/branches/miniprojects:2754-2824
code/branches/netp2:2835-2988
code/branches/netp3:2988-3082
code/branches/netp6:3214-3302
code/branches/network:2356
code/branches/network64:2210-2355
code/branches/objecthierarchy:1911-2085,2100,2110-2169
code/branches/objecthierarchy2:2171-2479
code/branches/overlay:2117-2385
code/branches/particles:2829-3085
code/branches/pch:3113-3194
code/branches/physics:1912-2055,2107-2439
code/branches/physics_merge:2436-2457
code/branches/pickups:1926-2086,2127,2827-2915
code/branches/pickups2:2107-2497,2915-3071
code/branches/presentation:2369-2652,2654-2660
code/branches/questsystem:1894-2088
code/branches/questsystem2:2107-2259
code/branches/questsystem5:2776-2905
code/branches/resource:3327-3366
code/branches/script_trigger:1295-1953,1955
code/branches/sound:2829-3010
code/branches/weapon:1925-2094
code/branches/weapon2:2107-2488
code/branches/weapons:2897-3051
code/branches/weaponsystem:2742-2890
+ /code/branches/core5:5768-5928
/code/branches/libraries:5612-5692
/code/branches/libraries2:5703-5737
/code/branches/pickup2:5942
/code/branches/resource2:3372-5694
/code/trunk:5815-5941
code/branches/buildsystem:1874-2276,2278-2400
code/branches/buildsystem2:2506-2658
code/branches/buildsystem3:2662-2708
code/branches/ceguilua:1802-1808
code/branches/core3:1572-1739
code/branches/core4:3221-3224,3227,3234-3238,3242,3244-3250,3252-3254,3256,3259-3261,3264-3265,3268-3275,3277-3278,3280,3284-3285,3287,3289-3294,3305,3309-3310
code/branches/gametypes:2826-3031
code/branches/gcc43:1580
code/branches/gui:1635-1723,2795-2894
code/branches/input:1629-1636
code/branches/lodfinal:2372-2411
code/branches/map:2801-3086,3089
code/branches/miniprojects:2754-2824
code/branches/netp2:2835-2988
code/branches/netp3:2988-3082
code/branches/netp6:3214-3302
code/branches/network:2356
code/branches/network64:2210-2355
code/branches/objecthierarchy:1911-2085,2100,2110-2169
code/branches/objecthierarchy2:2171-2479
code/branches/overlay:2117-2385
code/branches/particles:2829-3085
code/branches/pch:3113-3194
code/branches/physics:1912-2055,2107-2439
code/branches/physics_merge:2436-2457
code/branches/pickups:1926-2086,2127,2827-2915
code/branches/pickups2:2107-2497,2915-3071
code/branches/presentation:2369-2652,2654-2660
code/branches/questsystem:1894-2088
code/branches/questsystem2:2107-2259
code/branches/questsystem5:2776-2905
code/branches/resource:3327-3366
code/branches/script_trigger:1295-1953,1955
code/branches/sound:2829-3010
code/branches/weapon:1925-2094
code/branches/weapon2:2107-2488
code/branches/weapons:2897-3051
code/branches/weaponsystem:2742-2890
Modified: code/branches/pickup2/src/orxonox/pickup/DroppedItem.cc
===================================================================
--- code/branches/pickup2/src/orxonox/pickup/DroppedItem.cc 2009-10-14 12:50:04 UTC (rev 5952)
+++ code/branches/pickup2/src/orxonox/pickup/DroppedItem.cc 2009-10-14 14:36:56 UTC (rev 5953)
@@ -38,109 +38,50 @@
namespace orxonox
{
- CreateFactory(DroppedItem);
+ CreateFactory(DroppedItem); //TODO: This isn't needed, is it?
/**
@brief
Constructor. Registers object and sets default values.
*/
- DroppedItem::DroppedItem(BaseObject* creator) : StaticEntity(creator)
+ DroppedItem::DroppedItem(BaseObject* creator) : PickupSpawner(creator)
{
RegisterObject(DroppedItem);
+ }
- this->triggerDistance_ = 20.0f;
- this->timeToLive_ = 0;
- this->item_ = NULL;
+ DroppedItem::DroppedItem(BaseObject* creator, BaseItem* item, float triggerDistance, float respawnTime, int maxSpawnedItems) : PickupSpawner(creator, item, triggerDistance, respawnTime, maxSpawnedItems)
+ {
+ RegisterObject(DroppedItem);
+ this->item_ = item;
}
/**
@brief
Default destructor.
*/
- //TODO: Destroy something?
DroppedItem::~DroppedItem()
{
}
- /**
- @brief
- Checks whether any pawn is in triggerDistance of the Item and calls this->trigger if so.
- @param dt
- The duration of the last time interval.
- */
- //TODO: Replace this with a DistanceTrigger!
- void DroppedItem::tick(float dt)
+ BaseItem* DroppedItem::getItem(void)
{
- if (this->item_)
- {
- for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it) //!< Iterate through all Pawns.
- {
- Vector3 distance = it->getWorldPosition() - this->getWorldPosition();
- if (distance.length() < this->triggerDistance_)
- this->trigger(*it);
- }
- }
+ return this->item_;
}
/**
@brief
- Called when the DroppedItem is triggered. Adds the item to the triggering pawn.
- */
- void DroppedItem::trigger(Pawn* pawn)
- {
- if (this->item_->pickedUp(pawn)) //If pickup was successful.
- {
- COUT(3) << "DroppedItem '" << this->item_->getPickupIdentifier() << "' picked up." << std::endl;
- this->destroy();
- }
- }
-
- /**
- @brief
- Creates a timer to call this->timerCallback() at expiration of timeToLive.
- */
- //TODO: Better Comments.
- void DroppedItem::createTimer()
- {
- if (this->timeToLive_ > 0)
- {
- this->timer_.setTimer(this->timeToLive_, false, createExecutor(createFunctor(&DroppedItem::timerCallback, this)), false);
- }
- }
-
- /**
- @brief
- Destroys the item. Called by the set timer upon its expiration.
- */
- //TODO: Choose better function name if this doesn't create dependency inconsistencies. e.g. this->destroy() or this->timeOut()
- //Make certain that only one pawn has the same item, because if not, deliting the item would lead to a possible segfault.
- //If the item is destroyed here, shouldn't it be destroyed in the destructor as well?
- void DroppedItem::timerCallback()
- {
- if (this->item_)
- {
- COUT(3) << "Delete DroppedItem with '" << this->item_->getPickupIdentifier() << "'" << std::endl;
- this->item_->destroy();
- }
-
- this->destroy();
- }
-
- /**
- @brief
*/
//TODO: Comment.
- //This is for pawns dropping items they have...
- //Probably better to create a spawner with only 1 item in it.
- //Various different thigs are done here, which in my opinion should eighter be done in XML or some where else, preferably in XML.
//Each pickup should have a XML template where the Model and Billboard, and so on, is specified.
- //The position, item and timetoLive should be specified by this Classes XMLPort function.
- //These adjustments above, will very likely create inkonsistencies in the level files, possibly templates.
/*static*/ DroppedItem* DroppedItem::createDefaultDrop(BaseItem* item, const Vector3& position, const ColourValue& flareColour, float timeToLive)
{
- DroppedItem* drop = new DroppedItem(item);
+ //TODO: triggerDistance?
+ float triggerDistance = 20.0;
+ DroppedItem* droppedItem = new DroppedItem(item, item, triggerDistance, 0, 1);
+
+ //TODO: Do this somehwere else?
Model* model = new Model(item);
Billboard* billboard = new Billboard(item);
@@ -151,18 +92,13 @@
billboard->setColour(flareColour);
billboard->setScale(0.5f);
- drop->setPosition(position);
- drop->attach(model);
- drop->attach(billboard);
+ droppedItem->setPosition(position);
+ droppedItem->attach(model);
+ droppedItem->attach(billboard);
- drop->setItem(item);
-
- drop->setTimeToLive(timeToLive);
- drop->createTimer();
-
COUT(3) << "Created DroppedItem for '" << item->getPickupIdentifier() << "' at (" << position.x << "," << position.y << "," << position.z << ")." << std::endl;
- return drop;
+ return droppedItem;
}
/**
Modified: code/branches/pickup2/src/orxonox/pickup/DroppedItem.h
===================================================================
--- code/branches/pickup2/src/orxonox/pickup/DroppedItem.h 2009-10-14 12:50:04 UTC (rev 5952)
+++ code/branches/pickup2/src/orxonox/pickup/DroppedItem.h 2009-10-14 14:36:56 UTC (rev 5953)
@@ -36,56 +36,27 @@
#include "OrxonoxPrereqs.h"
-#include "tools/Timer.h"
-#include "tools/interfaces/Tickable.h"
-#include "worldentities/StaticEntity.h"
+#include "PickupSpawner.h"
namespace orxonox
{
- class _OrxonoxExport DroppedItem : public StaticEntity, public Tickable
+ class _OrxonoxExport DroppedItem : public PickupSpawner
{
- public:
- DroppedItem(BaseObject* creator);
- virtual ~DroppedItem();
+ public:
+ DroppedItem(BaseObject* creator);
+ DroppedItem(BaseObject* creator, BaseItem* item, float triggerDistance, float respawnTime, int maxSpawnedItems);
+ virtual ~DroppedItem();
- //TODO: Comment.
- //DroppedItem -> Item with no owner, alone in space?
- //Would be much nicer if it would be triggered by a standard issue DistanceTrigger.
- //Where is this created? I see no XMLPort.
- //Where is the item for this created? What happens if more than one pawn triggers this?
- //Add more than just one items, or even better create the ability to add a Collection.? Rename to ...?
+ static DroppedItem* createDefaultDrop(BaseItem* item, const Vector3& position, const ColourValue& flareColour = ColourValue(0.5f, 1.0f, 0.3f), float timeToLive = 0);
+ static DroppedItem* createDefaultDrop(BaseItem* item, Pawn* pawn, const ColourValue& flareColour = ColourValue(0.5f, 1.0f, 0.3f), float timeToLive = 0);
- void tick(float dt);
- void trigger(Pawn* pawn);
+ protected:
+ virtual BaseItem* getItem(void);
- static DroppedItem* createDefaultDrop(BaseItem* item, const Vector3& position, const ColourValue& flareColour = ColourValue(0.5f, 1.0f, 0.3f), float timeToLive = 0);
- static DroppedItem* createDefaultDrop(BaseItem* item, Pawn* pawn, const ColourValue& flareColour = ColourValue(0.5f, 1.0f, 0.3f), float timeToLive = 0);
+ private:
- void createTimer(); //TODO: Can this be made private, too?
- void timerCallback(); //TODO: This should really be private.
+ BaseItem* item_; //!< The dropped item.
- inline float getTriggerDistance() const
- { return this->triggerDistance_; }
- inline void setTriggerDistance(float distance)
- { this->triggerDistance_ = distance; }
-
- inline BaseItem* getItem() const
- { return this->item_; }
- //TODO: Needs to be public?
- inline void setItem(BaseItem* item)
- { this->item_ = item; }
-
- inline float getTimeToLive() const
- { return this->timeToLive_; }
- //TODO: Needs to be public?
- inline void setTimeToLive(float time)
- { this->timeToLive_ = time; }
- private:
- float timeToLive_;
- float triggerDistance_;
- BaseItem* item_;
-
- Timer timer_;
};
}
Modified: code/branches/pickup2/src/orxonox/pickup/PickupSpawner.cc
===================================================================
--- code/branches/pickup2/src/orxonox/pickup/PickupSpawner.cc 2009-10-14 12:50:04 UTC (rev 5952)
+++ code/branches/pickup2/src/orxonox/pickup/PickupSpawner.cc 2009-10-14 14:36:56 UTC (rev 5953)
@@ -45,6 +45,7 @@
namespace orxonox
{
+
const float PickupSpawner::bounceSpeed_s = 6.0f;
const float PickupSpawner::rotationSpeed_s = 1.0f;
const float PickupSpawner::bounceDistance_s = 4.0f;
@@ -59,12 +60,32 @@
*/
PickupSpawner::PickupSpawner(BaseObject* creator) : StaticEntity(creator)
{
+ this->initialize();
+ }
+
+ PickupSpawner::PickupSpawner(BaseObject* creator, BaseItem* item, float triggerDistance, float respawnTime, int maxSpawnedItems) : StaticEntity(creator)
+ {
+ this->initialize();
+
+ //TODO: Does this actually work?
+ this->itemTemplateName_ = item->getIdentifier()->getName();
+ this->itemTemplate_ = Template::getTemplate(this->itemTemplateName_);
+
+ this->triggerDistance_ = triggerDistance;
+ this->respawnTime_ = respawnTime;
+ this->setMaxSpawnedItems(maxSpawnedItems);
+ }
+
+ void PickupSpawner::initialize(void)
+ {
RegisterObject(PickupSpawner);
- this->itemTemplate_ = 0;
+ this->itemTemplate_ = NULL;
this->triggerDistance_ = 20;
this->respawnTime_ = 0.0f;
this->tickSum_ = 0.0f;
+ this->maxSpawnedItems_ = INF;
+ this->spawnsRemaining_ = INF;
}
/**
@@ -91,6 +112,7 @@
XMLPortParam(PickupSpawner, "item", setItemTemplateName, getItemTemplateName, xmlelement, mode);
XMLPortParam(PickupSpawner, "triggerDistance", setTriggerDistance, getTriggerDistance, xmlelement, mode);
XMLPortParam(PickupSpawner, "respawnTime", setRespawnTime, getRespawnTime, xmlelement, mode);
+ XMLPortParam(PickupSpawner, "maxSpawnedItems", setMaxSpawnedItems, getMaxSpawnedItems, xmlelement, mode);
//TODO: Kill hack.
// HACKs
@@ -136,6 +158,12 @@
this->itemTemplate_ = Template::getTemplate(name);
}
+ void PickupSpawner::setMaxSpawnedItems(int items)
+ {
+ this->maxSpawnedItems_ = items;
+ this->spawnsRemaining_ = items;
+ }
+
/**
@brief
Tick, checks if any Pawn is close enough to trigger.
@@ -147,12 +175,15 @@
{
if (this->isActive())
{
+ //! Triggers as soon as a Pawn is in the specified distance.
for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it)
{
Vector3 distance = it->getWorldPosition() - this->getWorldPosition();
if (distance.length() < this->triggerDistance_)
this->trigger(*it);
}
+
+ //! Animation.
this->yaw(Radian(rotationSpeed_s*dt));
this->tickSum_ += bounceSpeed_s*dt;
this->translate(Vector3(0,bounceDistance_s*dt*sin(this->tickSum_),0));
@@ -173,21 +204,26 @@
*/
void PickupSpawner::trigger(Pawn* pawn)
{
- if (this->isActive() && this->itemTemplate_ && this->itemTemplate_->getBaseclassIdentifier())
+ if (this->isActive() && this->itemTemplate_ && this->itemTemplate_->getBaseclassIdentifier()) //!< Checks whether PickupItem is active, amongst other things.
{
- BaseObject* newObject = this->itemTemplate_->getBaseclassIdentifier()->fabricate(this);
- BaseItem* asItem = orxonox_cast<BaseItem*>(newObject);
- if (asItem)
+ BaseItem* item = this->getItem();
+ if (item != NULL) //!< If the conversion was successful.
{
- asItem->setPickupIdentifier(this->itemTemplateName_);
- asItem->addTemplate(this->itemTemplate_);
+ item->setPickupIdentifier(this->itemTemplateName_); //TODO: Needed?
+ item->addTemplate(this->itemTemplate_); //TODO: Does what?
- if (asItem->pickedUp(pawn))
+ if(item->pickedUp(pawn))
{
COUT(3) << this->itemTemplateName_ << " got picked up." << std::endl;
- if (this->respawnTime_ > 0.0f)
+
+ if(this->spawnsRemaining_ != INF)
{
+ this->spawnsRemaining_--;
+ }
+
+ if (this->spawnsRemaining_ != 0 && this->respawnTime_ > 0.0f)
+ {
this->respawnTimer_.setTimer(this->respawnTime_, false, createExecutor(createFunctor(&PickupSpawner::respawnTimerCallback, this)));
this->setActive(false);
@@ -195,13 +231,34 @@
}
}
else
- newObject->destroy();
+ {
+ item->destroy();
+ }
}
}
+
+ if(this->spawnsRemaining_ == 0)
+ {
+ COUT(3) << "PickupSpawner empty, selfdistruct initialized." << std::endl;
+ this->setActive(false);
+ this->destroy();
+ }
}
/**
@brief
+ Creates a BaseItem of the type specified by the PickupSpawner.
+ @return
+ The BaseItem created.
+ */
+ BaseItem* PickupSpawner::getItem(void)
+ {
+ BaseObject* newItem = this->itemTemplate_->getBaseclassIdentifier()->fabricate(this); //!< Creates new object of specified item type.
+ return orxonox_cast<BaseItem*>(newItem);
+ }
+
+ /**
+ @brief
Invoked by the timer, re-activates the PickupSpawner.
*/
void PickupSpawner::respawnTimerCallback()
Modified: code/branches/pickup2/src/orxonox/pickup/PickupSpawner.h
===================================================================
--- code/branches/pickup2/src/orxonox/pickup/PickupSpawner.h 2009-10-14 12:50:04 UTC (rev 5952)
+++ code/branches/pickup2/src/orxonox/pickup/PickupSpawner.h 2009-10-14 14:36:56 UTC (rev 5953)
@@ -52,6 +52,7 @@
public:
//TODO: Add limit of items spawned here. Also possibility to spawn collections?
PickupSpawner(BaseObject* creator);
+ PickupSpawner(BaseObject* creator, BaseItem* item, float triggerDistance, float respawnTime, int maxSpawnedItems);
virtual ~PickupSpawner();
virtual void changedActivity(); //!< Invoked when activity has changed (set visibilty).
@@ -101,10 +102,24 @@
*/
inline void setRespawnTime(float time)
{ this->respawnTime_ = time; }
+
+
+ inline int getMaxSpawnedItems(void)
+ { return this->maxSpawnedItems_; }
+ void setMaxSpawnedItems(int items);
+
+ protected:
+ virtual BaseItem* getItem(void);
+
private:
+ void initialize(void);
+
std::string itemTemplateName_; //!< Template name of the item to spawn.
Template* itemTemplate_; //!< Template of the item to spawn.
+ int maxSpawnedItems_; //!< Maximum number of items spawned by this PickupSpawner.
+ int spawnsRemaining_; //!< Number of items that can be spawned by this PickupSpawner until it selfdestructs.
+
float triggerDistance_; //!< Distance in which this gets triggered.
/* Pickup animation */
@@ -115,6 +130,8 @@
float respawnTime_; //!< Time after which this gets re-actived.
Timer respawnTimer_; //!< Timer used for re-activating.
+
+ static const int INF = -1; //!< Constant for infinity.
};
}
More information about the Orxonox-commit
mailing list