Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8537 was 8537, checked in by catherine, 13 years ago

Almost working.

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