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 | * Damian 'Mozork' Frick |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file PickupSpawner.h |
---|
31 | @brief Definition of the PickupSpawner class. |
---|
32 | @ingroup Pickup |
---|
33 | */ |
---|
34 | |
---|
35 | #ifndef _PickupSpawner_H__ |
---|
36 | #define _PickupSpawner_H__ |
---|
37 | |
---|
38 | #include "PickupPrereqs.h" |
---|
39 | |
---|
40 | #include <map> |
---|
41 | #include <string> |
---|
42 | #include "tools/Timer.h" |
---|
43 | |
---|
44 | #include "interfaces/Pickupable.h" |
---|
45 | |
---|
46 | #include "tools/interfaces/Tickable.h" |
---|
47 | #include "worldentities/StaticEntity.h" |
---|
48 | |
---|
49 | namespace orxonox |
---|
50 | { |
---|
51 | /** |
---|
52 | @brief |
---|
53 | The PickupSpawner class is responsible for spawning @ref orxonox::Pickupable "Pickupables" of a specific type. |
---|
54 | Furthermore it can be specified how long the time interval between spawning two items is and how many @ref orxonox::Pickupable "Pickupables" are spawned at maximum, amongst other things. The parameters that can be used to further specify the behaviour of the PickupSpawner are: |
---|
55 | - The <b>triggerDistance</b> can be used to specify how far away an entity has to be to still trigger the PickupSPawner (and thus receive a @ref orxonox::Pickupable "Pickupable" form it). The default is 10. |
---|
56 | - The <b>respawnTime</b> is the time in seconds that passes until the PickupSpawner is enabled again, after having spawned a @ref orxonox::Pickupable "Pickupable". The default is 0. |
---|
57 | - The <b>maxSpawnedItems</b> is the number of @ref orxonox::Pickupable "Pickupables" that are spawned by this PickupSpawner at the most. The default is -1, which denotes infinity. |
---|
58 | |
---|
59 | A PickupSpawner is created in XML, which can be done in the following fashion: |
---|
60 | @code |
---|
61 | <PickupSpawner position="-100,0,-100" respawnTime="30" maxSpawnedItems="10"> |
---|
62 | <pickup> |
---|
63 | <SomePickup > |
---|
64 | </pickup> |
---|
65 | </PickupSpawner> |
---|
66 | @endcode |
---|
67 | As we can see, since the PickupSpawner is a StaticEntity, it also has spatial coordinates. We can also see, that the type of @ref orxonox::Pickupable "Pickupable" which is spawned hast to be specified as well. |
---|
68 | |
---|
69 | @author |
---|
70 | Daniel 'Huty' Haggenmueller |
---|
71 | @author |
---|
72 | Damian 'Mozork' Frick |
---|
73 | |
---|
74 | @ingroup Pickup |
---|
75 | */ |
---|
76 | class _PickupExport PickupSpawner : public StaticEntity, public Tickable |
---|
77 | { |
---|
78 | public: |
---|
79 | PickupSpawner(Context* context); //!< Default Constructor. |
---|
80 | virtual ~PickupSpawner(); //!< Destructor. |
---|
81 | |
---|
82 | static PickupSpawner* createDroppedPickup(Context* context, Pickupable* pickup, PickupCarrier* carrier, float triggerDistance = 10.0); |
---|
83 | |
---|
84 | virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode) override; //!< Method for creating a PickupSpawner through XML. |
---|
85 | virtual void tick(float dt) override; //!< Tick, checks if any Pawn is close enough to trigger. |
---|
86 | |
---|
87 | /** |
---|
88 | @brief Get the distance in which to trigger. |
---|
89 | @return Returns the distance in which this gets triggered. |
---|
90 | */ |
---|
91 | inline float getTriggerDistance() const |
---|
92 | { return this->triggerDistance_; } |
---|
93 | /** |
---|
94 | @brief Get the time to respawn. |
---|
95 | @returns Returns the time after which this gets re-actived. |
---|
96 | */ |
---|
97 | inline float getRespawnTime() const |
---|
98 | { return this->respawnTime_; } |
---|
99 | /** |
---|
100 | @brief Get the maximum number of items that will be spawned by this PickupSpawner. |
---|
101 | @return Returns the maximum number of items spawned by this PickupSpawner. |
---|
102 | */ |
---|
103 | inline int getMaxSpawnedItems(void) const |
---|
104 | { return this->maxSpawnedItems_; } |
---|
105 | /** |
---|
106 | @brief Returns the name of the template which is used to create a pickup for this spawner. |
---|
107 | */ |
---|
108 | inline const std::string& getPickupTemplateName() const |
---|
109 | { return this->pickupTemplateName_; } |
---|
110 | /** |
---|
111 | @brief Returns the template which is used to create a pickup for this spawner. |
---|
112 | */ |
---|
113 | inline Template* getPickupTemplate() const |
---|
114 | {return this->pickupTemplate_; } |
---|
115 | |
---|
116 | |
---|
117 | void setPickupTemplateName(const std::string& name); |
---|
118 | void setPickupTemplate(Template* temp); |
---|
119 | |
---|
120 | |
---|
121 | /** |
---|
122 | @brief Set the distance in which to trigger. |
---|
123 | @param value The new distance in which to trigger. |
---|
124 | */ |
---|
125 | inline void setTriggerDistance(float value) |
---|
126 | { this->triggerDistance_ = value; } |
---|
127 | /** |
---|
128 | @brief Set the time to respawn. |
---|
129 | @param time New time after which this gets re-actived. |
---|
130 | */ |
---|
131 | inline void setRespawnTime(float time) |
---|
132 | { this->respawnTime_ = time; } |
---|
133 | |
---|
134 | void setMaxSpawnedItems(int items); //!< Sets the maximum number of spawned items. |
---|
135 | |
---|
136 | private: |
---|
137 | void initialize(void); |
---|
138 | |
---|
139 | void trigger(PickupCarrier* carrier); //!< Method called when a carrier is close enough. |
---|
140 | void respawnTimerCallback(); //!< Method called when the timer runs out. |
---|
141 | |
---|
142 | void decrementSpawnsRemaining(void); //!< Decrements the number of remaining spawns. |
---|
143 | void startRespawnTimer(void); //!< Invoked by the timer, re-activates the PickupSpawner. |
---|
144 | |
---|
145 | /** |
---|
146 | @brief Helper method. Adds a PickupCarrier to the list of PickupCarrier that are blocked form getting a Pickupable from the PickupSpawner for a specified time. |
---|
147 | @param carrier A pointer to the PickupCarrier to be blocked. |
---|
148 | @param time The time for which the Pawn is blocked. Default is 5. |
---|
149 | */ |
---|
150 | void block(PickupCarrier* carrier, unsigned int time = DEFAULT_BLOCKED_TIME) |
---|
151 | { this->blocked_.insert(std::pair<PickupCarrier*, std::time_t>(carrier, std::time(0)+time)); } |
---|
152 | |
---|
153 | |
---|
154 | |
---|
155 | |
---|
156 | |
---|
157 | Pickupable* createPickup(void); //!< Creates a new Pickupable. |
---|
158 | void setPickupable(Pickupable* pickup); //!< Sets a Pickupable for the PickupSpawner to spawn. |
---|
159 | |
---|
160 | Pickupable* pickup_; //!< The pickup to be spawned. |
---|
161 | StaticEntity* representation_; //!< The active representation of the spawner. |
---|
162 | std::string pickupTemplateName_; //!< The name of the pickup template. |
---|
163 | Template* pickupTemplate_; //!< The template to be used to create a pickupable. |
---|
164 | |
---|
165 | int maxSpawnedItems_; //!< Maximum number of items spawned by this PickupSpawner. |
---|
166 | int spawnsRemaining_; //!< Number of items that can be spawned by this PickupSpawner until it selfdestructs. |
---|
167 | |
---|
168 | float triggerDistance_; //!< Distance in which this gets triggered. |
---|
169 | |
---|
170 | float respawnTime_; //!< Time after which this gets re-actived. |
---|
171 | Timer respawnTimer_; //!< Timer used for re-activating. |
---|
172 | std::map<PickupCarrier*, std::time_t> blocked_; |
---|
173 | |
---|
174 | bool selfDestruct_; //!< True if the PickupSpawner is selfdestructing. |
---|
175 | |
---|
176 | static const int INF = -1; //!< Constant for infinity. |
---|
177 | static const unsigned int DEFAULT_BLOCKED_TIME = 5; //!< The default time a PickupCarrier is blocked after picking up a Pickupable. |
---|
178 | }; |
---|
179 | } |
---|
180 | |
---|
181 | #endif /* _PickupSpawner_H__ */ |
---|