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 | * Fabian 'x3n' Landau |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file ParticleProjectile.h |
---|
31 | @brief Implementation of the ParticleProjectile class. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "BallProjectile.h" |
---|
35 | |
---|
36 | #include <OgreParticleEmitter.h> |
---|
37 | #include "core/CoreIncludes.h" |
---|
38 | #include "tools/ParticleInterface.h" |
---|
39 | #include "Scene.h" |
---|
40 | |
---|
41 | namespace orxonox |
---|
42 | { |
---|
43 | RegisterClass(BallProjectile); |
---|
44 | |
---|
45 | BallProjectile::BallProjectile(Context* context) : Projectile(context) |
---|
46 | { |
---|
47 | RegisterObject(BallProjectile); |
---|
48 | |
---|
49 | this->particles_ = nullptr; |
---|
50 | this->setDestroyAfterCollision(false); //I want the ball to bounce, not to be destroyed |
---|
51 | |
---|
52 | //setEffect("Orxonox/sparks2"); |
---|
53 | } |
---|
54 | |
---|
55 | BallProjectile::~BallProjectile() |
---|
56 | { |
---|
57 | if (this->isInitialized() && this->particles_) |
---|
58 | { |
---|
59 | this->detachOgreObject(this->particles_->getParticleSystem()); |
---|
60 | delete this->particles_; |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | void BallProjectile::Bounce(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs) { |
---|
65 | |
---|
66 | Vector3 velocity = this->getVelocity(); |
---|
67 | Vector3 positionOtherObject = otherObject->getPosition(); |
---|
68 | Vector3 contactPosition = this->getPosition(); |
---|
69 | |
---|
70 | if (positionOtherObject.y < 0) { |
---|
71 | this->destroy(); |
---|
72 | } |
---|
73 | else { |
---|
74 | int distance_X = positionOtherObject.x - contactPosition.x; |
---|
75 | |
---|
76 | if (distance_X < 0) |
---|
77 | distance_X = -distance_X; |
---|
78 | |
---|
79 | int distance_Y = positionOtherObject.y - contactPosition.y; |
---|
80 | |
---|
81 | if (distance_Y < 0) |
---|
82 | distance_Y = -distance_Y; |
---|
83 | |
---|
84 | if (distance_X < distance_Y) |
---|
85 | velocity.x = -velocity.x; |
---|
86 | if (distance_Y < distance_X) |
---|
87 | velocity.y = -velocity.y; |
---|
88 | else { |
---|
89 | velocity.x = -velocity.x; |
---|
90 | velocity.y = -velocity.y; |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | /** |
---|
97 | @brief |
---|
98 | The function called when a projectile hits another thing. |
---|
99 | Calls the hit-function, starts the shield recharge countdown, displays visual hit effects defined in Pawn. |
---|
100 | Needs to be called in the collidesAgainst() function by every Class directly inheriting from BasicProjectile. |
---|
101 | @param otherObject |
---|
102 | A pointer to the object the Projectile has collided against. |
---|
103 | @param contactPoint |
---|
104 | A btManifoldPoint indicating the point of contact/impact. |
---|
105 | @param cs |
---|
106 | The btCollisionShape of the other object |
---|
107 | @return |
---|
108 | Returns true if the collision resulted in a successful hit. |
---|
109 | @see Pawn.h |
---|
110 | */ |
---|
111 | |
---|
112 | |
---|
113 | bool BallProjectile::processCollision(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs) |
---|
114 | { |
---|
115 | if (GameMode::isMaster()) |
---|
116 | { |
---|
117 | |
---|
118 | Pawn* victim = orxonox_cast<Pawn*>(otherObject); // If otherObject isn't a Pawn, then victim is nullptr |
---|
119 | |
---|
120 | WorldEntity* entity = orxonox_cast<WorldEntity*>(this); |
---|
121 | assert(entity); // The projectile must not be a WorldEntity. |
---|
122 | |
---|
123 | // If visual effects after destruction cause problems, put this block below the effects code block |
---|
124 | if (victim) |
---|
125 | { |
---|
126 | victim->hit(this->getShooter(), contactPoint, cs, this->getDamage(), this->getHealthDamage(), this->getShieldDamage()); |
---|
127 | victim->startShieldRechargeCountdown(); |
---|
128 | } |
---|
129 | |
---|
130 | // Visual effects for being hit, depending on whether the shield is hit or not |
---|
131 | if (this->getShooter()) // If the owner does not exist (anymore?), no effects are displayed. |
---|
132 | { |
---|
133 | // Damping and explosion effect is only played if the victim is no Pawn (see cast above) |
---|
134 | // or if the victim is a Pawn, has no shield left, is still alive and any damage goes to the health |
---|
135 | if (!victim || (victim && !victim->hasShield() && victim->getHealth() > 0.0f && (this->getDamage() > 0.0f || this->getHealthDamage() > 0.0f))) |
---|
136 | { |
---|
137 | { |
---|
138 | ParticleSpawner* effect = new ParticleSpawner(this->getShooter()->getContext()); |
---|
139 | effect->setPosition(entity->getPosition()); |
---|
140 | effect->setOrientation(entity->getOrientation()); |
---|
141 | effect->setDestroyAfterLife(true); |
---|
142 | effect->setSource("Orxonox/explosion3"); |
---|
143 | effect->setLifetime(2.0f); |
---|
144 | } |
---|
145 | // Second effect with same condition |
---|
146 | { |
---|
147 | ParticleSpawner* effect = new ParticleSpawner(this->getShooter()->getContext()); |
---|
148 | effect->setPosition(entity->getPosition()); |
---|
149 | effect->setOrientation(entity->getOrientation()); |
---|
150 | effect->setDestroyAfterLife(true); |
---|
151 | effect->setSource("Orxonox/smoke4"); |
---|
152 | effect->setLifetime(3.0f); |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | // victim->isAlive() is not false until the next tick, so getHealth() > 0 is used instead |
---|
157 | if (victim && victim->hasShield() && (this->getDamage() > 0.0f || this->getShieldDamage() > 0.0f) && victim->getHealth() > 0.0f) |
---|
158 | { |
---|
159 | ParticleSpawner* effect = new ParticleSpawner(this->getShooter()->getContext()); |
---|
160 | effect->setDestroyAfterLife(true); |
---|
161 | effect->setSource("Orxonox/Shield"); |
---|
162 | effect->setLifetime(0.5f); |
---|
163 | victim->attach(effect); |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | Bounce(otherObject, contactPoint, cs); |
---|
168 | |
---|
169 | return true; |
---|
170 | } |
---|
171 | return false; |
---|
172 | } |
---|
173 | } |
---|