Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11592 was 11592, checked in by zarron, 6 years ago

shooting mechanism fixed

File size: 13.7 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        //Check if otherObject is a Gumba (that walking enemies)
135
136         else if (gumba != nullptr && !(gumba->hasCollided_)) {
137
138            //If player jumps on its head, kill the Gumba, else, kill the player
139            if (getVelocity().z >= -20) {
140                // If player hasn't a power up, he dies. Else he shrinks and the gumba dies.
141                if(PowerUpCounter_ == 0){
142                    this->die();
143                } 
144                else{
145                    PowerUpCounter_--;
146                    this->changeClothes();
147
148                    gumba->destroyLater();
149                    gumba->hasCollided_ = true;
150                }
151
152          } else {
153            gumba->destroyLater();
154            gumba->hasCollided_ = true;
155            SOBGame->addGumba();
156
157
158        }
159    }
160
161        else if (fireball != nullptr && !(fireball->hasCollided_)){
162            //this-> die();
163        }
164
165    //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
166    if (reachedLvlEndState_ == 0 && flagstone != nullptr && !(flagstone->hasCollided_)) {
167        flagstone->hasCollided_ = true;
168        reachedLvlEndState_ = 1;
169
170        SOBGame->setDone(true);
171        SOBGame->addPoints(flagstone->getPoints());
172       
173
174    }
175    if (castlestone != nullptr && !(castlestone->hasCollided_)) {
176        castlestone->hasCollided_ = true;
177        reachedLvlEndState_++;
178
179    }
180
181    return true;
182}
183
184
185//Self implemented sign function that returns either 1 or -1 (and never 0)
186int SOBFigure::sgn(float x) {
187    if (x < 0.0) return -1;
188    return 1;
189}
190
191//Function to spawn the Fireball
192void SOBFigure::spawnFireball() {
193        SOBCenterpoint* center_ = ((SOB*)getGametype())->center_;
194
195         SOBFireball* ball = new SOBFireball(center_->getContext());
196         Vector3 spawnpos = this->getWorldPosition();
197         spawnpos.z += 0;
198
199        if (ball != nullptr && center_ != nullptr)
200        {
201            ball->addTemplate("fireball");
202            bool direction = ((this->getWorldOrientation().getRoll().valueRadians())>-1.6&&(this->getWorldOrientation().getRoll().valueRadians()<1.6));
203            ball->setDirection(direction);
204            orxout() << "Rotation: " << this->getWorldOrientation().getRoll() << " direction: "<< direction <<endl;
205            if(direction)
206            {
207                spawnpos.x+=10;
208            }
209            else
210            {
211                spawnpos.x-=10;
212            }
213            ball->setPosition(spawnpos);
214
215        }
216     }
217
218//For those of you who don't have an idea: the tick function is called about 50 times/sec
219void SOBFigure::tick(float dt)
220{
221    SUPER(SOBFigure, tick, dt);
222
223
224    bool inputAllowed = true;
225
226    //the particle spawner that generates the fire from the backpack when pressed
227    if (particlespawner_ == NULL) {
228        for (WorldEntity* object : this->getAttachedObjects())
229        {
230           if (object->isA(Class(ParticleSpawner)))
231            particlespawner_ = object;
232        }
233    }
234
235
236    //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
237    if (reachedLvlEndState_ != 0) {
238        timeCounter_+= dt;
239        inputAllowed = false;
240    }
241    if (reachedLvlEndState_ == 1 && timeCounter_ >= 1.5) {
242        timeCounter_ = 0;
243        reachedLvlEndState_ = 2;
244    }
245
246
247    //if input blocked, then cancel every movement operation
248    if (!inputAllowed) {
249        moveUpPressed_      = false;
250        moveDownPressed_    = false;
251        moveLeftPressed_    = false;
252        moveRightPressed_   = false;
253    }
254
255    //set the gravityto standard 350
256    if (firePressed_ == false) {
257        gravityAcceleration_ = 350.0;
258
259    }
260
261    if (hasLocalController())
262    {
263        SOB* SOBGame = orxonox_cast<SOB*>(getGametype());
264        Vector3 velocity = getVelocity();
265        Vector3 position = getPosition();
266
267        if (!predead_)
268            velocity.y = 0;
269        //If player falls in a hole
270        if (position.z < -100) {
271            dead_ = true;
272            SOBGame->setDone(true);
273        }
274
275
276        if (dead_) {
277            velocity.x = 0;
278            velocity.z = 0;
279            setVelocity(velocity);
280           
281            if (firePressed_)
282                SOBGame->restart();
283            return;
284        }
285
286
287        int maxvelocity_x = 100;
288        int speedAddedPerTick = 5;
289        int camMaxOffset = 25;
290
291        timeSinceLastFire_ += dt;
292        lastSpeed_z = velocity.z;
293
294
295
296        //Handle the rocket fire from the jetpack
297        if (velocity.z > 40)
298            particlespawner_->setVisible(true); 
299        else
300            particlespawner_->setVisible(false); 
301
302
303        //If player hits space and collides against an object under him then jump
304        if (inputAllowed && firePressed_ && isColliding_ && (collDisZ_ >= 0 && collDisZ_ <+ 10)) {
305            gravityAcceleration_ = 100.0;
306            velocity.z = 110; 
307        }
308
309
310        //Left-right movement with acceleration and rotation
311        float rot = getOrientation().getRoll().valueDegrees();
312        if (moveRightPressed_) {
313            if (!(rot < 5.0 && -5.0 < rot))
314                setOrientation(Vector3::UNIT_Z, getOrientation().getRoll() - sgn(rot)*dt*Radian(6));
315
316            if (std::abs(velocity.x) < maxvelocity_x) {
317                velocity.x += speedAddedPerTick;
318
319            }
320        } else if (moveLeftPressed_) {
321            if (!(abs(rot) > 175.0 ))
322                setOrientation(Vector3::UNIT_Z, getOrientation().getRoll() + sgn(rot)*dt*Radian(6));
323
324
325
326            if (std::abs(velocity.x) < maxvelocity_x) {
327                velocity.x -= speedAddedPerTick;
328            }
329        } else {
330            velocity.x /= 1.1;
331        }
332
333        //If moveUp pressed, fire a fireball
334        if(moveUpPressed_ && (PowerUpCounter_ >= FireballPower) && fireallowed_)
335        {
336            spawnFireball();
337            fireallowed_  = false;
338            firecooldown_ = 0;
339        }
340
341        //Increase the firecooldown
342        if(firecooldown_> 0.5)
343        {
344            fireallowed_ = true;
345        }
346        if(!fireallowed_)
347        {
348            firecooldown_ += dt;
349        }
350
351        //Again another EndOfLevel behavior
352        if (reachedLvlEndState_ == 1)
353            velocity.x = -2;
354        if (reachedLvlEndState_ == 2)
355            velocity.x = 30;
356        if (reachedLvlEndState_ == 3) {
357            velocity.x = 0;
358            velocity.y = 20;
359        }
360        if (reachedLvlEndState_ == 4) {
361            lvlEnded_ = true;
362            dead_ = true;
363        }
364
365        //velocity = acc. * time
366        velocity.z -= gravityAcceleration_*dt;
367        setVelocity(velocity);
368
369
370        //Camera operation - the camera should always follow the player in a specific region
371        Camera* cam = getCamera();
372        Vector3 campos = cam->getPosition();
373
374        if (campos.x + camMaxOffset < position.x) {
375            campos.x = position.x - camMaxOffset;
376            cam->setPosition(campos);
377        }
378        if (campos.x - camMaxOffset > position.x) {
379            campos.x = position.x + camMaxOffset;
380            cam->setPosition(campos);
381        }
382
383    }
384
385    // Reset key variables
386    moveUpPressed_      = false;
387    moveDownPressed_    = false;
388    moveLeftPressed_    = false;
389    moveRightPressed_   = false;
390
391    isColliding_ = false;
392    collDisZ_ = 0;
393
394}
395
396
397
398
399
400//The following functions read the input of the player and then set the bools for the movement
401void SOBFigure::moveFrontBack(const Vector2& value)
402{
403    if (value.x > 0)
404    {
405        moveUpPressed_ = true;
406        moveDownPressed_ = false;
407    }
408    else
409    {
410        moveUpPressed_ = false;
411        moveDownPressed_ = true;
412    }
413}
414
415void SOBFigure::moveRightLeft(const Vector2& value)
416{
417    if (value.x > 0)
418    {
419        moveLeftPressed_ = false;
420        moveRightPressed_ = true;
421    }
422    else
423    {
424        moveLeftPressed_ = true;
425        moveRightPressed_ = false;
426    }
427}
428
429void SOBFigure::boost(bool boost)
430{
431    firePressed_ = boost;
432}
433
434
435
436// PRE: name is an existing name of a material. Example orxo_material for orxo_material.material in data_extern/materials
437//      !!! PowerUpCounter_ has to be modified before changing the clothes!!!
438// POST: clothes of body of player are changed to name
439void SOBFigure::changeClothes(){
440            // clothes: white (basic), red (one PowerUp), orange (Fireball enabled)
441            std::string clothes[] = {"orxo_material", "orxo_material_gross", "orxo_material_fire"};
442
443            std::set<WorldEntity*> attachedObjects = this->getAttachedObjects();
444            std::set<WorldEntity*>::iterator it;
445            for (it = attachedObjects.begin(); it != attachedObjects.end(); ++it)
446            {
447                Model* FiguresModel = orxonox_cast<Model*>(*it);
448                if (FiguresModel != nullptr)
449                {
450                    FiguresModel->setSubMaterial(clothes[PowerUpCounter_] , 4); // 4 is the body
451                }
452            }   
453}
454// PRE:
455// POST: Player jumps out of the game, game is finished and can be restarted.
456void SOBFigure::die(){
457    Vector3 vel = getVelocity();
458    vel.y = -80;
459    vel.z = 200;
460    setVelocity(vel);
461    predead_= true; 
462    SOB* SOBGame = orxonox_cast<SOB*>(getGametype());
463    SOBGame->setDone(true);
464}
465
466}
Note: See TracBrowser for help on using the repository browser.