Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11578 was 11578, checked in by varxth, 6 years ago

abgleich

File size: 13.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#include <OgreMath.h>
42
43#include "SOBMushroom.h"
44#include "SOBGumba.h"
45#include "SOBFireball.h"
46#include "SOB.h"
47#include "SOBFlagstone.h"
48#include "SOBCastlestone.h"
49#include "SOBFireball.h"
50#include <BulletCollision/NarrowPhaseCollision/btManifoldPoint.h>
51
52namespace orxonox
53{
54    RegisterClass(SOBFigure);
55
56    SOBFigure::SOBFigure(Context* context) : ControllableEntity(context)
57    {
58        RegisterObject(SOBFigure);
59
60        // initialize variables
61        gravityAcceleration_ = 350.0;
62
63        //Vars for movement of player
64        moveUpPressed_      = false;
65        moveDownPressed_    = false;
66        moveLeftPressed_    = false;
67        moveDownPressed_    = false;
68        firePressed_        = false;
69        collDisZ_           = 0;
70
71        //Times and turning
72        timeSinceLastFire_  = 0.0;
73        lastSpeed_z         = 0.0;
74        pitch_              = 0.0;
75        timeCounter_        = 0;
76
77        //Properties of player
78
79        isColliding_        = true;
80        particlespawner_    = NULL;
81
82        //Properties of players life
83        predead_            = false;
84        dead_               = false;
85        lvlEnded_           = false;
86        reachedLvlEndState_ = 0;
87
88        // Properties concerning PowerUps and items
89        PowerUpCounter_     = 0;
90        maxPowerUp_         = 2;
91        FireballPower       = 2;
92        //Properties of fireing Fireballs, NOTE! fireballs are fired with the moveUP Key, not with the fire key
93        fireallowed_        = true;
94        firecooldown_       = 0;
95
96       
97        setAngularFactor(0.0); //Means player doesn't turn on collision, so he doesn't fall over while walking over the ground
98        this->enableCollisionCallback(); // Turns on that on every collision function collidesAgainst is executed
99    }
100
101
102
103    bool SOBFigure::collidesAgainst(WorldEntity* otherObject, const btCollisionShape* ownCollisionShape, btManifoldPoint& contactPoint) {
104
105        //Inform tick fct that player is colliding and tell him how far away the collision point is from player middle point in z dir
106        isColliding_ = true;
107        collDisZ_ = getPosition().z - contactPoint.getPositionWorldOnB().getZ();
108
109
110        //Orxocast returns object with casted type if otherObject has that class, and if not a nullptr
111        SOBMushroom*    mush        = orxonox_cast<SOBMushroom*>    (otherObject);
112        SOBGumba*       gumba       = orxonox_cast<SOBGumba*>       (otherObject);
113        SOBFlagstone*   flagstone   = orxonox_cast<SOBFlagstone*>   (otherObject);
114        SOBCastlestone* castlestone = orxonox_cast<SOBCastlestone*> (otherObject);
115        SOBFireball* fireball       = orxonox_cast<SOBFireball*>    (otherObject);
116        SOB* SOBGame                = orxonox_cast<SOB*>            (getGametype()); 
117
118
119        //Check if otherObject is a powerup-mushroom
120        if (mush != nullptr && !(mush->hasCollided_)) {
121            otherObject->destroyLater();
122
123            PowerUpCounter_++;
124            if(PowerUpCounter_ > maxPowerUp_)   PowerUpCounter_ = maxPowerUp_; // you had already the max
125            else                                this->changeClothes();
126
127            SOBGame->addMushroom(); // Tell the gametype to increase points
128            mush->hasCollided_ = true; // needed because of destroyLater takes some time and player should receive points only once
129
130           
131           
132        }
133       
134
135        //Check if otherObject is a Gumba (that walking enemies)
136
137         else if (gumba != nullptr && !(gumba->hasCollided_)) {
138
139            //If player jumps on its head, kill the Gumba, else, kill the player
140            if (getVelocity().z >= -20) {
141                // If player hasn't a power up, he dies. Else he shrinks and the gumba dies.
142                if(PowerUpCounter_ == 0){
143                    this->die();
144                } 
145                else{
146                    PowerUpCounter_--;
147                    this->changeClothes();
148
149                    gumba->destroyLater();
150                    gumba->hasCollided_ = true;
151                }
152
153          } else {
154            gumba->destroyLater();
155            gumba->hasCollided_ = true;
156            SOBGame->addGumba();
157
158
159        }
160    }
161
162        else if (fireball != nullptr && !(fireball->hasCollided_)){
163            //this-> die();
164        }
165
166    //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
167    if (reachedLvlEndState_ == 0 && flagstone != nullptr && !(flagstone->hasCollided_)) {
168        flagstone->hasCollided_ = true;
169        reachedLvlEndState_ = 1;
170
171        SOBGame->setDone(true);
172        SOBGame->addPoints(flagstone->getPoints());
173       
174
175    }
176    if (castlestone != nullptr && !(castlestone->hasCollided_)) {
177        castlestone->hasCollided_ = true;
178        reachedLvlEndState_++;
179
180    }
181
182    return true;
183}
184
185
186//Self implemented sign function that returns either 1 or -1 (and never 0)
187int SOBFigure::sgn(float x) {
188    if (x < 0.0) return -1;
189    return 1;
190}
191
192//Function to spawn the Fireball
193void SOBFigure::spawnFireball() {
194        SOBCenterpoint* center_ = ((SOB*)getGametype())->center_;
195
196         SOBFireball* ball = new SOBFireball(center_->getContext());
197         Vector3 spawnpos = this->getWorldPosition();
198         spawnpos.z += 0;
199
200        if (ball != nullptr && center_ != nullptr)
201        {
202            ball->addTemplate("fireball");
203            ball->setPosition(spawnpos);
204            bool direction = ((this->getWorldOrientation().getRoll().valueRadians())>-1.6&&(this->getWorldOrientation().getRoll().valueRadians()<1.6));
205            ball->setDirection(direction);
206            orxout() << "Rotation: " << this->getWorldOrientation().getRoll() << " direction: "<< direction <<endl;
207            //ball->
208
209        }
210     }
211
212//For those of you who don't have an idea: the tick function is called about 50 times/sec
213void SOBFigure::tick(float dt)
214{
215    SUPER(SOBFigure, tick, dt);
216
217
218    bool inputAllowed = true;
219
220    //the particle spawner that generates the fire from the backpack when pressed
221    if (particlespawner_ == NULL) {
222        for (WorldEntity* object : this->getAttachedObjects())
223        {
224           if (object->isA(Class(ParticleSpawner)))
225            particlespawner_ = object;
226        }
227    }
228
229
230    //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
231    if (reachedLvlEndState_ != 0) {
232        timeCounter_+= dt;
233        inputAllowed = false;
234    }
235    if (reachedLvlEndState_ == 1 && timeCounter_ >= 1.5) {
236        timeCounter_ = 0;
237        reachedLvlEndState_ = 2;
238    }
239
240
241    //if input blocked, then cancel every movement operation
242    if (!inputAllowed) {
243        moveUpPressed_      = false;
244        moveDownPressed_    = false;
245        moveLeftPressed_    = false;
246        moveRightPressed_   = false;
247    }
248
249    //set the gravityto standard 350
250    if (firePressed_ == false) {
251        gravityAcceleration_ = 350.0;
252
253    }
254
255    if (hasLocalController())
256    {
257        SOB* SOBGame = orxonox_cast<SOB*>(getGametype());
258        Vector3 velocity = getVelocity();
259        Vector3 position = getPosition();
260
261        if (!predead_)
262            velocity.y = 0;
263        //If player falls in a hole
264        if (position.z < -100) {
265            dead_ = true;
266            SOBGame->setDone(true);
267        }
268
269
270        if (dead_) {
271            velocity.x = 0;
272            velocity.z = 0;
273            setVelocity(velocity);
274           
275            if (firePressed_)
276                SOBGame->restart();
277            return;
278        }
279
280
281        int maxvelocity_x = 100;
282        int speedAddedPerTick = 5;
283        int camMaxOffset = 25;
284
285        timeSinceLastFire_ += dt;
286        lastSpeed_z = velocity.z;
287
288
289
290        //Handle the rocket fire from the jetpack
291        if (velocity.z > 40)
292            particlespawner_->setVisible(true); 
293        else
294            particlespawner_->setVisible(false); 
295
296
297        //If player hits space and collides against an object under him then jump
298        if (inputAllowed && firePressed_ && isColliding_ && (collDisZ_ >= 0 && collDisZ_ <+ 10)) {
299            gravityAcceleration_ = 100.0;
300            velocity.z = 110; 
301        }
302
303
304        //Left-right movement with acceleration and rotation
305        float rot = getOrientation().getRoll().valueDegrees();
306        if (moveRightPressed_) {
307            if (!(rot < 5.0 && -5.0 < rot))
308                setOrientation(Vector3::UNIT_Z, getOrientation().getRoll() - sgn(rot)*dt*Radian(6));
309
310            if (std::abs(velocity.x) < maxvelocity_x) {
311                velocity.x += speedAddedPerTick;
312
313            }
314        } else if (moveLeftPressed_) {
315            if (!(abs(rot) > 175.0 ))
316                setOrientation(Vector3::UNIT_Z, getOrientation().getRoll() + sgn(rot)*dt*Radian(6));
317
318
319
320            if (std::abs(velocity.x) < maxvelocity_x) {
321                velocity.x -= speedAddedPerTick;
322            }
323        } else {
324            velocity.x /= 1.1;
325        }
326
327        //If moveUp pressed, fire a fireball
328        if(moveUpPressed_ && (PowerUpCounter_ >= FireballPower) && fireallowed_)
329        {
330            spawnFireball();
331            fireallowed_  = false;
332            firecooldown_ = 0;
333        }
334
335        //Increase the firecooldown
336        if(firecooldown_> 0.5)
337        {
338            fireallowed_ = true;
339        }
340        if(!fireallowed_)
341        {
342            firecooldown_ += dt;
343        }
344
345        //Again another EndOfLevel behavior
346        if (reachedLvlEndState_ == 1)
347            velocity.x = -2;
348        if (reachedLvlEndState_ == 2)
349            velocity.x = 30;
350        if (reachedLvlEndState_ == 3) {
351            velocity.x = 0;
352            velocity.y = 20;
353        }
354        if (reachedLvlEndState_ == 4) {
355            lvlEnded_ = true;
356            dead_ = true;
357        }
358
359        //velocity = acc. * time
360        velocity.z -= gravityAcceleration_*dt;
361        setVelocity(velocity);
362
363
364        //Camera operation - the camera should always follow the player in a specific region
365        Camera* cam = getCamera();
366        Vector3 campos = cam->getPosition();
367
368        if (campos.x + camMaxOffset < position.x) {
369            campos.x = position.x - camMaxOffset;
370            cam->setPosition(campos);
371        }
372        if (campos.x - camMaxOffset > position.x) {
373            campos.x = position.x + camMaxOffset;
374            cam->setPosition(campos);
375        }
376
377    }
378
379    // Reset key variables
380    moveUpPressed_      = false;
381    moveDownPressed_    = false;
382    moveLeftPressed_    = false;
383    moveRightPressed_   = false;
384
385    isColliding_ = false;
386    collDisZ_ = 0;
387
388}
389
390
391
392
393
394//The following functions read the input of the player and then set the bools for the movement
395void SOBFigure::moveFrontBack(const Vector2& value)
396{
397    if (value.x > 0)
398    {
399        moveUpPressed_ = true;
400        moveDownPressed_ = false;
401    }
402    else
403    {
404        moveUpPressed_ = false;
405        moveDownPressed_ = true;
406    }
407}
408
409void SOBFigure::moveRightLeft(const Vector2& value)
410{
411    if (value.x > 0)
412    {
413        moveLeftPressed_ = false;
414        moveRightPressed_ = true;
415    }
416    else
417    {
418        moveLeftPressed_ = true;
419        moveRightPressed_ = false;
420    }
421}
422
423void SOBFigure::boost(bool boost)
424{
425    firePressed_ = boost;
426}
427
428
429
430// PRE: name is an existing name of a material. Example orxo_material for orxo_material.material in data_extern/materials
431//      !!! PowerUpCounter_ has to be modified before changing the clothes!!!
432// POST: clothes of body of player are changed to name
433void SOBFigure::changeClothes(){
434            // clothes: white (basic), red (one PowerUp), orange (Fireball enabled)
435            std::string clothes[] = {"orxo_material", "orxo_material_gross", "orxo_material_fire"};
436
437            std::set<WorldEntity*> attachedObjects = this->getAttachedObjects();
438            std::set<WorldEntity*>::iterator it;
439            for (it = attachedObjects.begin(); it != attachedObjects.end(); ++it)
440            {
441                Model* FiguresModel = orxonox_cast<Model*>(*it);
442                if (FiguresModel != nullptr)
443                {
444                    FiguresModel->setSubMaterial(clothes[PowerUpCounter_] , 4); // 4 is the body
445                }
446            }   
447}
448// PRE:
449// POST: Player jumps out of the game, game is finished and can be restarted.
450void SOBFigure::die(){
451    Vector3 vel = getVelocity();
452    vel.y = -80;
453    vel.z = 200;
454    setVelocity(vel);
455    predead_= true; 
456    SOB* SOBGame = orxonox_cast<SOB*>(getGametype());
457    SOBGame->setDone(true);
458}
459
460}
Note: See TracBrowser for help on using the repository browser.