[11616] | 1 | /* ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 2 | * > www.orxonox.net < |
---|
| 3 | * |
---|
| 4 | * |
---|
| 5 | * License notice: |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or |
---|
| 8 | * modify it under the terms of the GNU General Public License |
---|
| 9 | * as published by the Free Software Foundation; either version 2 |
---|
| 10 | * of the License, or (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | * |
---|
| 21 | * Author: |
---|
[11660] | 22 | * Viviane Yang |
---|
[11616] | 23 | * Co-authors: |
---|
| 24 | * ... |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
[11660] | 28 | |
---|
| 29 | |
---|
[11616] | 30 | /** |
---|
| 31 | @file Asteroids2DStone.cc |
---|
[11660] | 32 | |
---|
[11616] | 33 | @brief Implementation of the Asteroids2DStone class. |
---|
| 34 | |
---|
[11660] | 35 | TO DO: |
---|
| 36 | - when to split? It does not work if you override kill() function...->damage() function? |
---|
| 37 | */ |
---|
| 38 | |
---|
[11616] | 39 | #include "Asteroids2DStone.h" |
---|
| 40 | #include "Asteroids2D.h" |
---|
| 41 | #include "Asteroids2DShip.h" |
---|
| 42 | #include "core/CoreIncludes.h" |
---|
| 43 | #include "util/Math.h" |
---|
| 44 | |
---|
| 45 | namespace orxonox |
---|
| 46 | { |
---|
| 47 | RegisterClass(Asteroids2DStone); |
---|
| 48 | |
---|
| 49 | Asteroids2DStone::Asteroids2DStone(Context* context) : Pawn(context) |
---|
| 50 | { |
---|
| 51 | RegisterObject(Asteroids2DStone); |
---|
[11637] | 52 | this->size = rand()%3+1; |
---|
[11616] | 53 | this->width = 1043; |
---|
| 54 | this->height = 646; |
---|
| 55 | this->setPosition(randomPosition(this->width, this->height)); |
---|
[11742] | 56 | this->setCollisionType(WorldEntity::CollisionType::Dynamic); |
---|
[11616] | 57 | this->setVelocity(randomVelocity(MAX_SPEED)); |
---|
| 58 | } |
---|
| 59 | |
---|
[11637] | 60 | Asteroids2DStone::Asteroids2DStone(Context* c, int sze, Vector3 pos) : Pawn(c) |
---|
| 61 | { |
---|
| 62 | RegisterObject(Asteroids2DStone); |
---|
| 63 | this->size = sze; |
---|
| 64 | this->width = 1043; |
---|
| 65 | this->height = 646; |
---|
| 66 | this->setPosition(pos); |
---|
[11742] | 67 | this->setCollisionType(WorldEntity::CollisionType::Dynamic); |
---|
[11637] | 68 | this->setVelocity(randomVelocity(MAX_SPEED)); |
---|
| 69 | } |
---|
| 70 | |
---|
[11616] | 71 | Vector3 Asteroids2DStone::randomPosition(float maxwidth, float maxheight) |
---|
| 72 | { |
---|
| 73 | Vector3 pos; |
---|
| 74 | pos.x = rnd(-maxwidth/2, maxwidth/2); |
---|
| 75 | pos.y = 0; |
---|
| 76 | pos.z = rnd(-maxheight/2, maxheight/2); |
---|
| 77 | return pos; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | Vector3 Asteroids2DStone::randomVelocity(float maxspeed) |
---|
| 81 | { |
---|
| 82 | Vector3 vel; |
---|
[11728] | 83 | vel.x = rnd(-maxspeed, maxspeed); |
---|
[11616] | 84 | vel.y = 0; |
---|
[11728] | 85 | vel.z = rnd(-maxspeed, maxspeed); |
---|
[11616] | 86 | return vel; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | void Asteroids2DStone::tick(float dt) |
---|
| 91 | { |
---|
| 92 | SUPER(Asteroids2DStone, tick, dt); |
---|
| 93 | Vector3 pos = this->getPosition(); |
---|
[11728] | 94 | Vector3 vel = this->getVelocity(); |
---|
[11637] | 95 | |
---|
[11728] | 96 | //ensure that the stone bounces from the playing field borders |
---|
| 97 | if(pos.x > width/2 || pos.x < -width/2) vel.x = -vel.x; |
---|
| 98 | if(pos.z > height/2 || pos.z < -height/2) vel.z = -vel.z; |
---|
| 99 | this->setVelocity(vel); |
---|
[11660] | 100 | |
---|
[11728] | 101 | //2D movement, position should always = 0 on y-axis |
---|
| 102 | if(pos.y!=0) pos.y = 0; |
---|
[11637] | 103 | this->setPosition(pos); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | inline bool Asteroids2DStone::collidesAgainst(WorldEntity* otherObject, const btCollisionShape* ownCollisionShape, btManifoldPoint& contactPoint) |
---|
| 107 | { |
---|
| 108 | if(orxonox_cast<Asteroids2DShip*>(otherObject)) |
---|
[11616] | 109 | { |
---|
[11660] | 110 | split(); |
---|
[11616] | 111 | } |
---|
[11637] | 112 | return false; |
---|
| 113 | } |
---|
[11616] | 114 | |
---|
[11666] | 115 | void Asteroids2DStone::damage(float damage, float healthdamage, float shielddamage, Pawn* originator, const btCollisionShape* cs) |
---|
| 116 | { |
---|
[11669] | 117 | if (getGame() && orxonox_cast<Asteroids2DShip*>(originator) != nullptr) |
---|
| 118 | getGame()->addPoints(100); |
---|
[11666] | 119 | split(); |
---|
| 120 | //Pawn::damage(damage, healthdamage, shielddamage, originator, cs); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | Asteroids2D* Asteroids2DStone::getGame() |
---|
| 124 | { |
---|
| 125 | if (game == nullptr) |
---|
| 126 | { |
---|
| 127 | for (Asteroids2D* asteroids : ObjectList<Asteroids2D>()) |
---|
| 128 | game = asteroids; |
---|
| 129 | } |
---|
| 130 | return game; |
---|
| 131 | } |
---|
| 132 | |
---|
[11637] | 133 | void Asteroids2DStone::split() |
---|
| 134 | { |
---|
[11668] | 135 | //orxout() << "split" << endl; |
---|
[11666] | 136 | if(size == 3) |
---|
[11637] | 137 | { |
---|
[11666] | 138 | Asteroids2DStone* newStone1 = new Asteroids2DStone(this->getContext(), 2, this->getPosition()); |
---|
| 139 | newStone1->addTemplate("stone2"); |
---|
| 140 | Asteroids2DStone* newStone2 = new Asteroids2DStone(this->getContext(), 2, this->getPosition()); |
---|
| 141 | newStone2->addTemplate("stone2"); |
---|
[11637] | 142 | |
---|
| 143 | }else if(size == 2){ |
---|
[11660] | 144 | |
---|
| 145 | |
---|
| 146 | //Templates can be found in data/levels/templates/asteroidsAsteroids2D.oxt |
---|
[11637] | 147 | Asteroids2DStone* newStone1 = new Asteroids2DStone(this->getContext(), 1, this->getPosition()); |
---|
| 148 | newStone1->addTemplate("stone1"); |
---|
| 149 | Asteroids2DStone* newStone2 = new Asteroids2DStone(this->getContext(), 1, this->getPosition()); |
---|
| 150 | newStone2->addTemplate("stone1"); |
---|
[11666] | 151 | } |
---|
| 152 | this->death(); |
---|
[11616] | 153 | } |
---|
| 154 | |
---|
| 155 | } |
---|