/* ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * Viviane Yang * Co-authors: * ... * */ /** @file Asteroids2DStone.cc @brief Implementation of the Asteroids2DStone class. TO DO: - when to split? It does not work if you override kill() function...->damage() function? */ #include "Asteroids2DStone.h" #include "Asteroids2D.h" #include "Asteroids2DShip.h" #include "core/CoreIncludes.h" #include "util/Math.h" namespace orxonox { RegisterClass(Asteroids2DStone); Asteroids2DStone::Asteroids2DStone(Context* context) : Pawn(context) { RegisterObject(Asteroids2DStone); this->size = rand()%3+1; this->width = 1043; this->height = 646; this->setPosition(randomPosition(this->width, this->height)); this->setCollisionType(WorldEntity::CollisionType::Dynamic); this->setVelocity(randomVelocity(MAX_SPEED)); } Asteroids2DStone::Asteroids2DStone(Context* c, int sze, Vector3 pos) : Pawn(c) { RegisterObject(Asteroids2DStone); this->size = sze; this->width = 1043; this->height = 646; this->setPosition(pos); this->setCollisionType(WorldEntity::CollisionType::Dynamic); this->setVelocity(randomVelocity(MAX_SPEED)); } Vector3 Asteroids2DStone::randomPosition(float maxwidth, float maxheight) { Vector3 pos; pos.x = rnd(-maxwidth/2, maxwidth/2); pos.y = 0; pos.z = rnd(-maxheight/2, maxheight/2); return pos; } Vector3 Asteroids2DStone::randomVelocity(float maxspeed) { Vector3 vel; vel.x = rnd(-maxspeed, maxspeed); vel.y = 0; vel.z = rnd(-maxspeed, maxspeed); return vel; } void Asteroids2DStone::tick(float dt) { SUPER(Asteroids2DStone, tick, dt); Vector3 pos = this->getPosition(); Vector3 vel = this->getVelocity(); //ensure that the stone bounces from the playing field borders if(pos.x > width/2 || pos.x < -width/2) vel.x = -vel.x; if(pos.z > height/2 || pos.z < -height/2) vel.z = -vel.z; this->setVelocity(vel); //2D movement, position should always = 0 on y-axis if(pos.y!=0) pos.y = 0; this->setPosition(pos); } inline bool Asteroids2DStone::collidesAgainst(WorldEntity* otherObject, const btCollisionShape* ownCollisionShape, btManifoldPoint& contactPoint) { if(orxonox_cast(otherObject)) { split(); } return false; } void Asteroids2DStone::damage(float damage, float healthdamage, float shielddamage, Pawn* originator, const btCollisionShape* cs) { if (getGame() && orxonox_cast(originator) != nullptr) getGame()->addPoints(100); split(); //Pawn::damage(damage, healthdamage, shielddamage, originator, cs); } Asteroids2D* Asteroids2DStone::getGame() { if (game == nullptr) { for (Asteroids2D* asteroids : ObjectList()) game = asteroids; } return game; } void Asteroids2DStone::split() { //orxout() << "split" << endl; if(size == 3) { Asteroids2DStone* newStone1 = new Asteroids2DStone(this->getContext(), 2, this->getPosition()); newStone1->addTemplate("stone2"); Asteroids2DStone* newStone2 = new Asteroids2DStone(this->getContext(), 2, this->getPosition()); newStone2->addTemplate("stone2"); }else if(size == 2){ //Templates can be found in data/levels/templates/asteroidsAsteroids2D.oxt Asteroids2DStone* newStone1 = new Asteroids2DStone(this->getContext(), 1, this->getPosition()); newStone1->addTemplate("stone1"); Asteroids2DStone* newStone2 = new Asteroids2DStone(this->getContext(), 1, this->getPosition()); newStone2->addTemplate("stone1"); } this->death(); } }