Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/SuperOrxoBros_FS17/src/modules/superorxobros/SOBFigure.cc @ 11416

Last change on this file since 11416 was 11416, checked in by jkindle, 7 years ago

Added the animation for the level end (walk into castle), Coins for the blocks, Castleblocks and Flagblocks

File size: 9.6 KB
Line 
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 *      Julien Kindle
24 *   Co-authors:
25 *     
26 *
27 */
28
29/**
30    @file SOBFigure.cc
31    @brief This class represents your figure when you play the minigame. Here the movement of the figure, activating items, ... are handled.
32*/
33
34#include "SOBFigure.h"
35
36#include "core/CoreIncludes.h"
37#include "core/XMLPort.h"
38#include "graphics/Model.h"
39#include "graphics/Camera.h"
40#include "graphics/ParticleSpawner.h"
41
42#include "SOBMushroom.h"
43#include "SOBGumba.h"
44#include "SOB.h"
45#include "SOBFlagstone.h"
46#include "SOBCastlestone.h"
47
48namespace orxonox
49{
50    RegisterClass(SOBFigure);
51
52    SOBFigure::SOBFigure(Context* context) : ControllableEntity(context)
53    {
54        RegisterObject(SOBFigure);
55
56        // initialize variables
57        gravityAcceleration_ = 350.0;
58
59        //Vars for movement of player
60        moveUpPressed_ = false;
61        moveDownPressed_ = false;
62        moveLeftPressed_ = false;
63        moveDownPressed_ = false;
64        firePressed_ = false;
65       
66        //Times and turning
67        timeSinceLastFire_ = 0.0;
68        lastSpeed_z = 0.0;
69        pitch_ = 0.0;
70        timeCounter_ = 0;
71
72        //Properties of player
73        gotPowerUp_ = false;
74        isColliding_ = true;
75        particlespawner_ = NULL;
76
77        //Properties of players life
78        predead_ = false;
79        dead_ = false;
80        lvlEnded_ = false;
81        reachedLvlEndState_ = 0;
82
83       
84        setAngularFactor(0.0); //Means player doesn't turn on collision, so he doesn't fall over while walking over the ground
85        this->enableCollisionCallback(); // Turns on that on every collision function collidesAgainst is executed
86    }
87
88
89
90    bool SOBFigure::collidesAgainst(WorldEntity* otherObject, const btCollisionShape* ownCollisionShape, btManifoldPoint& contactPoint) {
91
92        isColliding_ = true;
93
94        //Orxocast returns object with casted type if otherObject has that class, and if not a nullptr
95        SOBMushroom* mush = orxonox_cast<SOBMushroom*>(otherObject);
96        SOBGumba* gumba = orxonox_cast<SOBGumba*>(otherObject);
97        SOBFlagstone* flagstone = orxonox_cast<SOBFlagstone*>(otherObject);
98        SOBCastlestone* castlestone = orxonox_cast<SOBCastlestone*>(otherObject);
99
100        //Check if otherObject is a powerup
101        if (mush != nullptr && !(mush->hasCollided_)) {
102            otherObject->destroyLater();
103            gotPowerUp_ = true;
104            SOB* SOBGame = orxonox_cast<SOB*>(getGametype()); //Get the Gametype
105            SOBGame->addMushroom(); // Tell the gametype to increase points
106            mush->hasCollided_ = true; // needed because of destroyLater takes some time and player should receive points only once
107
108        //Check if otherObject is a Gumba (that walking enemies)
109        } else if (gumba != nullptr && !(gumba->hasCollided_)) {
110
111            //If player jumps on its head, kill it, else, kill the player
112            if (getVelocity().z >= -20) {
113              Vector3 vel = getVelocity();
114              vel.y = -80;
115              vel.z = 200;
116              setVelocity(vel);
117              predead_=true; 
118
119          } else {
120            gumba->destroyLater();
121            gumba->hasCollided_ = true;
122            SOB* SOBGame = orxonox_cast<SOB*>(getGametype());
123            SOBGame->addGumba();
124
125
126        }
127    }
128
129    //Purpose is that if player hits the flag, he should walk into the castle at the end of the level. For that we use SOBCastlestone
130    if (reachedLvlEndState_ == 0 && flagstone != nullptr && !(flagstone->hasCollided_)) {
131        flagstone->hasCollided_ = true;
132        reachedLvlEndState_ = 1;
133        SOB* SOBGame = orxonox_cast<SOB*>(getGametype());
134        SOBGame->addPoints(flagstone->getPoints());
135
136    }
137    if (castlestone != nullptr && !(castlestone->hasCollided_)) {
138        castlestone->hasCollided_ = true;
139        reachedLvlEndState_++;
140
141    }
142
143    return true;
144}
145
146
147//Self implemented sign function that returns either 1 or -1 (and never 0)
148int SOBFigure::sgn(float x) {
149    if (x < 0.0) return -1;
150    return 1;
151}
152
153//For those of you who don't have an idea: the tick function is called about 50 times/sec
154void SOBFigure::tick(float dt)
155{
156    SUPER(SOBFigure, tick, dt);
157
158
159    bool inputAllowed = true;
160
161    //the particle spawner that generates the fire from the backpack when pressed
162    if (particlespawner_ == NULL) {
163        for (WorldEntity* object : this->getAttachedObjects())
164        {
165           if (object->isA(Class(ParticleSpawner)))
166            particlespawner_ = object;
167        }
168    }
169
170
171    //Behavior on level end - this is like described above for the movement from the player when hit the flag. He moves then into the castle
172    if (reachedLvlEndState_ != 0) {
173        timeCounter_+= dt;
174        inputAllowed = false;
175    }
176    if (reachedLvlEndState_ == 1 && timeCounter_ >= 1.5) {
177        timeCounter_ = 0;
178        reachedLvlEndState_ = 2;
179    }
180
181
182    //if input blocked, then cancel every movement operation
183    if (!inputAllowed) {
184        moveUpPressed_ = false;
185        moveDownPressed_ = false;
186        moveLeftPressed_ = false;
187        moveRightPressed_ = false;
188    }
189
190    //set the gravityto standard 350
191    if (firePressed_ == false) {
192        gravityAcceleration_ = 350.0;
193
194    }
195
196    if (hasLocalController())
197    {
198        Vector3 velocity = getVelocity();
199        Vector3 position = getPosition();
200
201        if (!predead_)
202            velocity.y = 0;
203        if (position.z < -100) {
204            dead_ = true;
205        }
206
207
208        if (dead_) {
209            velocity.x = 0;
210            velocity.z = 0;
211            setVelocity(velocity);
212            SOB* SOBGame = orxonox_cast<SOB*>(getGametype());
213            if (firePressed_)
214                SOBGame->restart();
215            return;
216        }
217
218
219        int maxvelocity_x = 100;
220        int speedAddedPerTick = 5;
221        int camMaxOffset = 25;
222
223        timeSinceLastFire_ += dt;
224        lastSpeed_z = velocity.z;
225
226
227
228        //Handle the rocket fire from the jetpack
229        if (velocity.z > 40)
230            particlespawner_->setVisible(true); 
231        else
232            particlespawner_->setVisible(false); 
233
234
235        //If player hits space and does not move in z-dir
236        if (inputAllowed && firePressed_ && isColliding_ && std::abs(velocity.z) < 0.1 && std::abs(lastSpeed_z) < 0.1) {
237            gravityAcceleration_ = 100.0;
238            velocity.z = 110; //150
239        }
240
241
242        //Left-right movement with acceleration
243        float rot = getOrientation().getRoll().valueDegrees();
244        if (moveRightPressed_) {
245            if (!(rot < 5.0 && -5.0 < rot))
246                setOrientation(Vector3::UNIT_Z, getOrientation().getRoll() - sgn(rot)*dt*Radian(6));
247
248            if (std::abs(velocity.x) < maxvelocity_x) {
249                velocity.x += speedAddedPerTick;
250
251            }
252        } else if (moveLeftPressed_) {
253            if (!(abs(rot) > 175.0 ))
254                setOrientation(Vector3::UNIT_Z, getOrientation().getRoll() + sgn(rot)*dt*Radian(6));
255
256
257
258            if (std::abs(velocity.x) < maxvelocity_x) {
259                velocity.x -= speedAddedPerTick;
260            }
261        } else {
262            velocity.x /= 1.1;
263        }
264
265
266        //Again another EndOfLevel behavior
267        if (reachedLvlEndState_ == 1)
268            velocity.x = -2;
269        if (reachedLvlEndState_ == 2)
270            velocity.x = 30;
271        if (reachedLvlEndState_ == 3) {
272            velocity.x = 0;
273            velocity.y = 20;
274        }
275        if (reachedLvlEndState_ == 4) {
276            lvlEnded_ = true;
277            dead_ = true;
278        }
279
280        //velocity = acc. * time
281        velocity.z -= gravityAcceleration_*dt;
282        setVelocity(velocity);
283
284
285        //Camera operation - the camera should always follow the player in a specific region
286        Camera* cam = getCamera();
287        Vector3 campos = cam->getPosition();
288
289        if (campos.x + camMaxOffset < position.x) {
290            campos.x = position.x - camMaxOffset;
291            cam->setPosition(campos);
292        }
293        if (campos.x - camMaxOffset > position.x) {
294            campos.x = position.x + camMaxOffset;
295            cam->setPosition(campos);
296        }
297
298
299
300
301    }
302
303
304
305    // Reset key variables
306    moveUpPressed_ = false;
307    moveDownPressed_ = false;
308    moveLeftPressed_ = false;
309    moveRightPressed_ = false;
310
311    isColliding_ = false;
312
313}
314
315
316
317
318
319//The following functions read the input of the player and then set the bools for the movement
320void SOBFigure::moveFrontBack(const Vector2& value)
321{
322    if (value.x > 0)
323    {
324        moveUpPressed_ = true;
325        moveDownPressed_ = false;
326    }
327    else
328    {
329        moveUpPressed_ = false;
330        moveDownPressed_ = true;
331    }
332}
333
334void SOBFigure::moveRightLeft(const Vector2& value)
335{
336    if (value.x > 0)
337    {
338        moveLeftPressed_ = false;
339        moveRightPressed_ = true;
340    }
341    else
342    {
343        moveLeftPressed_ = true;
344        moveRightPressed_ = false;
345    }
346}
347
348void SOBFigure::boost(bool boost)
349{
350    firePressed_ = boost;
351}
352
353
354}
Note: See TracBrowser for help on using the repository browser.