Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/Presentation_HS17_merge/src/modules/flappyorx/FlappyOrxShip.cc @ 11760

Last change on this file since 11760 was 11759, checked in by landauf, 7 years ago

[FlappyOrx_HS17] initialize all variables

File size: 4.9 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 *      FLeo Merholz
24 *      Pascal Schärli
25 *   Co-authors:
26 *      ...
27 *
28 */
29
30/**
31    @file FlappyOrxShip.cc
32    @brief Implementation of the FlappyOrxShip class.
33*/
34
35#include "FlappyOrxShip.h"
36#include "core/CoreIncludes.h"
37#include "core/XMLPort.h"
38#include "graphics/Camera.h"
39#include "graphics/ParticleSpawner.h"
40#include <math.h> 
41#include <ctime>
42
43namespace orxonox
44{
45    RegisterClass(FlappyOrxShip);
46
47    FlappyOrxShip::FlappyOrxShip(Context* context) : SpaceShip(context)
48    {
49        RegisterObject(FlappyOrxShip);
50
51        isFlapping = false;
52        isDead = true;
53        speed = 0;
54        upwardThrust = 0;
55        gravity = 0;
56        particleLifespan = 0.1;
57        particleAge = 0;
58        deathTime = 0;
59
60        particlespawner_ = NULL;
61       
62    }
63
64    void FlappyOrxShip::XMLPort(Element& xmlelement, XMLPort::Mode mode)
65    {   
66        SUPER(FlappyOrxShip, XMLPort, xmlelement, mode);
67        XMLPortParam(FlappyOrxShip,"speedBase", setSpeedBase, getSpeedBase, xmlelement,mode);
68        XMLPortParam(FlappyOrxShip,"speedIncrease", setSpeedIncrease, getSpeedIncrease, xmlelement,mode);
69        XMLPortParam(FlappyOrxShip,"tubeDistanceBase", setTubeDistanceBase, getTubeDistanceBase, xmlelement,mode);
70        XMLPortParam(FlappyOrxShip,"tubeDistanceIncrease", setTubeDistanceIncrease, getTubeDistanceIncrease, xmlelement,mode);
71
72        XMLPortParam(FlappyOrxShip,"upwardThrust", setUpwardthrust, getUpwardthrust, xmlelement,mode);
73        XMLPortParam(FlappyOrxShip,"gravity", setGravity, getGravity, xmlelement,mode);
74    }
75
76    void FlappyOrxShip::tick(float dt)
77    {
78        SUPER(FlappyOrxShip, tick, dt);
79
80
81        particleAge+=dt;
82        //the particle spawner that generates the fire from the backpack when pressed
83        if (particlespawner_ == NULL) {
84            for (WorldEntity* object : this->getAttachedObjects())
85            {
86                if (object->isA(Class(ParticleSpawner)))
87                particlespawner_ = (ParticleSpawner*) object;
88            }
89        }
90        else if(particleAge>particleLifespan){
91            particlespawner_->setLifetime(1);
92        }
93
94
95        //Execute movement
96        if (this->hasLocalController())
97        {
98            if(getHealth()<0){
99                setHealth(1);
100                getGame()->death();
101                death();   
102            }
103            Vector3 pos = getPosition();
104
105            getGame()->updatePlayerPos(pos.x);
106           
107
108            if(not isDead){
109                velocity.y += gravity*dt;
110                if(isFlapping){
111                    isFlapping = false;
112                    if(pos.z > -150)
113                        velocity.y = -upwardThrust;
114                }
115
116                pos += Vector3(speed + velocity.x, 0, velocity.y) * dt; 
117            }
118           
119                       
120            // Camera
121            Camera* camera = this->getCamera();
122            if (camera != nullptr)
123            {
124                camera->setPosition(pos.x,-100,0);
125                camera->setOrientation(Vector3::UNIT_Z, Degree(0));
126
127            }
128
129           
130            setPosition(pos);
131            setOrientation(Vector3::UNIT_Y, Degree(270-velocity.y/10));
132
133            if(pos.z > 150){
134                getGame()->death();
135                death();
136            }
137           
138        }
139    }
140
141    time_t FlappyOrxShip::timeUntilRespawn(){
142        return 2-time(0)+deathTime;
143    }
144
145    void FlappyOrxShip::boost(bool boost){
146
147        if(not isDead){
148            particleAge = 0;
149        } 
150   
151        if(isDead&&timeUntilRespawn()<=0){
152            isDead = false;
153            getGame()->setDead(false);
154        }
155        isFlapping=boost;
156    }
157
158 
159    FlappyOrx* FlappyOrxShip::getGame()
160    {
161        if (game == nullptr)
162        {
163            for (FlappyOrx* flappyOrx : ObjectList<FlappyOrx>())
164                game = flappyOrx;
165        }
166        return game;
167    }
168
169    void FlappyOrxShip::death()
170    {
171        isDead = true;
172
173        deathTime = time(0);
174        Vector3 pos = getPosition();
175        pos.x = 0;
176        pos.z = 0;
177        pos.y = 0;
178        velocity.y = 0;
179        setPosition(pos);
180    }
181}
Note: See TracBrowser for help on using the repository browser.