[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" |
---|
[11505] | 40 | #include <math.h> |
---|
[11595] | 41 | #include <ctime> |
---|
[11503] | 42 | |
---|
| 43 | namespace orxonox |
---|
| 44 | { |
---|
| 45 | RegisterClass(FlappyOrxShip); |
---|
| 46 | |
---|
| 47 | FlappyOrxShip::FlappyOrxShip(Context* context) : SpaceShip(context) |
---|
| 48 | { |
---|
| 49 | RegisterObject(FlappyOrxShip); |
---|
| 50 | |
---|
[11759] | 51 | isFlapping = false; |
---|
[11565] | 52 | isDead = true; |
---|
[11761] | 53 | velocity = 0; |
---|
[11759] | 54 | speed = 0; |
---|
| 55 | upwardThrust = 0; |
---|
| 56 | gravity = 0; |
---|
[11631] | 57 | particleLifespan = 0.1; |
---|
[11620] | 58 | particleAge = 0; |
---|
[11759] | 59 | deathTime = 0; |
---|
[11620] | 60 | |
---|
| 61 | particlespawner_ = NULL; |
---|
[11514] | 62 | |
---|
| 63 | } |
---|
[11554] | 64 | |
---|
[11514] | 65 | void FlappyOrxShip::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[11620] | 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); |
---|
[11503] | 75 | } |
---|
[11514] | 76 | |
---|
[11503] | 77 | void FlappyOrxShip::tick(float dt) |
---|
[11514] | 78 | { |
---|
| 79 | SUPER(FlappyOrxShip, tick, dt); |
---|
[11620] | 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 | |
---|
[11514] | 96 | //Execute movement |
---|
[11566] | 97 | if (this->hasLocalController()) |
---|
[11503] | 98 | { |
---|
[11521] | 99 | if(getHealth()<0){ |
---|
[11543] | 100 | setHealth(1); |
---|
| 101 | getGame()->death(); |
---|
| 102 | death(); |
---|
[11521] | 103 | } |
---|
[11503] | 104 | Vector3 pos = getPosition(); |
---|
[11529] | 105 | |
---|
| 106 | getGame()->updatePlayerPos(pos.x); |
---|
| 107 | |
---|
| 108 | |
---|
[11566] | 109 | if(not isDead){ |
---|
[11761] | 110 | velocity += gravity*dt; |
---|
[11566] | 111 | if(isFlapping){ |
---|
| 112 | isFlapping = false; |
---|
| 113 | if(pos.z > -150) |
---|
[11761] | 114 | velocity = -upwardThrust; |
---|
[11566] | 115 | } |
---|
| 116 | |
---|
[11761] | 117 | pos += Vector3(speed, 0, velocity) * dt; |
---|
[11503] | 118 | } |
---|
| 119 | |
---|
[11566] | 120 | |
---|
[11503] | 121 | // Camera |
---|
| 122 | Camera* camera = this->getCamera(); |
---|
| 123 | if (camera != nullptr) |
---|
| 124 | { |
---|
[11543] | 125 | camera->setPosition(pos.x,-100,0); |
---|
| 126 | camera->setOrientation(Vector3::UNIT_Z, Degree(0)); |
---|
| 127 | |
---|
[11503] | 128 | } |
---|
| 129 | |
---|
[11529] | 130 | |
---|
[11503] | 131 | setPosition(pos); |
---|
[11761] | 132 | setOrientation(Vector3::UNIT_Y, Degree(270-velocity/10)); |
---|
[11543] | 133 | |
---|
| 134 | if(pos.z > 150){ |
---|
| 135 | getGame()->death(); |
---|
| 136 | death(); |
---|
| 137 | } |
---|
[11503] | 138 | |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
[11755] | 142 | time_t FlappyOrxShip::timeUntilRespawn(){ |
---|
[11635] | 143 | return 2-time(0)+deathTime; |
---|
[11595] | 144 | } |
---|
| 145 | |
---|
[11503] | 146 | void FlappyOrxShip::boost(bool boost){ |
---|
[11595] | 147 | |
---|
[11620] | 148 | if(not isDead){ |
---|
| 149 | particleAge = 0; |
---|
| 150 | } |
---|
[11595] | 151 | |
---|
| 152 | if(isDead&&timeUntilRespawn()<=0){ |
---|
[11565] | 153 | isDead = false; |
---|
[11576] | 154 | getGame()->setDead(false); |
---|
[11565] | 155 | } |
---|
[11503] | 156 | isFlapping=boost; |
---|
| 157 | } |
---|
| 158 | |
---|
[11624] | 159 | |
---|
[11503] | 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 | { |
---|
[11566] | 172 | isDead = true; |
---|
[11595] | 173 | |
---|
| 174 | deathTime = time(0); |
---|
[11537] | 175 | Vector3 pos = getPosition(); |
---|
| 176 | pos.x = 0; |
---|
[11543] | 177 | pos.z = 0; |
---|
| 178 | pos.y = 0; |
---|
[11761] | 179 | velocity = 0; |
---|
[11537] | 180 | setPosition(pos); |
---|
[11503] | 181 | } |
---|
| 182 | } |
---|