Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/SOBv2_HS17/src/modules/superorxobros/SOBFigure.cc @ 11491

Last change on this file since 11491 was 11491, checked in by varxth, 7 years ago

improved power up of player

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