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 | * Joel Smely |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file SplitGunProjectile.h |
---|
31 | @brief Implementation of the SplitGunProjectile class. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "SplitGunProjectile.h" |
---|
35 | |
---|
36 | #include "core/CoreIncludes.h" |
---|
37 | #include "core/command/Executor.h" |
---|
38 | #include "util/Convert.h" |
---|
39 | #include "util/Math.h" |
---|
40 | |
---|
41 | namespace orxonox |
---|
42 | { |
---|
43 | RegisterClass(SplitGunProjectile); |
---|
44 | |
---|
45 | SplitGunProjectile::SplitGunProjectile(Context* context) : BillboardProjectile(context) |
---|
46 | { |
---|
47 | RegisterObject(SplitGunProjectile); |
---|
48 | |
---|
49 | this->numberOfSplits_ = 0; |
---|
50 | this->numberOfChilds_ = 0; |
---|
51 | this->splitTime_ = 1.0; |
---|
52 | this->spread_ = 0.2; |
---|
53 | } |
---|
54 | |
---|
55 | void SplitGunProjectile::setNumberOfSplits(int numberOfSplits) |
---|
56 | { |
---|
57 | if (numberOfSplits >= 0) |
---|
58 | { |
---|
59 | this->numberOfSplits_ = numberOfSplits; |
---|
60 | } |
---|
61 | else |
---|
62 | { |
---|
63 | this->numberOfSplits_ = 0; |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | void SplitGunProjectile::setNumberOfChilds(int numberOfChilds) |
---|
68 | { |
---|
69 | if (numberOfChilds >= 0) |
---|
70 | { |
---|
71 | this->numberOfChilds_ = numberOfChilds; |
---|
72 | } |
---|
73 | else |
---|
74 | { |
---|
75 | this->numberOfChilds_ = 0; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | void SplitGunProjectile::setSplitTime(float splitTime) |
---|
80 | { |
---|
81 | if (splitTime >= 0) |
---|
82 | { |
---|
83 | this->splitTime_ = splitTime; |
---|
84 | this->textureTimer_.setTimer(this->splitTime_, false, createExecutor(createFunctor(&SplitGunProjectile::split, this))); |
---|
85 | } |
---|
86 | else |
---|
87 | { |
---|
88 | this->splitTime_ = 0; |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | void SplitGunProjectile::setSpread(float spread) |
---|
93 | { |
---|
94 | spread_ = spread; |
---|
95 | } |
---|
96 | |
---|
97 | void SplitGunProjectile::split() |
---|
98 | { |
---|
99 | if (numberOfSplits_ > 0) |
---|
100 | { |
---|
101 | -- numberOfSplits_; |
---|
102 | |
---|
103 | // Calculate a normalized vector (velocityOffset) that is perpendicluar to the velocity of this projectile. Since there are infinitly many perpendicular vectors a random one is chosen. |
---|
104 | Vector3 velocityInitial = this->getVelocity(); |
---|
105 | Vector3 velocityOffset = velocityInitial.perpendicular(); |
---|
106 | velocityOffset.normalise(); |
---|
107 | Degree angle = Degree(rnd(0,360)); |
---|
108 | velocityOffset = Quaternion(angle, velocityInitial.normalisedCopy()) * velocityOffset; |
---|
109 | velocityOffset.normalise(); |
---|
110 | |
---|
111 | // Create as many childs as defined by numberOfChilds_ |
---|
112 | for (int i = 0; i < numberOfChilds_; ++i) |
---|
113 | { |
---|
114 | // Every child projectile should fly uniform in different directions -> Rotate the velocityOffset in every iteration |
---|
115 | velocityOffset = Quaternion(Degree(360/numberOfChilds_), velocityInitial.normalisedCopy()) * velocityOffset; |
---|
116 | velocityOffset.normalise(); |
---|
117 | |
---|
118 | SplitGunProjectile* projectile = new SplitGunProjectile(this->getContext()); |
---|
119 | |
---|
120 | projectile->setMaterial(this->getMaterial()); |
---|
121 | projectile->setOrientation(this->getOrientation()); |
---|
122 | projectile->setPosition(this->getPosition()); |
---|
123 | projectile->setVelocity(velocityInitial + spread_*velocityOffset*velocityInitial.length()); |
---|
124 | |
---|
125 | projectile->setNumberOfSplits(this->numberOfSplits_); |
---|
126 | projectile->setNumberOfChilds(this->numberOfChilds_); |
---|
127 | projectile->setSplitTime(this->splitTime_); |
---|
128 | projectile->setSpread(this->spread_); |
---|
129 | |
---|
130 | projectile->setShooter(this->getShooter()); |
---|
131 | projectile->setDamage(this->getDamage()); |
---|
132 | projectile->setShieldDamage(this->getShieldDamage()); |
---|
133 | projectile->setHealthDamage(this->getHealthDamage()); |
---|
134 | } |
---|
135 | |
---|
136 | numberOfSplits_ = 0; |
---|
137 | } |
---|
138 | } |
---|
139 | } |
---|