Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/OrxoBlox_FS19/src/modules/flappyorx/FlappyOrxShip.cc @ 12359

Last change on this file since 12359 was 11781, checked in by landauf, 7 years ago

eol-style native (no changes in code)

  • Property svn:eol-style set to native
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        velocity = 0;
54        speed = 0;
55        upwardThrust = 0;
56        gravity = 0;
57        particleLifespan = 0.1;
58        particleAge = 0;
59        deathTime = 0;
60
61        particlespawner_ = NULL;
62       
63    }
64
65    void FlappyOrxShip::XMLPort(Element& xmlelement, XMLPort::Mode mode)
66    {   
67        SUPER(FlappyOrxShip, XMLPort, xmlelement, mode);
68        XMLPortParam(FlappyOrxShip,"speedBase", setSpeedBase, getSpeedBase, xmlelement,mode);
69        XMLPortParam(FlappyOrxShip,"speedIncrease", setSpeedIncrease, getSpeedIncrease, xmlelement,mode);
70        XMLPortParam(FlappyOrxShip,"tubeDistanceBase", setTubeDistanceBase, getTubeDistanceBase, xmlelement,mode);
71        XMLPortParam(FlappyOrxShip,"tubeDistanceIncrease", setTubeDistanceIncrease, getTubeDistanceIncrease, xmlelement,mode);
72
73        XMLPortParam(FlappyOrxShip,"upwardThrust", setUpwardthrust, getUpwardthrust, xmlelement,mode);
74        XMLPortParam(FlappyOrxShip,"gravity", setGravity, getGravity, xmlelement,mode);
75    }
76
77    void FlappyOrxShip::tick(float dt)
78    {
79        SUPER(FlappyOrxShip, tick, dt);
80
81
82        particleAge+=dt;
83        //the particle spawner that generates the fire from the backpack when pressed
84        if (particlespawner_ == NULL) {
85            for (WorldEntity* object : this->getAttachedObjects())
86            {
87                if (object->isA(Class(ParticleSpawner)))
88                particlespawner_ = (ParticleSpawner*) object;
89            }
90        }
91        else if(particleAge>particleLifespan){
92            particlespawner_->setLifetime(1);
93        }
94
95
96        //Execute movement
97        if (this->hasLocalController())
98        {
99            if(getHealth()<0){
100                setHealth(1);
101                getGame()->death();
102                death();   
103            }
104            Vector3 pos = getPosition();
105
106            getGame()->updatePlayerPos(pos.x);
107           
108
109            if(not isDead){
110                velocity += gravity*dt;
111                if(isFlapping){
112                    isFlapping = false;
113                    if(pos.z > -150)
114                        velocity = -upwardThrust;
115                }
116
117                pos += Vector3(speed, 0, velocity) * dt;
118            }
119           
120                       
121            // Camera
122            Camera* camera = this->getCamera();
123            if (camera != nullptr)
124            {
125                camera->setPosition(pos.x,-100,0);
126                camera->setOrientation(Vector3::UNIT_Z, Degree(0));
127
128            }
129
130           
131            setPosition(pos);
132            setOrientation(Vector3::UNIT_Y, Degree(270-velocity/10));
133
134            if(pos.z > 150){
135                getGame()->death();
136                death();
137            }
138           
139        }
140    }
141
142    time_t FlappyOrxShip::timeUntilRespawn(){
143        return 2-time(0)+deathTime;
144    }
145
146    void FlappyOrxShip::boost(bool boost){
147
148        if(not isDead){
149            particleAge = 0;
150        } 
151   
152        if(isDead&&timeUntilRespawn()<=0){
153            isDead = false;
154            getGame()->setDead(false);
155        }
156        isFlapping=boost;
157    }
158
159 
160    FlappyOrx* FlappyOrxShip::getGame()
161    {
162        if (game == nullptr)
163        {
164            for (FlappyOrx* flappyOrx : ObjectList<FlappyOrx>())
165                game = flappyOrx;
166        }
167        return game;
168    }
169
170    void FlappyOrxShip::death()
171    {
172        isDead = true;
173
174        deathTime = time(0);
175        Vector3 pos = getPosition();
176        pos.x = 0;
177        pos.z = 0;
178        pos.y = 0;
179        velocity = 0;
180        setPosition(pos);
181    }
182}
Note: See TracBrowser for help on using the repository browser.