Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/asteroids2D/Asteroids2DStone.cc @ 11801

Last change on this file since 11801 was 11782, checked in by landauf, 7 years ago

tabs → spaces

  • Property svn:eol-style set to native
File size: 4.8 KB
Line 
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:
22 *      Viviane Yang
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28
29
30/**
31    @file Asteroids2DStone.cc
32
33    @brief Implementation of the Asteroids2DStone class.
34
35    TO DO: 
36    - when to split? It does not work if you override kill() function...->damage() function?
37*/ 
38
39#include "Asteroids2DStone.h"
40#include "Asteroids2D.h"
41#include "Asteroids2DShip.h"
42#include "core/CoreIncludes.h"
43#include "util/Math.h"
44
45namespace orxonox
46{
47    RegisterClass(Asteroids2DStone);
48
49    Asteroids2DStone::Asteroids2DStone(Context* context) : Pawn(context)
50    {
51        RegisterObject(Asteroids2DStone);
52        this->size = rand()%3+1;
53        this->width = 1043;
54        this->height = 646;
55        this->setPosition(randomPosition(this->width, this->height));
56        this->setCollisionType(WorldEntity::CollisionType::Dynamic);
57        this->setVelocity(randomVelocity(MAX_SPEED));
58    }
59
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);
67        this->setCollisionType(WorldEntity::CollisionType::Dynamic);
68        this->setVelocity(randomVelocity(MAX_SPEED));
69    }
70
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;
83        vel.x = rnd(-maxspeed, maxspeed);
84        vel.y = 0;
85        vel.z = rnd(-maxspeed, maxspeed);
86        return vel;
87    }
88
89
90    void Asteroids2DStone::tick(float dt)
91    {
92        SUPER(Asteroids2DStone, tick, dt);
93        Vector3 pos = this->getPosition();
94        Vector3 vel = this->getVelocity();
95
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);
100
101        //2D movement, position should always = 0 on y-axis
102        if(pos.y!=0) pos.y = 0;
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))
109        {
110            split();
111        }
112        return false;
113    }
114
115    void Asteroids2DStone::damage(float damage, float healthdamage, float shielddamage, Pawn* originator, const btCollisionShape* cs)
116    {
117        if (getGame() && orxonox_cast<Asteroids2DShip*>(originator) != nullptr)
118            getGame()->addPoints(100);
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
133    void Asteroids2DStone::split()
134    {
135        //orxout() << "split" << endl;
136        if(size == 3)
137        {
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");
142
143        }else if(size == 2){
144
145
146            //Templates can be found in data/levels/templates/asteroidsAsteroids2D.oxt
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");
151        }   
152        this->death();
153    }
154
155}
Note: See TracBrowser for help on using the repository browser.