[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 | - instead of moving over the boarders of the playing field, modify the tick function so that the stones bounce back |
---|
| 37 | - when to split? It does not work if you override kill() function...->damage() function? |
---|
| 38 | */ |
---|
| 39 | |
---|
[11616] | 40 | #include "Asteroids2DStone.h" |
---|
| 41 | #include "Asteroids2D.h" |
---|
| 42 | #include "Asteroids2DShip.h" |
---|
| 43 | #include "core/CoreIncludes.h" |
---|
| 44 | #include "util/Math.h" |
---|
| 45 | |
---|
| 46 | namespace orxonox |
---|
| 47 | { |
---|
| 48 | RegisterClass(Asteroids2DStone); |
---|
| 49 | |
---|
| 50 | Asteroids2DStone::Asteroids2DStone(Context* context) : Pawn(context) |
---|
| 51 | { |
---|
| 52 | RegisterObject(Asteroids2DStone); |
---|
[11637] | 53 | this->size = rand()%3+1; |
---|
[11616] | 54 | this->width = 1043; |
---|
| 55 | this->height = 646; |
---|
| 56 | this->setPosition(randomPosition(this->width, this->height)); |
---|
[11637] | 57 | this->setCollisionTypeStr("dynamic"); |
---|
[11616] | 58 | this->setVelocity(randomVelocity(MAX_SPEED)); |
---|
| 59 | } |
---|
| 60 | |
---|
[11637] | 61 | Asteroids2DStone::Asteroids2DStone(Context* c, int sze, Vector3 pos) : Pawn(c) |
---|
| 62 | { |
---|
| 63 | RegisterObject(Asteroids2DStone); |
---|
| 64 | this->size = sze; |
---|
| 65 | this->width = 1043; |
---|
| 66 | this->height = 646; |
---|
| 67 | this->setPosition(pos); |
---|
| 68 | this->setCollisionTypeStr("dynamic"); |
---|
| 69 | this->setVelocity(randomVelocity(MAX_SPEED)); |
---|
| 70 | } |
---|
| 71 | |
---|
[11616] | 72 | Vector3 Asteroids2DStone::randomPosition(float maxwidth, float maxheight) |
---|
| 73 | { |
---|
| 74 | Vector3 pos; |
---|
| 75 | pos.x = rnd(-maxwidth/2, maxwidth/2); |
---|
| 76 | pos.y = 0; |
---|
| 77 | pos.z = rnd(-maxheight/2, maxheight/2); |
---|
| 78 | return pos; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | Vector3 Asteroids2DStone::randomVelocity(float maxspeed) |
---|
| 82 | { |
---|
| 83 | Vector3 vel; |
---|
| 84 | vel.x = rnd(maxspeed); |
---|
| 85 | vel.y = 0; |
---|
| 86 | vel.z = rnd(maxspeed); |
---|
| 87 | return vel; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | void Asteroids2DStone::tick(float dt) |
---|
| 92 | { |
---|
| 93 | SUPER(Asteroids2DStone, tick, dt); |
---|
| 94 | Vector3 pos = this->getPosition(); |
---|
[11637] | 95 | |
---|
[11660] | 96 | |
---|
[11637] | 97 | if(pos.x >= width/2){ |
---|
| 98 | pos.x = -width/2; |
---|
| 99 | }else if(pos.x <= -width/2){ |
---|
| 100 | pos.x = -width/2; |
---|
| 101 | }else if(pos.z >= height/2){ |
---|
| 102 | pos.z = -height/2; |
---|
| 103 | }else if(pos.z <= -height/2){ |
---|
| 104 | pos.z = -width/2; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | this->setPosition(pos); |
---|
| 108 | |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | inline bool Asteroids2DStone::collidesAgainst(WorldEntity* otherObject, const btCollisionShape* ownCollisionShape, btManifoldPoint& contactPoint) |
---|
| 112 | { |
---|
| 113 | if(orxonox_cast<Asteroids2DShip*>(otherObject)) |
---|
[11616] | 114 | { |
---|
[11660] | 115 | split(); |
---|
[11616] | 116 | } |
---|
[11637] | 117 | return false; |
---|
| 118 | } |
---|
[11616] | 119 | |
---|
[11666] | 120 | void Asteroids2DStone::damage(float damage, float healthdamage, float shielddamage, Pawn* originator, const btCollisionShape* cs) |
---|
| 121 | { |
---|
[11669] | 122 | if (getGame() && orxonox_cast<Asteroids2DShip*>(originator) != nullptr) |
---|
| 123 | getGame()->addPoints(100); |
---|
[11666] | 124 | split(); |
---|
| 125 | //Pawn::damage(damage, healthdamage, shielddamage, originator, cs); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | Asteroids2D* Asteroids2DStone::getGame() |
---|
| 129 | { |
---|
| 130 | if (game == nullptr) |
---|
| 131 | { |
---|
| 132 | for (Asteroids2D* asteroids : ObjectList<Asteroids2D>()) |
---|
| 133 | game = asteroids; |
---|
| 134 | } |
---|
| 135 | return game; |
---|
| 136 | } |
---|
| 137 | |
---|
[11637] | 138 | void Asteroids2DStone::split() |
---|
| 139 | { |
---|
[11668] | 140 | //orxout() << "split" << endl; |
---|
[11666] | 141 | if(size == 3) |
---|
[11637] | 142 | { |
---|
[11666] | 143 | Asteroids2DStone* newStone1 = new Asteroids2DStone(this->getContext(), 2, this->getPosition()); |
---|
| 144 | newStone1->addTemplate("stone2"); |
---|
| 145 | Asteroids2DStone* newStone2 = new Asteroids2DStone(this->getContext(), 2, this->getPosition()); |
---|
| 146 | newStone2->addTemplate("stone2"); |
---|
[11637] | 147 | |
---|
| 148 | }else if(size == 2){ |
---|
[11660] | 149 | |
---|
| 150 | |
---|
| 151 | //Templates can be found in data/levels/templates/asteroidsAsteroids2D.oxt |
---|
[11637] | 152 | Asteroids2DStone* newStone1 = new Asteroids2DStone(this->getContext(), 1, this->getPosition()); |
---|
| 153 | newStone1->addTemplate("stone1"); |
---|
| 154 | Asteroids2DStone* newStone2 = new Asteroids2DStone(this->getContext(), 1, this->getPosition()); |
---|
| 155 | newStone2->addTemplate("stone1"); |
---|
[11666] | 156 | } |
---|
| 157 | this->death(); |
---|
[11616] | 158 | } |
---|
| 159 | |
---|
| 160 | } |
---|