Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/Asteroid_HS17/src/modules/asteroids2D/Asteroids2DStone.cc @ 11669

Last change on this file since 11669 was 11669, checked in by vyang, 7 years ago

Kommentare

File size: 4.7 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    - 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
40#include "Asteroids2DStone.h"
41#include "Asteroids2D.h"
42#include "Asteroids2DShip.h"
43#include "core/CoreIncludes.h"
44#include "util/Math.h"
45
46namespace orxonox
47{
48    RegisterClass(Asteroids2DStone);
49
50    Asteroids2DStone::Asteroids2DStone(Context* context) : Pawn(context)
51    {
52        RegisterObject(Asteroids2DStone);
53        this->size = rand()%3+1;
54        this->width = 1043;
55        this->height = 646;
56        this->setPosition(randomPosition(this->width, this->height));
57        this->setCollisionTypeStr("dynamic");
58        this->setVelocity(randomVelocity(MAX_SPEED));
59    }
60
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
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();
95
96
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))
114        {
115            split();
116        }
117        return false;
118    }
119
120    void Asteroids2DStone::damage(float damage, float healthdamage, float shielddamage, Pawn* originator, const btCollisionShape* cs)
121    {
122        if (getGame() && orxonox_cast<Asteroids2DShip*>(originator) != nullptr)
123            getGame()->addPoints(100);
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
138    void Asteroids2DStone::split()
139    {
140        //orxout() << "split" << endl;
141        if(size == 3)
142        {
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");
147
148        }else if(size == 2){
149
150
151            //Templates can be found in data/levels/templates/asteroidsAsteroids2D.oxt
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");
156        }   
157        this->death();
158    }
159
160}
Note: See TracBrowser for help on using the repository browser.