Changeset 5953
- Timestamp:
- Oct 14, 2009, 4:36:56 PM (15 years ago)
- Location:
- code/branches/pickup2
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/pickup2
- Property svn:mergeinfo changed
/code/branches/pickup2 (added) merged: 5942 /code/trunk (added) merged: 5900-5901,5923,5929,5936-5938,5940
- Property svn:mergeinfo changed
-
code/branches/pickup2/src/orxonox/pickup/DroppedItem.cc
r5947 r5953 39 39 namespace orxonox 40 40 { 41 CreateFactory(DroppedItem); 41 CreateFactory(DroppedItem); //TODO: This isn't needed, is it? 42 42 43 43 /** … … 45 45 Constructor. Registers object and sets default values. 46 46 */ 47 DroppedItem::DroppedItem(BaseObject* creator) : StaticEntity(creator)47 DroppedItem::DroppedItem(BaseObject* creator) : PickupSpawner(creator) 48 48 { 49 49 RegisterObject(DroppedItem); 50 } 50 51 51 this->triggerDistance_ = 20.0f; 52 this->timeToLive_ = 0; 53 this->item_ = NULL; 52 DroppedItem::DroppedItem(BaseObject* creator, BaseItem* item, float triggerDistance, float respawnTime, int maxSpawnedItems) : PickupSpawner(creator, item, triggerDistance, respawnTime, maxSpawnedItems) 53 { 54 RegisterObject(DroppedItem); 55 this->item_ = item; 54 56 } 55 57 … … 58 60 Default destructor. 59 61 */ 60 //TODO: Destroy something?61 62 DroppedItem::~DroppedItem() 62 63 { … … 64 65 } 65 66 66 /** 67 @brief 68 Checks whether any pawn is in triggerDistance of the Item and calls this->trigger if so. 69 @param dt 70 The duration of the last time interval. 71 */ 72 //TODO: Replace this with a DistanceTrigger! 73 void DroppedItem::tick(float dt) 67 BaseItem* DroppedItem::getItem(void) 74 68 { 75 if (this->item_) 76 { 77 for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it) //!< Iterate through all Pawns. 78 { 79 Vector3 distance = it->getWorldPosition() - this->getWorldPosition(); 80 if (distance.length() < this->triggerDistance_) 81 this->trigger(*it); 82 } 83 } 84 } 85 86 /** 87 @brief 88 Called when the DroppedItem is triggered. Adds the item to the triggering pawn. 89 */ 90 void DroppedItem::trigger(Pawn* pawn) 91 { 92 if (this->item_->pickedUp(pawn)) //If pickup was successful. 93 { 94 COUT(3) << "DroppedItem '" << this->item_->getPickupIdentifier() << "' picked up." << std::endl; 95 this->destroy(); 96 } 97 } 98 99 /** 100 @brief 101 Creates a timer to call this->timerCallback() at expiration of timeToLive. 102 */ 103 //TODO: Better Comments. 104 void DroppedItem::createTimer() 105 { 106 if (this->timeToLive_ > 0) 107 { 108 this->timer_.setTimer(this->timeToLive_, false, createExecutor(createFunctor(&DroppedItem::timerCallback, this)), false); 109 } 110 } 111 112 /** 113 @brief 114 Destroys the item. Called by the set timer upon its expiration. 115 */ 116 //TODO: Choose better function name if this doesn't create dependency inconsistencies. e.g. this->destroy() or this->timeOut() 117 //Make certain that only one pawn has the same item, because if not, deliting the item would lead to a possible segfault. 118 //If the item is destroyed here, shouldn't it be destroyed in the destructor as well? 119 void DroppedItem::timerCallback() 120 { 121 if (this->item_) 122 { 123 COUT(3) << "Delete DroppedItem with '" << this->item_->getPickupIdentifier() << "'" << std::endl; 124 this->item_->destroy(); 125 } 126 127 this->destroy(); 69 return this->item_; 128 70 } 129 71 … … 133 75 */ 134 76 //TODO: Comment. 135 //This is for pawns dropping items they have...136 //Probably better to create a spawner with only 1 item in it.137 //Various different thigs are done here, which in my opinion should eighter be done in XML or some where else, preferably in XML.138 77 //Each pickup should have a XML template where the Model and Billboard, and so on, is specified. 139 //The position, item and timetoLive should be specified by this Classes XMLPort function.140 //These adjustments above, will very likely create inkonsistencies in the level files, possibly templates.141 78 /*static*/ DroppedItem* DroppedItem::createDefaultDrop(BaseItem* item, const Vector3& position, const ColourValue& flareColour, float timeToLive) 142 79 { 143 DroppedItem* drop = new DroppedItem(item); 80 //TODO: triggerDistance? 81 float triggerDistance = 20.0; 82 DroppedItem* droppedItem = new DroppedItem(item, item, triggerDistance, 0, 1); 83 84 //TODO: Do this somehwere else? 144 85 Model* model = new Model(item); 145 86 Billboard* billboard = new Billboard(item); … … 152 93 billboard->setScale(0.5f); 153 94 154 drop->setPosition(position); 155 drop->attach(model); 156 drop->attach(billboard); 157 158 drop->setItem(item); 159 160 drop->setTimeToLive(timeToLive); 161 drop->createTimer(); 95 droppedItem->setPosition(position); 96 droppedItem->attach(model); 97 droppedItem->attach(billboard); 162 98 163 99 COUT(3) << "Created DroppedItem for '" << item->getPickupIdentifier() << "' at (" << position.x << "," << position.y << "," << position.z << ")." << std::endl; 164 100 165 return drop ;101 return droppedItem; 166 102 } 167 103 -
code/branches/pickup2/src/orxonox/pickup/DroppedItem.h
r5947 r5953 37 37 #include "OrxonoxPrereqs.h" 38 38 39 #include "tools/Timer.h" 40 #include "tools/interfaces/Tickable.h" 41 #include "worldentities/StaticEntity.h" 39 #include "PickupSpawner.h" 42 40 43 41 namespace orxonox 44 42 { 45 class _OrxonoxExport DroppedItem : public StaticEntity, public Tickable43 class _OrxonoxExport DroppedItem : public PickupSpawner 46 44 { 47 public: 48 DroppedItem(BaseObject* creator); 49 virtual ~DroppedItem(); 45 public: 46 DroppedItem(BaseObject* creator); 47 DroppedItem(BaseObject* creator, BaseItem* item, float triggerDistance, float respawnTime, int maxSpawnedItems); 48 virtual ~DroppedItem(); 50 49 51 //TODO: Comment. 52 //DroppedItem -> Item with no owner, alone in space? 53 //Would be much nicer if it would be triggered by a standard issue DistanceTrigger. 54 //Where is this created? I see no XMLPort. 55 //Where is the item for this created? What happens if more than one pawn triggers this? 56 //Add more than just one items, or even better create the ability to add a Collection.? Rename to ...? 50 static DroppedItem* createDefaultDrop(BaseItem* item, const Vector3& position, const ColourValue& flareColour = ColourValue(0.5f, 1.0f, 0.3f), float timeToLive = 0); 51 static DroppedItem* createDefaultDrop(BaseItem* item, Pawn* pawn, const ColourValue& flareColour = ColourValue(0.5f, 1.0f, 0.3f), float timeToLive = 0); 57 52 58 void tick(float dt);59 void trigger(Pawn* pawn);53 protected: 54 virtual BaseItem* getItem(void); 60 55 61 static DroppedItem* createDefaultDrop(BaseItem* item, const Vector3& position, const ColourValue& flareColour = ColourValue(0.5f, 1.0f, 0.3f), float timeToLive = 0); 62 static DroppedItem* createDefaultDrop(BaseItem* item, Pawn* pawn, const ColourValue& flareColour = ColourValue(0.5f, 1.0f, 0.3f), float timeToLive = 0); 56 private: 63 57 64 void createTimer(); //TODO: Can this be made private, too? 65 void timerCallback(); //TODO: This should really be private. 58 BaseItem* item_; //!< The dropped item. 66 59 67 inline float getTriggerDistance() const68 { return this->triggerDistance_; }69 inline void setTriggerDistance(float distance)70 { this->triggerDistance_ = distance; }71 72 inline BaseItem* getItem() const73 { return this->item_; }74 //TODO: Needs to be public?75 inline void setItem(BaseItem* item)76 { this->item_ = item; }77 78 inline float getTimeToLive() const79 { return this->timeToLive_; }80 //TODO: Needs to be public?81 inline void setTimeToLive(float time)82 { this->timeToLive_ = time; }83 private:84 float timeToLive_;85 float triggerDistance_;86 BaseItem* item_;87 88 Timer timer_;89 60 }; 90 61 } -
code/branches/pickup2/src/orxonox/pickup/PickupSpawner.cc
r5947 r5953 46 46 namespace orxonox 47 47 { 48 48 49 const float PickupSpawner::bounceSpeed_s = 6.0f; 49 50 const float PickupSpawner::rotationSpeed_s = 1.0f; … … 60 61 PickupSpawner::PickupSpawner(BaseObject* creator) : StaticEntity(creator) 61 62 { 63 this->initialize(); 64 } 65 66 PickupSpawner::PickupSpawner(BaseObject* creator, BaseItem* item, float triggerDistance, float respawnTime, int maxSpawnedItems) : StaticEntity(creator) 67 { 68 this->initialize(); 69 70 //TODO: Does this actually work? 71 this->itemTemplateName_ = item->getIdentifier()->getName(); 72 this->itemTemplate_ = Template::getTemplate(this->itemTemplateName_); 73 74 this->triggerDistance_ = triggerDistance; 75 this->respawnTime_ = respawnTime; 76 this->setMaxSpawnedItems(maxSpawnedItems); 77 } 78 79 void PickupSpawner::initialize(void) 80 { 62 81 RegisterObject(PickupSpawner); 63 82 64 this->itemTemplate_ = 0;83 this->itemTemplate_ = NULL; 65 84 this->triggerDistance_ = 20; 66 85 this->respawnTime_ = 0.0f; 67 86 this->tickSum_ = 0.0f; 87 this->maxSpawnedItems_ = INF; 88 this->spawnsRemaining_ = INF; 68 89 } 69 90 … … 92 113 XMLPortParam(PickupSpawner, "triggerDistance", setTriggerDistance, getTriggerDistance, xmlelement, mode); 93 114 XMLPortParam(PickupSpawner, "respawnTime", setRespawnTime, getRespawnTime, xmlelement, mode); 115 XMLPortParam(PickupSpawner, "maxSpawnedItems", setMaxSpawnedItems, getMaxSpawnedItems, xmlelement, mode); 94 116 95 117 //TODO: Kill hack. … … 137 159 } 138 160 161 void PickupSpawner::setMaxSpawnedItems(int items) 162 { 163 this->maxSpawnedItems_ = items; 164 this->spawnsRemaining_ = items; 165 } 166 139 167 /** 140 168 @brief … … 148 176 if (this->isActive()) 149 177 { 178 //! Triggers as soon as a Pawn is in the specified distance. 150 179 for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it) 151 180 { … … 154 183 this->trigger(*it); 155 184 } 185 186 //! Animation. 156 187 this->yaw(Radian(rotationSpeed_s*dt)); 157 188 this->tickSum_ += bounceSpeed_s*dt; … … 174 205 void PickupSpawner::trigger(Pawn* pawn) 175 206 { 176 if (this->isActive() && this->itemTemplate_ && this->itemTemplate_->getBaseclassIdentifier()) 177 { 178 BaseObject* newObject = this->itemTemplate_->getBaseclassIdentifier()->fabricate(this); 179 BaseItem* asItem = orxonox_cast<BaseItem*>(newObject); 180 if (asItem) 207 if (this->isActive() && this->itemTemplate_ && this->itemTemplate_->getBaseclassIdentifier()) //!< Checks whether PickupItem is active, amongst other things. 208 { 209 BaseItem* item = this->getItem(); 210 if (item != NULL) //!< If the conversion was successful. 181 211 { 182 asItem->setPickupIdentifier(this->itemTemplateName_);183 asItem->addTemplate(this->itemTemplate_);184 185 if (asItem->pickedUp(pawn))212 item->setPickupIdentifier(this->itemTemplateName_); //TODO: Needed? 213 item->addTemplate(this->itemTemplate_); //TODO: Does what? 214 215 if(item->pickedUp(pawn)) 186 216 { 187 217 COUT(3) << this->itemTemplateName_ << " got picked up." << std::endl; 188 218 189 if (this->respawnTime_ > 0.0f) 219 220 if(this->spawnsRemaining_ != INF) 221 { 222 this->spawnsRemaining_--; 223 } 224 225 if (this->spawnsRemaining_ != 0 && this->respawnTime_ > 0.0f) 190 226 { 191 227 this->respawnTimer_.setTimer(this->respawnTime_, false, createExecutor(createFunctor(&PickupSpawner::respawnTimerCallback, this))); … … 196 232 } 197 233 else 198 newObject->destroy(); 234 { 235 item->destroy(); 236 } 199 237 } 200 238 } 239 240 if(this->spawnsRemaining_ == 0) 241 { 242 COUT(3) << "PickupSpawner empty, selfdistruct initialized." << std::endl; 243 this->setActive(false); 244 this->destroy(); 245 } 246 } 247 248 /** 249 @brief 250 Creates a BaseItem of the type specified by the PickupSpawner. 251 @return 252 The BaseItem created. 253 */ 254 BaseItem* PickupSpawner::getItem(void) 255 { 256 BaseObject* newItem = this->itemTemplate_->getBaseclassIdentifier()->fabricate(this); //!< Creates new object of specified item type. 257 return orxonox_cast<BaseItem*>(newItem); 201 258 } 202 259 -
code/branches/pickup2/src/orxonox/pickup/PickupSpawner.h
r5947 r5953 53 53 //TODO: Add limit of items spawned here. Also possibility to spawn collections? 54 54 PickupSpawner(BaseObject* creator); 55 PickupSpawner(BaseObject* creator, BaseItem* item, float triggerDistance, float respawnTime, int maxSpawnedItems); 55 56 virtual ~PickupSpawner(); 56 57 … … 102 103 inline void setRespawnTime(float time) 103 104 { this->respawnTime_ = time; } 105 106 107 inline int getMaxSpawnedItems(void) 108 { return this->maxSpawnedItems_; } 109 void setMaxSpawnedItems(int items); 110 111 protected: 112 virtual BaseItem* getItem(void); 113 104 114 private: 115 void initialize(void); 116 105 117 std::string itemTemplateName_; //!< Template name of the item to spawn. 106 118 Template* itemTemplate_; //!< Template of the item to spawn. 119 120 int maxSpawnedItems_; //!< Maximum number of items spawned by this PickupSpawner. 121 int spawnsRemaining_; //!< Number of items that can be spawned by this PickupSpawner until it selfdestructs. 107 122 108 123 float triggerDistance_; //!< Distance in which this gets triggered. … … 116 131 float respawnTime_; //!< Time after which this gets re-actived. 117 132 Timer respawnTimer_; //!< Timer used for re-activating. 133 134 static const int INF = -1; //!< Constant for infinity. 118 135 }; 119 136 }
Note: See TracChangeset
for help on using the changeset viewer.