Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8566 was 8566, checked in by dafrick, 14 years ago

Fixing another bug.

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