Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

function to change clothes

File size: 11.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
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::string name = "orxo_material_gross";
116            this->changeClothes(name);
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                   
139           // now, change the clothes of the Figure to old ones
140            std::string name = "orxo_material";
141            this->changeClothes(name);
142                    gumba->destroyLater();
143                    gumba->hasCollided_ = true;
144                }
145
146          } else {
147            gumba->destroyLater();
148            gumba->hasCollided_ = true;
149            SOB* SOBGame = orxonox_cast<SOB*>(getGametype());
150            SOBGame->addGumba();
151
152
153        }
154    }
155
156    //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
157    if (reachedLvlEndState_ == 0 && flagstone != nullptr && !(flagstone->hasCollided_)) {
158        flagstone->hasCollided_ = true;
159        reachedLvlEndState_ = 1;
160        SOB* SOBGame = orxonox_cast<SOB*>(getGametype());
161        SOBGame->setDone(true);
162        SOBGame->addPoints(flagstone->getPoints());
163       
164
165    }
166    if (castlestone != nullptr && !(castlestone->hasCollided_)) {
167        castlestone->hasCollided_ = true;
168        reachedLvlEndState_++;
169
170    }
171
172    return true;
173}
174
175
176//Self implemented sign function that returns either 1 or -1 (and never 0)
177int SOBFigure::sgn(float x) {
178    if (x < 0.0) return -1;
179    return 1;
180}
181
182//For those of you who don't have an idea: the tick function is called about 50 times/sec
183void SOBFigure::tick(float dt)
184{
185    SUPER(SOBFigure, tick, dt);
186
187
188    bool inputAllowed = true;
189
190    //the particle spawner that generates the fire from the backpack when pressed
191    if (particlespawner_ == NULL) {
192        for (WorldEntity* object : this->getAttachedObjects())
193        {
194           if (object->isA(Class(ParticleSpawner)))
195            particlespawner_ = object;
196        }
197    }
198
199
200    //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
201    if (reachedLvlEndState_ != 0) {
202        timeCounter_+= dt;
203        inputAllowed = false;
204    }
205    if (reachedLvlEndState_ == 1 && timeCounter_ >= 1.5) {
206        timeCounter_ = 0;
207        reachedLvlEndState_ = 2;
208    }
209
210
211    //if input blocked, then cancel every movement operation
212    if (!inputAllowed) {
213        moveUpPressed_ = false;
214        moveDownPressed_ = false;
215        moveLeftPressed_ = false;
216        moveRightPressed_ = false;
217    }
218
219    //set the gravityto standard 350
220    if (firePressed_ == false) {
221        gravityAcceleration_ = 350.0;
222
223    }
224
225    if (hasLocalController())
226    {
227        Vector3 velocity = getVelocity();
228        Vector3 position = getPosition();
229
230        if (!predead_)
231            velocity.y = 0;
232        //If player falls in a hole
233        if (position.z < -100) {
234            dead_ = true;
235            SOB* SOBGame = orxonox_cast<SOB*>(getGametype());
236            SOBGame->setDone(true);
237        }
238
239
240        if (dead_) {
241            velocity.x = 0;
242            velocity.z = 0;
243            setVelocity(velocity);
244            SOB* SOBGame = orxonox_cast<SOB*>(getGametype());
245            if (firePressed_)
246                SOBGame->restart();
247            return;
248        }
249
250
251        int maxvelocity_x = 100;
252        int speedAddedPerTick = 5;
253        int camMaxOffset = 25;
254
255        timeSinceLastFire_ += dt;
256        lastSpeed_z = velocity.z;
257
258
259
260        //Handle the rocket fire from the jetpack
261        if (velocity.z > 40)
262            particlespawner_->setVisible(true); 
263        else
264            particlespawner_->setVisible(false); 
265
266
267        //If player hits space and collides against an object under him then jump
268        if (inputAllowed && firePressed_ && isColliding_ && (collDisZ_ >= 0 && collDisZ_ <+ 10)) {
269        //if (inputAllowed && firePressed_ && isColliding_ ) {
270
271            gravityAcceleration_ = 100.0;
272            velocity.z = 110; 
273        }
274
275
276        //Left-right movement with acceleration and rotation
277        float rot = getOrientation().getRoll().valueDegrees();
278        if (moveRightPressed_) {
279            if (!(rot < 5.0 && -5.0 < rot))
280                setOrientation(Vector3::UNIT_Z, getOrientation().getRoll() - sgn(rot)*dt*Radian(6));
281
282            if (std::abs(velocity.x) < maxvelocity_x) {
283                velocity.x += speedAddedPerTick;
284
285            }
286        } else if (moveLeftPressed_) {
287            if (!(abs(rot) > 175.0 ))
288                setOrientation(Vector3::UNIT_Z, getOrientation().getRoll() + sgn(rot)*dt*Radian(6));
289
290
291
292            if (std::abs(velocity.x) < maxvelocity_x) {
293                velocity.x -= speedAddedPerTick;
294            }
295        } else {
296            velocity.x /= 1.1;
297        }
298
299
300        //Again another EndOfLevel behavior
301        if (reachedLvlEndState_ == 1)
302            velocity.x = -2;
303        if (reachedLvlEndState_ == 2)
304            velocity.x = 30;
305        if (reachedLvlEndState_ == 3) {
306            velocity.x = 0;
307            velocity.y = 20;
308        }
309        if (reachedLvlEndState_ == 4) {
310            lvlEnded_ = true;
311            dead_ = true;
312        }
313
314        //velocity = acc. * time
315        velocity.z -= gravityAcceleration_*dt;
316        setVelocity(velocity);
317
318
319        //Camera operation - the camera should always follow the player in a specific region
320        Camera* cam = getCamera();
321        Vector3 campos = cam->getPosition();
322
323        if (campos.x + camMaxOffset < position.x) {
324            campos.x = position.x - camMaxOffset;
325            cam->setPosition(campos);
326        }
327        if (campos.x - camMaxOffset > position.x) {
328            campos.x = position.x + camMaxOffset;
329            cam->setPosition(campos);
330        }
331
332
333
334
335    }
336
337
338
339    // Reset key variables
340    moveUpPressed_ = false;
341    moveDownPressed_ = false;
342    moveLeftPressed_ = false;
343    moveRightPressed_ = false;
344
345    isColliding_ = false;
346    collDisZ_ = 0;
347
348}
349
350
351
352
353
354//The following functions read the input of the player and then set the bools for the movement
355void SOBFigure::moveFrontBack(const Vector2& value)
356{
357    if (value.x > 0)
358    {
359        moveUpPressed_ = true;
360        moveDownPressed_ = false;
361    }
362    else
363    {
364        moveUpPressed_ = false;
365        moveDownPressed_ = true;
366    }
367}
368
369void SOBFigure::moveRightLeft(const Vector2& value)
370{
371    if (value.x > 0)
372    {
373        moveLeftPressed_ = false;
374        moveRightPressed_ = true;
375    }
376    else
377    {
378        moveLeftPressed_ = true;
379        moveRightPressed_ = false;
380    }
381}
382
383void SOBFigure::boost(bool boost)
384{
385    firePressed_ = boost;
386}
387
388
389
390// PRE: name is an existing name of a material. Example orxo_material for orxo_material.material in data_extern/materials
391// POST: clothes of body of player are changed to name
392void SOBFigure::changeClothes(std::string& name){
393            std::set<WorldEntity*> attachedObjects = this->getAttachedObjects();
394            std::set<WorldEntity*>::iterator it;
395            for (it = attachedObjects.begin(); it != attachedObjects.end(); ++it)
396            {
397                Model* FiguresModel = orxonox_cast<Model*>(*it);
398                if (FiguresModel != nullptr)
399                {
400
401                    FiguresModel->setSubMaterial(name, 4); // 4 is the body
402
403                }
404            }   
405}
406
407}
Note: See TracBrowser for help on using the repository browser.