Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/Asteroid_HS17/src/modules/asteroids2D/Asteroids2D.cc @ 11668

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

Kommentare hinzugefuegt und Projektile in die richtige Richtung ausgerichtet.

File size: 6.4 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Viviane Yang
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29
30
31/**
32    @file Asteroids2D.cc
33    @brief Implementation of the Asteroids2D class.
34
35    TODO:
36        - Implement a counting system for the score
37        - HUD can be improved (display health, points, level etc.)
38        - Projectiles still fly in a mysterious direction
39        - Problem: The asteroids spawn randomly on the playing field.
40            During spawn or level up, there is the possibility that they pawn at the position of the ship.
41            ->spawn somewhere with a safty distance/radius
42
43*/
44
45#include "Asteroids2D.h"
46#include "Asteroids2DShip.h" // Necessary for getPlayer function. Do NOT include this in Header!
47#include "Asteroids2DStone.h"
48#include "core/CoreIncludes.h"
49#include "Highscore.h"
50
51namespace orxonox
52{
53    RegisterUnloadableClass(Asteroids2D);
54
55    Asteroids2D::Asteroids2D(Context* context) : Deathmatch(context)
56    {
57        RegisterObject(Asteroids2D);
58
59        bEndGame = false;
60        lives = 3; 
61        level = 1;
62        point = 0;
63        bShowLevel = false;
64        multiplier = 1;
65        b_combo = false;
66        firstTick_ = true;
67        this->numberOfBots_ = 0; //sets number of default bots temporarly to 0
68        this->center_ = nullptr;
69        this->setHUDTemplate("Asteroids2DHUD");
70        levelupTimer.setTimer(30.0f, true, createExecutor(createFunctor(&Asteroids2D::levelUp, this))); //level up every 30s
71    }
72
73
74
75    void Asteroids2D::levelUp()
76    {
77        level++;
78        if (getPlayer() != nullptr)
79        {
80
81            //Do something that indicates a level up
82            for (int i = 0; i < 7; i++)
83            {
84                WeakPtr<ExplosionPart> chunk5 = new ExplosionPart(this->center_->getContext());
85                chunk5->setPosition(Vector3(600, 0, 100.f * i - 300));
86                chunk5->setVelocity(Vector3(1000, 0, 0));  //player->getVelocity()
87                chunk5->setScale(10);
88                chunk5->setEffect1("Orxonox/explosion2b");
89                chunk5->setEffect2("Orxonox/smoke6");
90                chunk5->Explode();
91
92            }
93        }
94        addPoints(multiplier * 20);
95        multiplier *= 2;
96        toggleShowLevel();
97        showLevelTimer.setTimer(1.0f, false, createExecutor(createFunctor(&Asteroids2D::toggleShowLevel, this)));
98
99
100        //After level up -> spawn stones. Modify for different difficulty level
101        for(int i = 0; i < level*2; i++){
102            spawnStone();
103        }
104
105    }
106
107    void Asteroids2D::tick(float dt)
108    {
109        //Do this only for the first tick, generate 5 stones in the beginning
110        if(this->firstTick_)
111        {
112            getPlayer();
113            for(int i = 0; i < 5; ++i)
114            {
115                spawnStone();
116            }
117            //after first tick, firstTick_ will remain false
118            this->firstTick_ = false;
119        }
120       
121        SUPER(Asteroids2D, tick, dt);
122    }
123
124    void Asteroids2D::spawnStone()
125    {
126
127        //stones are created with a size -> second constructor in Asteroids2DStone class
128        Asteroids2DStone* newStone = new Asteroids2DStone(this->center_->getContext());
129        newStone->setAsteroids2DPlayer(player);
130
131        //look at templates in data/levels/templates/asteroidsAsteroids2D.oxt
132        switch(newStone->getSize()){
133            case 1:
134                newStone->addTemplate("stone1");
135                break;
136            case 2: 
137                newStone->addTemplate("stone2");
138                break;
139            case 3:
140                newStone->addTemplate("stone3");
141                break;
142            default:
143                orxout(internal_error) << "Asteroids2DStone has invalid size and cannot be created" << endl;
144                break;
145
146        }
147    }
148
149    Asteroids2DShip* Asteroids2D::getPlayer()
150    {
151        if (player == nullptr)
152        {
153            for (Asteroids2DShip* ship : ObjectList<Asteroids2DShip>())
154            {
155                player = ship;
156            }
157        }
158        return player;
159    }
160
161    void Asteroids2D::costLife()
162    {
163        --lives;
164        if(lives <= 0){
165            endGameTimer.setTimer(8.0f, false, createExecutor(createFunctor(&Asteroids2D::end, this)));           
166        }
167    };
168   
169    //The first function that will be called
170    void Asteroids2D::start()
171    {
172        //orxout() << "start" << endl;
173
174        // Set variable to temporarily force the player to spawn.
175        this->bForceSpawn_ = false;
176
177        if (this->center_ == nullptr)  // abandon mission!
178        {
179            orxout(internal_error) << "Asteroids2D: No Centerpoint specified." << endl;
180            GSLevel::startMainMenu();
181            return;
182        }
183        // Call start for the parent class.
184        Deathmatch::start();
185    }
186
187    void Asteroids2D::playerPreSpawn(PlayerInfo* player)
188    {
189        if(lives <= 0)
190        {
191            this->end();
192        }
193    }
194
195    //This function will be called from the ship or the stone class (if the stone was hit)
196    void Asteroids2D::addPoints(int numPoints)
197    {
198        if (!bEndGame)
199        {
200            point += numPoints * multiplier;
201            b_combo = true;
202        }
203    }
204
205    void Asteroids2D::end()
206    {
207        // DON'T CALL THIS!
208        //      Deathmatch::end();
209        // It will misteriously crash the game!
210        // Instead startMainMenu, this won't crash.
211        if (Highscore::exists()){
212                    int score = this->getPoints();
213                    if(score > Highscore::getInstance().getHighestScoreOfGame("Asteroids2D")) 
214                        Highscore::getInstance().storeHighscore("Asteroids2D",score);
215
216          }
217        GSLevel::startMainMenu();
218    }
219}
Note: See TracBrowser for help on using the repository browser.