Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Some simplification.

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