Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added orxo_material_gross.png to BlenderFilesSOB

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