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