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 | * Florian Zinggeler |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file InvaderShip.cc |
---|
31 | @brief Implementation of the InvaderShip class. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "InvaderShip.h" |
---|
35 | |
---|
36 | #include "core/CoreIncludes.h" |
---|
37 | #include "core/XMLPort.h" |
---|
38 | #include "Invader.h" |
---|
39 | #include "InvaderEnemy.h" |
---|
40 | #include "graphics/Camera.h" |
---|
41 | #include "weapons/projectiles/Projectile.h" |
---|
42 | |
---|
43 | namespace orxonox |
---|
44 | { |
---|
45 | RegisterClass(InvaderShip); |
---|
46 | |
---|
47 | InvaderShip::InvaderShip(Context* context) : SpaceShip(context) |
---|
48 | { |
---|
49 | RegisterObject(InvaderShip); |
---|
50 | |
---|
51 | speed = 500; |
---|
52 | isFireing = false; |
---|
53 | damping = 10; |
---|
54 | } |
---|
55 | |
---|
56 | void InvaderShip::tick(float dt) |
---|
57 | { |
---|
58 | Vector3 pos = getPosition(); |
---|
59 | |
---|
60 | //Movement calculation |
---|
61 | lastTimeFront += dt * damping; |
---|
62 | lastTimeLeft += dt * damping; |
---|
63 | lastTime += dt; |
---|
64 | |
---|
65 | velocity.x = interpolate(clamp(lastTimeLeft, 0.0f, 1.0f), desiredVelocity.x, 0.0f); |
---|
66 | velocity.y = interpolate(clamp(lastTimeFront, 0.0f, 1.0f), desiredVelocity.y, 0.0f); |
---|
67 | |
---|
68 | //Execute movement |
---|
69 | if (this->hasLocalController()) |
---|
70 | { |
---|
71 | float dist_y = velocity.y * dt; |
---|
72 | float dist_x = velocity.x * dt; |
---|
73 | if(dist_y + posforeward > -42*3 && dist_y + posforeward < 42*6) |
---|
74 | posforeward += dist_y; |
---|
75 | else |
---|
76 | { |
---|
77 | velocity.y = 0; |
---|
78 | // restart if game ended |
---|
79 | if (getGame()) |
---|
80 | if (getGame()->bEndGame) |
---|
81 | { |
---|
82 | getGame()->start(); |
---|
83 | return; |
---|
84 | } |
---|
85 | } |
---|
86 | if (pos.z + dist_x > 42*2.5 || pos.z + dist_x < -42*3) |
---|
87 | velocity.x = 0; |
---|
88 | pos += Vector3(1000 + velocity.y, 0, velocity.x) * dt; |
---|
89 | } |
---|
90 | |
---|
91 | // shoot! |
---|
92 | if (isFireing) |
---|
93 | ControllableEntity::fire(0); |
---|
94 | |
---|
95 | // Camera |
---|
96 | Camera* camera = this->getCamera(); |
---|
97 | if (camera != nullptr) |
---|
98 | { |
---|
99 | camera->setPosition(Vector3(-pos.z, -posforeward, 0)); |
---|
100 | camera->setOrientation(Vector3::UNIT_Z, Degree(90)); |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | // bring back on track! |
---|
106 | if(pos.y != 0) |
---|
107 | pos.y = 0; |
---|
108 | |
---|
109 | setPosition(pos); |
---|
110 | setOrientation(Vector3::UNIT_Y, Degree(270)); |
---|
111 | |
---|
112 | // Level up! |
---|
113 | if (pos.x > 42000) |
---|
114 | { |
---|
115 | updateLevel(); |
---|
116 | setPosition(Vector3(0, 0, pos.z)); // pos - Vector3(30000, 0, 0) |
---|
117 | } |
---|
118 | |
---|
119 | SUPER(InvaderShip, tick, dt); |
---|
120 | } |
---|
121 | |
---|
122 | void InvaderShip::updateLevel() |
---|
123 | { |
---|
124 | lastTime = 0; |
---|
125 | if (getGame()) |
---|
126 | getGame()->levelUp(); |
---|
127 | } |
---|
128 | |
---|
129 | void InvaderShip::moveFrontBack(const Vector2& value) |
---|
130 | { |
---|
131 | lastTimeLeft = 0; |
---|
132 | desiredVelocity.x = -value.x * speed; |
---|
133 | } |
---|
134 | |
---|
135 | void InvaderShip::moveRightLeft(const Vector2& value) |
---|
136 | { |
---|
137 | lastTimeFront = 0; |
---|
138 | desiredVelocity.y = value.y * speed * 42; |
---|
139 | } |
---|
140 | void InvaderShip::boost(bool bBoost) |
---|
141 | { |
---|
142 | isFireing = bBoost; |
---|
143 | } |
---|
144 | void InvaderShip::rotateRoll(const Vector2& value) |
---|
145 | { |
---|
146 | if (getGame()) |
---|
147 | if (getGame()->bEndGame) |
---|
148 | getGame()->end(); |
---|
149 | } |
---|
150 | inline bool InvaderShip::collidesAgainst(WorldEntity* otherObject, const btCollisionShape* ownCollisionShape, btManifoldPoint& contactPoint) |
---|
151 | { |
---|
152 | // orxout() << "touch!!! " << endl; //<< otherObject << " at " << contactPoint; |
---|
153 | InvaderEnemy* enemy = orxonox_cast<InvaderEnemy*>(otherObject); |
---|
154 | Projectile* shot = orxonox_cast<Projectile*>(otherObject); |
---|
155 | // ensure that this gets only called once per enemy. |
---|
156 | if (enemy != nullptr && lastEnemy != enemy) |
---|
157 | { |
---|
158 | lastEnemy = enemy; |
---|
159 | |
---|
160 | removeHealth(20); |
---|
161 | if (getGame()) |
---|
162 | { |
---|
163 | getGame()->multiplier = 1; |
---|
164 | } |
---|
165 | } |
---|
166 | // was shot, decrease multiplier |
---|
167 | else if (shot != nullptr && lastShot != shot) |
---|
168 | { |
---|
169 | if (getGame() && orxonox_cast<InvaderEnemy*>(shot->getShooter()) != nullptr) |
---|
170 | { |
---|
171 | if (getGame()->multiplier > 1) |
---|
172 | { |
---|
173 | lastShot = shot; |
---|
174 | getGame()->multiplier -= 1; |
---|
175 | } |
---|
176 | } |
---|
177 | } |
---|
178 | return false; |
---|
179 | // SUPER(InvaderShip, collidesAgainst, otherObject, contactPoint); |
---|
180 | } |
---|
181 | |
---|
182 | Invader* InvaderShip::getGame() |
---|
183 | { |
---|
184 | if (game == nullptr) |
---|
185 | { |
---|
186 | for (Invader* invader : ObjectList<Invader>()) |
---|
187 | game = invader; |
---|
188 | } |
---|
189 | return game; |
---|
190 | } |
---|
191 | |
---|
192 | void InvaderShip::death() |
---|
193 | { |
---|
194 | getGame()->costLife(); |
---|
195 | SpaceShip::death(); |
---|
196 | } |
---|
197 | } |
---|