Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/OrxoBlox_FS19/src/modules/OrxoBlox/OrxoBlox.cc @ 12366

Last change on this file since 12366 was 12366, checked in by ahuwyler, 5 years ago

Here and there

File size: 8.7 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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file OrxoBlox.cc
31    @brief Implementation of the OrxoBlox class.
32*/
33
34#include "OrxoBlox.h"
35#include "Highscore.h"
36
37#include "core/CoreIncludes.h"
38#include "core/EventIncludes.h"
39#include "core/command/Executor.h"
40
41
42#include "core/config/ConfigValueIncludes.h"//Remove??
43
44#include "gamestates/GSLevel.h"
45
46
47#include "chat/ChatManager.h"//Remove?
48
49#include "OrxoBloxCenterpoint.h"
50#include "OrxoBloxBall.h"
51#include "OrxoBloxStones.h"
52#include "OrxoBloxWall.h"
53#include "OrxoBloxShip.h"
54
55namespace orxonox
56{
57   
58
59    RegisterUnloadableClass(OrxoBlox);
60
61    /**
62    @brief
63        Constructor. Registers and initializes the object.
64    */
65    OrxoBlox::OrxoBlox(Context* context) : Deathmatch(context)
66    {
67        RegisterObject(OrxoBlox);
68
69        this->center_ = nullptr;
70        this->ball_ = nullptr;
71        this->futureWall_ = nullptr;
72        this->player_ = nullptr;
73        level_ = 0;
74
75        this->setHUDTemplate("OrxoBloxHUD");
76        //Error when specified
77
78        // Pre-set the timer, but don't start it yet.
79        this->starttimer_.setTimer(1.0, false, createExecutor(createFunctor(&OrxoBlox::startBall, this)));
80        this->starttimer_.stopTimer();
81
82       
83
84    }
85
86    /**
87    @brief
88        Destructor. Cleans up, if initialized.
89    */
90    OrxoBlox::~OrxoBlox()
91    {
92        if (this->isInitialized())
93            this->cleanup();
94    }
95
96    /**
97    @brief
98        Cleans up the Gametype by destroying the ball and the bats.
99    */
100    void OrxoBlox::cleanup()
101    {
102        if (this->ball_ != nullptr) // Destroy the ball, if present.
103        {
104            this->ball_->destroy();
105            this->ball_ = nullptr;
106        }
107
108        if (this->futureWall_)
109            {
110            this->futureWall_->destroy();
111            this->futureWall_ = nullptr;
112            }
113
114        for (OrxoBloxWall* wall : this->activeWalls_)
115            wall->destroy();
116        this->activeWalls_.clear();
117       
118
119        for (OrxoBloxStones* stone : this->stones_)
120            stone->destroy();
121        this->stones_.clear();
122       
123
124    }
125
126    /**
127    @brieftt   
128        Starts the OrxoBlox minigame.
129    */
130    void OrxoBlox::start()
131    {
132        if (this->center_ != nullptr) // There needs to be a OrxoBloxCenterpoint, i.e. the area the game takes place.
133        {
134            if (this->ball_ == nullptr) // If there is no ball, create a new ball.
135            {
136                this->ball_ = new OrxoBloxBall(this->center_->getContext());
137                // Apply the template for the ball specified by the centerpoint.
138                this->ball_->addTemplate(this->center_->getBalltemplate());
139            }
140
141            // Attach the ball to the centerpoint and set the parameters as specified in the centerpoint, the ball is attached to.
142            this->center_->attach(this->ball_);
143            //Startposition Ball
144            this->ball_->setPosition(0, 0, 40);
145            this->ball_->setFieldDimension(this->center_->getFieldDimension());
146            this->ball_->setSpeed(0);
147            this->ball_->setAccelerationFactor(this->center_->getBallAccelerationFactor());
148           
149            level_=1;
150
151            // Create the first Wall.
152            this->LevelUp();
153
154            //Create Ship
155            //this->ship_ = new OrxoBloxShip(this->center_->getContext());
156            //this->ship_->setPosition(0, 0, 0);
157
158        }
159        else // If no centerpoint was specified, an error is thrown and the level is exited.
160        {
161            orxout(internal_error) << "OrxoBlox: No Centerpoint specified." << endl;
162            GSLevel::startMainMenu();
163            return;
164        }
165
166        // Start the timer. After it has expired the ball is started.
167        this->starttimer_.startTimer();
168
169        // Set variable to temporarily force the player to spawn.
170        // Set variable to temporarily force the player to spawn.
171        bool temp = this->bForceSpawn_;
172        this->bForceSpawn_ = true;
173
174        // Call start for the parent class.
175        Deathmatch::start();
176
177        // Reset the variable.
178        this->bForceSpawn_ = temp;
179
180    }
181
182    /**
183    @brief
184        Ends the OrxoBlox minigame.
185    */
186    void OrxoBlox::end()
187    {
188        ChatManager::message("You suck!!");
189
190        if (Highscore::exists())
191        {
192            int score = this->getScore(this->getPlayer());
193            Highscore::getInstance().storeScore("Tetris", score, this->getPlayer());
194        }
195
196        this->cleanup();
197
198        // Call end for the parent class.
199        Deathmatch::end();
200        GSLevel::startMainMenu();
201    }
202
203    PlayerInfo* OrxoBlox::getPlayer()
204    {
205        return this->player_;
206    }
207   
208    void OrxoBlox::spawnPlayer(PlayerInfo* player)
209    {
210        assert(player);
211
212        if(this->player_ == nullptr)
213        {
214            this->player_ = player;
215            this->players_[player].state_ = PlayerState::Alive;
216        }
217
218    }
219
220    void OrxoBlox::LevelUp(){
221        level_++;
222        this->playerScored(this->player_);// add points
223        for(OrxoBloxStones* stone : this->stones_){
224            int x_=(stone->getPosition()).x;
225            int y_=(stone->getPosition()).y;
226            int z_=(stone->getPosition()).z;
227            if(z_==90)this->end();
228
229            stone->setPosition(x_,y_,z_+9.0f);
230        }
231
232
233        this->createWall();
234        this->activeWalls_.push_back(this->futureWall_);
235        for (int i = 0; i < this->futureWall_->getNumberOfStones(); i++) {
236            if (this->futureWall_->getStone(i) == nullptr) {
237                orxout() << "Added nullptr to std::list stones_" << endl;
238            }
239
240            this->stones_.push_back(this->futureWall_->getStone(i));
241        }
242
243        for(OrxoBloxWall* wall : this->activeWalls_) {
244            if(wall->isEmpty()) {
245                wall->destroy();
246            }
247            int NumberOfStones = 0;
248            for(int i = 0; i < 10; i++) {
249                if(wall->getStone(i) == nullptr) {
250                    continue;
251                }
252                else {
253                    NumberOfStones++;
254                }
255
256            }
257        }
258        //new location of ship
259        //new amount of balls
260        //create balls
261        //insert new wall
262    }
263
264    void OrxoBlox::createWall(){
265        this->futureWall_ = new OrxoBloxWall(this->center_->getContext());
266        // Apply the stone template to the stone.
267        this->futureWall_->addTemplate(this->center_->getWallTemplate());
268
269        // Attach the brick to the Centerpoint and set the position of the brick to be at the left side.
270        this->center_->attach(this->futureWall_);
271       
272        this->futureWall_->setPosition(0, 0, 0);
273        this->futureWall_->setGame(this);
274    }
275
276
277    /**
278    @brief
279        Starts the ball with some default speed.
280    */
281    void OrxoBlox::startBall()
282    {
283        if (this->ball_ != nullptr && this->center_ != nullptr)
284            this->ball_->setSpeed(this->center_->getBallSpeed());
285    }
286
287    OrxoBloxStones* OrxoBlox::CheckForCollision(OrxoBloxBall* Ball) {
288
289        orxout() << "Checking for Collision" << endl;
290        Vector3 BallPosition = Ball->getPosition();
291        for(OrxoBloxStones* someStone : this->stones_)
292        {
293            if(someStone == nullptr) 
294            {
295                continue;
296            }
297            orxout() << "Checking a stone" << endl;
298            const Vector3& StonePosition = someStone->getPosition(); //!< Saves the position of the currentStone
299            int size = someStone->getSize()/2;
300            if((BallPosition.x - Ball->getRadius() >= StonePosition.x - size && BallPosition.x + Ball->getRadius() <= StonePosition.x + size) && 
301                (BallPosition.z - Ball->getRadius() >= StonePosition.z - size && BallPosition.z + Ball->getRadius() <= StonePosition.z + size)) {
302                orxout() << "FOUND ONE" << endl;
303                return someStone;
304            }
305        }
306        orxout() << "Found nothing...." << endl;
307        return nullptr;
308    }
309   
310}
Note: See TracBrowser for help on using the repository browser.