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 | * FLeo Merholz |
---|
24 | * Pascal Schärli |
---|
25 | * Co-authors: |
---|
26 | * ... |
---|
27 | * |
---|
28 | */ |
---|
29 | |
---|
30 | /** |
---|
31 | @file FlappyOrxShip.cc |
---|
32 | @brief Implementation of the FlappyOrxShip class. |
---|
33 | */ |
---|
34 | |
---|
35 | #include "FlappyOrxShip.h" |
---|
36 | #include "core/CoreIncludes.h" |
---|
37 | #include "core/XMLPort.h" |
---|
38 | #include "graphics/Camera.h" |
---|
39 | #include "graphics/ParticleSpawner.h" |
---|
40 | #include <math.h> |
---|
41 | #include <ctime> |
---|
42 | |
---|
43 | namespace orxonox |
---|
44 | { |
---|
45 | RegisterClass(FlappyOrxShip); |
---|
46 | |
---|
47 | FlappyOrxShip::FlappyOrxShip(Context* context) : SpaceShip(context) |
---|
48 | { |
---|
49 | RegisterObject(FlappyOrxShip); |
---|
50 | |
---|
51 | |
---|
52 | isDead = true; |
---|
53 | deathTime = 0; |
---|
54 | |
---|
55 | particleLifespan = 0.1; |
---|
56 | particleAge = 0; |
---|
57 | |
---|
58 | particlespawner_ = NULL; |
---|
59 | |
---|
60 | } |
---|
61 | |
---|
62 | void FlappyOrxShip::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
63 | { |
---|
64 | SUPER(FlappyOrxShip, XMLPort, xmlelement, mode); |
---|
65 | XMLPortParam(FlappyOrxShip,"speedBase", setSpeedBase, getSpeedBase, xmlelement,mode); |
---|
66 | XMLPortParam(FlappyOrxShip,"speedIncrease", setSpeedIncrease, getSpeedIncrease, xmlelement,mode); |
---|
67 | XMLPortParam(FlappyOrxShip,"tubeDistanceBase", setTubeDistanceBase, getTubeDistanceBase, xmlelement,mode); |
---|
68 | XMLPortParam(FlappyOrxShip,"tubeDistanceIncrease", setTubeDistanceIncrease, getTubeDistanceIncrease, xmlelement,mode); |
---|
69 | |
---|
70 | XMLPortParam(FlappyOrxShip,"upwardThrust", setUpwardthrust, getUpwardthrust, xmlelement,mode); |
---|
71 | XMLPortParam(FlappyOrxShip,"gravity", setGravity, getGravity, xmlelement,mode); |
---|
72 | } |
---|
73 | |
---|
74 | void FlappyOrxShip::tick(float dt) |
---|
75 | { |
---|
76 | SUPER(FlappyOrxShip, tick, dt); |
---|
77 | |
---|
78 | |
---|
79 | particleAge+=dt; |
---|
80 | //the particle spawner that generates the fire from the backpack when pressed |
---|
81 | if (particlespawner_ == NULL) { |
---|
82 | for (WorldEntity* object : this->getAttachedObjects()) |
---|
83 | { |
---|
84 | if (object->isA(Class(ParticleSpawner))) |
---|
85 | particlespawner_ = (ParticleSpawner*) object; |
---|
86 | } |
---|
87 | } |
---|
88 | else if(particleAge>particleLifespan){ |
---|
89 | particlespawner_->setLifetime(1); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | //Execute movement |
---|
94 | if (this->hasLocalController()) |
---|
95 | { |
---|
96 | if(getHealth()<0){ |
---|
97 | setHealth(1); |
---|
98 | getGame()->death(); |
---|
99 | death(); |
---|
100 | } |
---|
101 | Vector3 pos = getPosition(); |
---|
102 | |
---|
103 | getGame()->updatePlayerPos(pos.x); |
---|
104 | |
---|
105 | |
---|
106 | if(not isDead){ |
---|
107 | velocity.y += gravity*dt; |
---|
108 | if(isFlapping){ |
---|
109 | isFlapping = false; |
---|
110 | if(pos.z > -150) |
---|
111 | velocity.y = -upwardThrust; |
---|
112 | } |
---|
113 | |
---|
114 | pos += Vector3(speed + velocity.x, 0, velocity.y) * dt; |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | // Camera |
---|
119 | Camera* camera = this->getCamera(); |
---|
120 | if (camera != nullptr) |
---|
121 | { |
---|
122 | camera->setPosition(pos.x,-100,0); |
---|
123 | camera->setOrientation(Vector3::UNIT_Z, Degree(0)); |
---|
124 | |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | setPosition(pos); |
---|
129 | setOrientation(Vector3::UNIT_Y, Degree(270-velocity.y/10)); |
---|
130 | |
---|
131 | if(pos.z > 150){ |
---|
132 | getGame()->death(); |
---|
133 | death(); |
---|
134 | } |
---|
135 | |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | int FlappyOrxShip::timeUntilRespawn(){ |
---|
140 | return 2-time(0)+deathTime; |
---|
141 | } |
---|
142 | |
---|
143 | void FlappyOrxShip::boost(bool boost){ |
---|
144 | |
---|
145 | if(not isDead){ |
---|
146 | particleAge = 0; |
---|
147 | } |
---|
148 | |
---|
149 | if(isDead&&timeUntilRespawn()<=0){ |
---|
150 | isDead = false; |
---|
151 | getGame()->setDead(false); |
---|
152 | } |
---|
153 | isFlapping=boost; |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | FlappyOrx* FlappyOrxShip::getGame() |
---|
158 | { |
---|
159 | if (game == nullptr) |
---|
160 | { |
---|
161 | for (FlappyOrx* flappyOrx : ObjectList<FlappyOrx>()) |
---|
162 | game = flappyOrx; |
---|
163 | } |
---|
164 | return game; |
---|
165 | } |
---|
166 | |
---|
167 | void FlappyOrxShip::death() |
---|
168 | { |
---|
169 | isDead = true; |
---|
170 | |
---|
171 | deathTime = time(0); |
---|
172 | Vector3 pos = getPosition(); |
---|
173 | pos.x = 0; |
---|
174 | pos.z = 0; |
---|
175 | pos.y = 0; |
---|
176 | velocity.y = 0; |
---|
177 | setPosition(pos); |
---|
178 | usleep(1000u); |
---|
179 | } |
---|
180 | } |
---|