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 | * Maurus Kaufmann |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "SpaceBoundaries.h" |
---|
30 | |
---|
31 | #include <OgreBillboardSet.h> |
---|
32 | |
---|
33 | #include "core/CoreIncludes.h" |
---|
34 | #include "core/object/ObjectListIterator.h" |
---|
35 | #include "core/XMLPort.h" |
---|
36 | |
---|
37 | #include "graphics/Billboard.h" |
---|
38 | #include "infos/PlayerInfo.h" |
---|
39 | #include "worldentities/WorldEntity.h" |
---|
40 | #include "worldentities/pawns/Pawn.h" |
---|
41 | |
---|
42 | namespace orxonox |
---|
43 | { |
---|
44 | RegisterClass(SpaceBoundaries); |
---|
45 | |
---|
46 | SpaceBoundaries::SpaceBoundaries(Context* context) : StaticEntity(context) |
---|
47 | { |
---|
48 | RegisterObject(SpaceBoundaries); |
---|
49 | |
---|
50 | this->setMaxDistance(3000); |
---|
51 | this->setWarnDistance(this->getMaxDistance()); |
---|
52 | this->setShowDistance(this->getMaxDistance()); |
---|
53 | this->setReaction(0); |
---|
54 | } |
---|
55 | SpaceBoundaries::~SpaceBoundaries() |
---|
56 | { |
---|
57 | if (this->isInitialized()) |
---|
58 | { |
---|
59 | this->pawnsIn_.clear(); |
---|
60 | |
---|
61 | for(BillboardAdministration& billboard : this->billboards_) |
---|
62 | { |
---|
63 | if( billboard.billy != nullptr) |
---|
64 | { |
---|
65 | delete billboard.billy; |
---|
66 | } |
---|
67 | } |
---|
68 | this->billboards_.clear(); |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | void SpaceBoundaries::checkWhoIsIn() |
---|
73 | { |
---|
74 | pawnsIn_.clear(); |
---|
75 | for(Pawn* currentPawn : ObjectList<Pawn>()) |
---|
76 | { |
---|
77 | if( this->reaction_ == 0 ) |
---|
78 | { |
---|
79 | float distance = this->computeDistance(currentPawn); |
---|
80 | if(distance <= this->maxDistance_) |
---|
81 | { |
---|
82 | pawnsIn_.push_back(currentPawn); |
---|
83 | } |
---|
84 | } else if (this->reaction_ == 2) { |
---|
85 | float distance = this->computeDistance(currentPawn); |
---|
86 | if(distance >= this->maxDistance_) |
---|
87 | { |
---|
88 | pawnsIn_.push_back(currentPawn); |
---|
89 | } |
---|
90 | } else { |
---|
91 | pawnsIn_.push_back(currentPawn); |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | void SpaceBoundaries::positionBillboard(const Vector3& position, float alpha) |
---|
97 | { |
---|
98 | size_t current; |
---|
99 | for (current = 0; current < this->billboards_.size(); ++current) |
---|
100 | if (!this->billboards_[current].usedYet) |
---|
101 | break; |
---|
102 | |
---|
103 | if (current == this->billboards_.size()) |
---|
104 | { |
---|
105 | Billboard* billboard = new Billboard(this->getContext()); |
---|
106 | billboard->setPosition(position); |
---|
107 | billboard->setSyncMode(ObjectDirection::None); |
---|
108 | this->setBillboardOptions(billboard); |
---|
109 | BillboardAdministration ba = {true, billboard}; |
---|
110 | this->billboards_.push_back(ba); |
---|
111 | } |
---|
112 | |
---|
113 | this->billboards_[current].billy->setPosition(position); |
---|
114 | this->billboards_[current].billy->setVisible(true); |
---|
115 | this->billboards_[current].billy->setColour(ColourValue(1, 1, 1, alpha)); |
---|
116 | this->billboards_[current].usedYet = true; |
---|
117 | |
---|
118 | Vector3 directionVector = (this->getWorldPosition() - position).normalisedCopy(); // vector from the position of the billboard to the center of the sphere |
---|
119 | this->billboards_[current].billy->setCommonDirection(directionVector); |
---|
120 | |
---|
121 | Vector3 upVector = Vector3(directionVector.z, directionVector.z, -(directionVector.x + directionVector.y)); // vector perpendicular to the direction vector |
---|
122 | upVector.normalise(); |
---|
123 | this->billboards_[current].billy->setCommonUpVector(upVector); |
---|
124 | } |
---|
125 | |
---|
126 | void SpaceBoundaries::setBillboardOptions(Billboard *billy) |
---|
127 | { |
---|
128 | if(billy != nullptr) |
---|
129 | { |
---|
130 | billy->setMaterial("Grid"); |
---|
131 | billy->setBillboardType(Ogre::BBT_PERPENDICULAR_COMMON); |
---|
132 | billy->setDefaultDimensions(150, 150); |
---|
133 | billy->setVisible(true); |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | void SpaceBoundaries::removeAllBillboards() |
---|
138 | { |
---|
139 | for(BillboardAdministration& billboard : this->billboards_) |
---|
140 | { |
---|
141 | billboard.usedYet = false; |
---|
142 | billboard.billy->setVisible(false); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | void SpaceBoundaries::setMaxDistance(float r) |
---|
147 | { |
---|
148 | this->maxDistance_ = r; |
---|
149 | } |
---|
150 | float SpaceBoundaries::getMaxDistance() |
---|
151 | { |
---|
152 | return this->maxDistance_; |
---|
153 | } |
---|
154 | |
---|
155 | void SpaceBoundaries::setWarnDistance(float r) |
---|
156 | { |
---|
157 | this->warnDistance_ = r; |
---|
158 | } |
---|
159 | float SpaceBoundaries::getWarnDistance() |
---|
160 | { |
---|
161 | return this->warnDistance_; |
---|
162 | } |
---|
163 | |
---|
164 | void SpaceBoundaries::setShowDistance(float r) |
---|
165 | { |
---|
166 | this->showDistance_ = r; |
---|
167 | } |
---|
168 | float SpaceBoundaries::getShowDistance() |
---|
169 | { |
---|
170 | return this->showDistance_; |
---|
171 | } |
---|
172 | |
---|
173 | void SpaceBoundaries::setHealthDecrease(float amount) |
---|
174 | { |
---|
175 | this->healthDecrease_ = amount/1000; |
---|
176 | } |
---|
177 | float SpaceBoundaries::getHealthDecrease() |
---|
178 | { |
---|
179 | return this->healthDecrease_; |
---|
180 | } |
---|
181 | |
---|
182 | void SpaceBoundaries::setReaction(int mode) |
---|
183 | { |
---|
184 | this->reaction_ = mode; |
---|
185 | } |
---|
186 | int SpaceBoundaries::getReaction() |
---|
187 | { |
---|
188 | return this->reaction_; |
---|
189 | } |
---|
190 | |
---|
191 | void SpaceBoundaries::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
192 | { |
---|
193 | SUPER(SpaceBoundaries, XMLPort, xmlelement, mode); |
---|
194 | |
---|
195 | XMLPortParam(SpaceBoundaries, "maxDistance", setMaxDistance, getMaxDistance, xmlelement, mode); |
---|
196 | XMLPortParam(SpaceBoundaries, "warnDistance", setWarnDistance, getWarnDistance, xmlelement, mode); |
---|
197 | XMLPortParam(SpaceBoundaries, "showDistance", setShowDistance, getShowDistance, xmlelement, mode); |
---|
198 | XMLPortParam(SpaceBoundaries, "healthDecrease", setHealthDecrease, getHealthDecrease, xmlelement, mode); |
---|
199 | XMLPortParam(SpaceBoundaries, "reactionMode", setReaction, getReaction, xmlelement, mode); |
---|
200 | } |
---|
201 | |
---|
202 | void SpaceBoundaries::tick(float dt) |
---|
203 | { |
---|
204 | this->checkWhoIsIn(); |
---|
205 | this->removeAllBillboards(); |
---|
206 | |
---|
207 | float distance; |
---|
208 | bool humanItem; |
---|
209 | for(Pawn* currentPawn : pawnsIn_) |
---|
210 | { |
---|
211 | if( currentPawn && currentPawn->getNode() ) |
---|
212 | { |
---|
213 | distance = this->computeDistance(currentPawn); |
---|
214 | humanItem = this->isHumanPlayer(currentPawn); |
---|
215 | // orxout() << "Distance:" << distance << endl; // message for debugging |
---|
216 | if(distance > this->warnDistance_ && distance < this->maxDistance_) // Display warning |
---|
217 | { |
---|
218 | if(humanItem) |
---|
219 | { |
---|
220 | this->displayWarning("Attention! You are close to the boundary!"); |
---|
221 | } |
---|
222 | } |
---|
223 | if(/* humanItem &&*/ std::abs(this->maxDistance_ - distance) < this->showDistance_ ) |
---|
224 | { |
---|
225 | this->displayBoundaries(currentPawn, 1.0f - fabs(this->maxDistance_ - distance) / this->showDistance_); // Show the boundary |
---|
226 | } |
---|
227 | if(distance > this->maxDistance_ && (this->reaction_ == 1) ) |
---|
228 | { |
---|
229 | if( humanItem ) |
---|
230 | { |
---|
231 | // orxout() << "Health should be decreasing!" << endl; |
---|
232 | this->displayWarning("You are out of the area now!"); |
---|
233 | } |
---|
234 | currentPawn->removeHealth( (distance - this->maxDistance_) * this->healthDecrease_); |
---|
235 | } |
---|
236 | if( (this->reaction_ == 0) && (distance + 100 > this->maxDistance_)) // Exception: A Pawn can't move more than 100 units per tick. |
---|
237 | { |
---|
238 | this->conditionalBounceBack(currentPawn, distance, dt); |
---|
239 | } |
---|
240 | if( this->reaction_ == 2 && (distance - 100 < this->maxDistance_) ) |
---|
241 | { |
---|
242 | this->conditionalBounceBack(currentPawn, distance, dt); |
---|
243 | } |
---|
244 | } |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | float SpaceBoundaries::computeDistance(WorldEntity *item) |
---|
249 | { |
---|
250 | if(item != nullptr) |
---|
251 | { |
---|
252 | Vector3 itemPosition = item->getWorldPosition(); |
---|
253 | return (itemPosition.distance(this->getWorldPosition())); |
---|
254 | } else { |
---|
255 | return -1; |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | void SpaceBoundaries::displayWarning(const std::string warnText) |
---|
260 | { |
---|
261 | // TODO |
---|
262 | } |
---|
263 | |
---|
264 | void SpaceBoundaries::displayBoundaries(Pawn *item, float alpha) |
---|
265 | { |
---|
266 | |
---|
267 | Vector3 direction = item->getWorldPosition() - this->getWorldPosition(); |
---|
268 | direction.normalise(); |
---|
269 | |
---|
270 | Vector3 boundaryPosition = this->getWorldPosition() + direction * this->maxDistance_; |
---|
271 | |
---|
272 | this->positionBillboard(boundaryPosition, alpha); |
---|
273 | } |
---|
274 | |
---|
275 | void SpaceBoundaries::conditionalBounceBack(Pawn *item, float currentDistance, float dt) |
---|
276 | { |
---|
277 | Vector3 normal = item->getWorldPosition() - this->getWorldPosition(); |
---|
278 | normal.normalise(); |
---|
279 | Vector3 velocity = item->getVelocity(); |
---|
280 | float normalSpeed = item->getVelocity().dotProduct(normal); |
---|
281 | |
---|
282 | /* Check, whether the Pawn would leave the boundary in the next tick, if so send it back. */ |
---|
283 | if( this->reaction_ == 0 && currentDistance + normalSpeed * dt > this->maxDistance_ - 10 ) // -10: "security measure" |
---|
284 | { |
---|
285 | bounceBack(item, &normal, &velocity); |
---|
286 | } else if (this->reaction_ == 2 && currentDistance - normalSpeed * dt < this->maxDistance_ + 10 ) // 10: "security measure" |
---|
287 | { |
---|
288 | normal = normal * (-1); |
---|
289 | bounceBack(item, &normal, &velocity); |
---|
290 | } |
---|
291 | } |
---|
292 | |
---|
293 | void SpaceBoundaries::bounceBack(Pawn *item, Vector3 *normal, Vector3 *velocity) |
---|
294 | { |
---|
295 | float dampingFactor = 0.5; |
---|
296 | *velocity = velocity->reflect(*normal); |
---|
297 | Vector3 acceleration = item->getAcceleration(); |
---|
298 | acceleration = acceleration.reflect(*normal); |
---|
299 | |
---|
300 | item->lookAt( *velocity + this->getWorldPosition() ); |
---|
301 | |
---|
302 | item->setAcceleration(acceleration * dampingFactor); |
---|
303 | item->setVelocity(*velocity * dampingFactor); |
---|
304 | |
---|
305 | item->setPosition( item->getWorldPosition() - *normal * 10 ); // Set the position of the Pawn to be well inside the boundary. |
---|
306 | } |
---|
307 | |
---|
308 | bool SpaceBoundaries::isHumanPlayer(Pawn *item) |
---|
309 | { |
---|
310 | if(item != nullptr) |
---|
311 | { |
---|
312 | if(item->getPlayer()) |
---|
313 | { |
---|
314 | return item->getPlayer()->isHumanPlayer(); |
---|
315 | } |
---|
316 | } |
---|
317 | return false; |
---|
318 | } |
---|
319 | |
---|
320 | } |
---|