Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Works now.

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