Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tetris/src/modules/tetris/Tetris.cc @ 8249

Last change on this file since 8249 was 8249, checked in by dafrick, 13 years ago

Re-adding tetris module.

File size: 6.2 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 *      ...
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file Tetris.cc
31    @brief Implementation of the Tetris class.
32*/
33
34#include "Tetris.h"
35
36#include "core/CoreIncludes.h"
37#include "core/EventIncludes.h"
38#include "core/command/Executor.h"
39
40#include "gamestates/GSLevel.h"
41
42#include "TetrisCenterpoint.h"
43#include "TetrisStone.h"
44#include "infos/PlayerInfo.h"
45
46namespace orxonox
47{
48
49    CreateUnloadableFactory(Tetris);
50
51    /**
52    @brief
53        Constructor. Registers and initializes the object.
54    */
55    Tetris::Tetris(BaseObject* creator) : Deathmatch(creator)
56    {
57        RegisterObject(Tetris);
58
59        this->activeStone_ = NULL;
60
61        // Pre-set the timer, but don't start it yet.
62        this->starttimer_.setTimer(1.0, false, createExecutor(createFunctor(&Tetris::startStone, this)));
63        this->starttimer_.stopTimer();
64    }
65
66    /**
67    @brief
68        Destructor. Cleans up, if initialized.
69    */
70    Tetris::~Tetris()
71    {
72        if (this->isInitialized())
73            this->cleanup();
74    }
75
76    /**
77    @brief
78        Cleans up the Gametype.
79    */
80    void Tetris::cleanup()
81    {
82       
83    }
84   
85    void Tetris::tick(float dt)
86    {
87        SUPER(Tetris, tick, dt);
88       
89        TetrisStone* stone = this->activeStone_;
90        if(stone != NULL)
91        {
92            Vector3 position = stone->getPosition();
93            if(position.x < this->center_->getStoneSize()/2.0)
94                position.x = this->center_->getStoneSize()/2.0;
95            else if(position.x > (this->center_->getWidth()-0.5)*this->center_->getStoneSize())
96                position.x = (this->center_->getWidth()-0.5)*this->center_->getStoneSize();
97           
98            if(position.y < this->center_->getStoneSize()/2.0)
99            {
100                position.y = this->center_->getStoneSize()/2.0;
101                stone->setVelocity(Vector3::ZERO);
102                this->createStone();
103                this->startStone();
104            }
105           
106            stone->setPosition(position);
107        }
108    }
109
110    /**
111    @brief
112        Starts the Tetris minigame.
113    */
114    void Tetris::start()
115    {
116        if (this->center_ != NULL) // There needs to be a TetrisCenterpoint, i.e. the area the game takes place.
117        {
118            // Create the first stone.
119            this->createStone();
120        }
121        else // If no centerpoint was specified, an error is thrown and the level is exited.
122        {
123            COUT(1) << "Error: No Centerpoint specified." << std::endl;
124            GSLevel::startMainMenu();
125            return;
126        }
127
128        // Start the timer. After it has expired the ball is started.
129        this->starttimer_.startTimer();
130
131        // Set variable to temporarily force the player to spawn.
132        bool temp = this->bForceSpawn_;
133        this->bForceSpawn_ = true;
134
135        // Call start for the parent class.
136        Deathmatch::start();
137
138        // Reset the variable.
139        this->bForceSpawn_ = temp;
140    }
141
142    /**
143    @brief
144        Ends the Tetris minigame.
145    */
146    void Tetris::end()
147    {
148        this->cleanup();
149
150        // Call end for the parent class.
151        Deathmatch::end();
152    }
153
154    /**
155    @brief
156        Spawns player.
157    */
158    void Tetris::spawnPlayersIfRequested()
159    {
160        // Spawn a human player.
161        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
162            if (it->first->isHumanPlayer() && (it->first->isReadyToSpawn() || this->bForceSpawn_))
163                this->spawnPlayer(it->first);
164    }
165
166    /**
167    @brief
168        Spawns the input player.
169    @param player
170        The player to be spawned.
171    */
172    void Tetris::spawnPlayer(PlayerInfo* player)
173    {
174        assert(player);
175
176        if(this->player_ != NULL)
177        {
178            this->player_ = player;
179            this->players_[player].state_ = PlayerState::Alive;
180        }
181    }
182
183    /**
184    @brief
185        Starts the first stone.
186    */
187    void Tetris::startStone(void)
188    {
189        if(this->player_ == NULL)
190            return;
191       
192        if(this->activeStone_ != NULL)
193            this->player_->stopControl();
194       
195        // Make the last stone to be created the active stone.
196        this->activeStone_ = this->stones_.back();
197       
198        this->player_->startControl(this->activeStone_);
199        this->activeStone_->setVelocity(0.0f, -this->center_->getStoneSpeed(), 0.0f);
200    }
201
202    /**
203    @brief
204        Creates a new stone.
205    */
206    void Tetris::createStone(void)
207    {
208        // Create a new stone and add it to the list of stones.
209        TetrisStone* stone = new TetrisStone(this->center_);
210        this->stones_.push_back(stone);
211       
212        // Apply the stone template to the stone.
213        stone->addTemplate(this->center_->getStoneTemplate());
214       
215        // Attach the stone to the Centerpoint and set the position of the stone to be at the top middle.
216        this->center_->attach(stone);
217        float xPos = (this->center_->getWidth()/2 + ((this->center_->getWidth() % 2)*2-1)/2.0)*this->center_->getStoneSize();
218        float yPos = (this->center_->getHeight()-0.5)*this->center_->getStoneSize();
219        stone->setPosition(xPos, yPos, 0.0f);
220    }
221
222    /**
223    @brief
224        Get the player.
225    @return
226        Returns a pointer to the player. If there is no player, NULL is returned.
227    */
228    PlayerInfo* Tetris::getPlayer(void) const
229    {
230        return this->player_;
231    }
232
233}
Note: See TracBrowser for help on using the repository browser.