Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Fixing bug in tetris.

File size: 8.8 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        this->player_ = NULL;
66    }
67
68    /**
69    @brief
70        Destructor. Cleans up, if initialized.
71    */
72    Tetris::~Tetris()
73    {
74        if (this->isInitialized())
75            this->cleanup();
76    }
77
78    /**
79    @brief
80        Cleans up the Gametype.
81    */
82    void Tetris::cleanup()
83    {
84
85    }
86
87    void Tetris::tick(float dt)
88    {
89        SUPER(Tetris, tick, dt);
90
91        if(this->activeStone_ != NULL)
92        {
93            std::pair<bool, TetrisStone*> valid = this->isValidStonePosition(this->activeStone_, this->activeStone_->getPosition());
94            if(!valid.first)
95            {
96                this->activeStone_->setVelocity(Vector3::ZERO);
97                if(valid.second != NULL)
98                {
99                    Vector3 position = Vector3(this->activeStone_->getPosition().x, valid.second->getPosition().y+this->center_->getStoneSize(), this->activeStone_->getPosition().z);
100                    this->activeStone_->setPosition(position);
101                }
102                this->createStone();
103                this->startStone();
104            }
105        }
106    }
107
108    std::pair<bool, TetrisStone*> Tetris::isValidMove(TetrisStone* stone, const Vector3& position)
109    {
110        assert(stone);
111
112        std::pair<bool, TetrisStone*> valid = std::pair<bool, TetrisStone*>(true, NULL);
113       
114        if(position.x < this->center_->getStoneSize()/2.0)  //!< If the stone touches the left edge of the level
115            valid.first = false;
116        else if(position.x > (this->center_->getWidth()-0.5)*this->center_->getStoneSize()) //!< If the stone touches the right edge of the level
117            valid.first = false;
118
119        for(std::vector<TetrisStone*>::const_iterator it = this->stones_.begin(); it != this->stones_.end(); ++it)
120        {
121            if(stone == *it)
122                continue;
123
124            const Vector3& currentStonePosition = (*it)->getPosition(); //!< Saves the position of the currentStone
125
126            if((position.x == currentStonePosition.x) && (position.y == currentStonePosition.y))
127            {
128                stone->setVelocity(Vector3::ZERO);
129                this->createStone();
130                this->startStone();
131                valid.first = false;
132                return valid;
133            }// This case applies if the stones overlap completely
134        }
135
136        return valid;
137    }
138
139    std::pair<bool, TetrisStone*> Tetris::isValidStonePosition(TetrisStone* stone, const Vector3& position)
140    {
141        assert(stone);
142
143        std::pair<bool, TetrisStone*> valid = std::pair<bool, TetrisStone*>(true, NULL);
144
145        if(position.y < this->center_->getStoneSize()/2.0) //!< If the stone has reached the bottom of the level
146        {
147            valid.first = false;
148            stone->setPosition(Vector3(stone->getPosition().x, this->center_->getStoneSize()/2.0, stone->getPosition().z));
149        }
150
151        for(std::vector<TetrisStone*>::const_iterator it = this->stones_.begin(); it != this->stones_.end(); ++it)
152        {
153            if(stone == *it)
154                continue;
155
156            const Vector3& currentStonePosition = (*it)->getPosition(); //!< Saves the position of the currentStone
157
158            if((position.x == currentStonePosition.x) && (position.y < currentStonePosition.y + this->center_->getStoneSize()))
159            {
160                valid.first = false;
161                valid.second = *it;
162                return valid;
163            }// This case applies if the stones overlap partially vertically
164        }
165
166        return valid;
167    }
168
169    /**
170    @brief
171        Starts the Tetris minigame.
172    */
173    void Tetris::start()
174    {
175        if (this->center_ != NULL) // There needs to be a TetrisCenterpoint, i.e. the area the game takes place.
176        {
177            // Create the first stone.
178            this->createStone();
179        }
180        else // If no centerpoint was specified, an error is thrown and the level is exited.
181        {
182            COUT(1) << "Error: No Centerpoint specified." << std::endl;
183            GSLevel::startMainMenu();
184            return;
185        }
186
187        // Start the timer. After it has expired the stone is started.
188        this->starttimer_.startTimer();
189
190        // Set variable to temporarily force the player to spawn.
191        bool temp = this->bForceSpawn_;
192        this->bForceSpawn_ = true;
193
194        // Call start for the parent class.
195        Deathmatch::start();
196
197        // Reset the variable.
198        this->bForceSpawn_ = temp;
199    }
200
201    /**
202    @brief
203        Ends the Tetris minigame.
204    */
205    void Tetris::end()
206    {
207        this->cleanup();
208
209        // Call end for the parent class.
210        Deathmatch::end();
211    }
212
213    /**
214    @brief
215        Spawns player.
216    */
217    void Tetris::spawnPlayersIfRequested()
218    {
219        // Spawn a human player.
220        for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it)
221            if (it->first->isHumanPlayer() && (it->first->isReadyToSpawn() || this->bForceSpawn_))
222                this->spawnPlayer(it->first);
223    }
224
225    /**
226    @brief
227        Spawns the input player.
228    @param player
229        The player to be spawned.
230    */
231    void Tetris::spawnPlayer(PlayerInfo* player)
232    {
233        assert(player);
234
235        if(this->player_ == NULL)
236        {
237            this->player_ = player;
238            this->players_[player].state_ = PlayerState::Alive;
239        }
240    }
241
242    /**
243    @brief
244        Starts the first stone.
245    */
246    void Tetris::startStone(void)
247    {
248        if(this->player_ == NULL)
249            return;
250       
251        if(this->activeStone_ != NULL)
252            this->player_->stopControl();
253       
254        // Make the last stone to be created the active stone.
255        this->activeStone_ = this->stones_.back();
256       
257        this->player_->startControl(this->activeStone_);
258        this->activeStone_->setVelocity(0.0f, -this->center_->getStoneSpeed(), 0.0f);
259    }
260
261    /**
262    @brief
263        Creates a new stone.
264    */
265    void Tetris::createStone(void)
266    {
267        // Create a new stone and add it to the list of stones.
268        TetrisStone* stone = new TetrisStone(this->center_);
269        this->stones_.push_back(stone);
270       
271        // Apply the stone template to the stone.
272        stone->addTemplate(this->center_->getStoneTemplate());
273       
274        // Attach the stone to the Centerpoint and set the position of the stone to be at the top middle.
275        this->center_->attach(stone);
276        float xPos = (this->center_->getWidth()/2 + ((this->center_->getWidth() % 2)*2-1)/2.0)*this->center_->getStoneSize();
277        float yPos = (this->center_->getHeight()-0.5)*this->center_->getStoneSize();
278        stone->setPosition(xPos, yPos, 0.0f);
279        stone->setGame(this);
280    }
281
282    /**
283    @brief
284        Get the player.
285    @return
286        Returns a pointer to the player. If there is no player, NULL is returned.
287    */
288    PlayerInfo* Tetris::getPlayer(void) const
289    {
290        return this->player_;
291    }
292
293    /**
294    @brief Set the TetrisCenterpoint (the playing field).
295    @param center A pointer to the TetrisCenterpoint to be set.
296    */
297    void Tetris::setCenterpoint(TetrisCenterpoint* center)
298    {
299        this->center_ = center;
300    }
301
302}
Note: See TracBrowser for help on using the repository browser.