Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pCuts/src/modules/tetris/TetrisBrick.cc @ 9086

Last change on this file since 9086 was 9086, checked in by jo, 13 years ago

Tetris contais almost the same features as before introducing bricks. Two bugs are still left. And the clear row feature has to be implemented.

File size: 8.3 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 TetrisBrick.cc
31    @brief Implementation of the TetrisBrick class.
32*/
33
34#include "TetrisBrick.h"
35
36#include "core/CoreIncludes.h"
37#include "core/XMLPort.h"
38
39#include "TetrisCenterpoint.h"
40#include "TetrisStone.h"
41#include "Tetris.h"
42
43namespace orxonox
44{
45    CreateFactory(TetrisBrick);
46
47    /**
48    @brief
49        Constructor. Registers and initializes the object.
50    */
51    TetrisBrick::TetrisBrick(BaseObject* creator): ControllableEntity(creator)
52    {
53        RegisterObject(TetrisBrick);
54
55        this->shapeIndex_ = 4; //<! TODO: random number between 0 and 7
56        this->stonesPerBrick_ = 4; //<! most tetris bricks is formed by 4 stones
57        this->delay_ = false;
58        this->delayTimer_.setTimer(0.2f, false, createExecutor(createFunctor(&TetrisBrick::enableMovement, this)));
59        this->lockRotation_ = false;
60        this->tetris_ = this->getTetris();
61        this->size_ = 10.0f; //TODO: fix this via this->tetris_->center_->getStoneSize();
62        this->rotationCount_ = 0;
63        this->createBrick(); //<! create a whole new Brick;
64    }
65
66    /**
67    @brief
68        This function partly initializes a TetrisBrick as an array of TetrisStones
69    */
70    void TetrisBrick::createBrick(void)
71    { //Index 0 : single stone, 1 : 4 in a row; 2: 4-Block right shifted; 3: 'T' 4: 4-Block left shifted;
72      //Index 5 : 4-Block; 6 : 'L'; 7 : mirrored 'L';
73        orxout()<< "TetrisBrick::createBrick" << endl;
74        if(this->shapeIndex_ == 0)
75            this->stonesPerBrick_ = 1;
76        for (unsigned int i = 0; i < this->stonesPerBrick_; i++)
77        {
78            // Create a new stone and add it to the brick.
79            TetrisStone* stone = new TetrisStone(this);
80            stone->setHealth(1.0f);
81            this->brickStones_.push_back(stone);
82            this->attach(stone);
83            this->formBrick(stone, i);
84            if(this->tetris_ != NULL)
85            {
86                stone->setGame(this->tetris_);
87                if(this->tetris_->getCenterpoint() != NULL)
88                    stone->addTemplate(this->tetris_->getCenterpoint()->getStoneTemplate());
89                else
90                        orxout()<< "tetris_->getCenterpoint == NULL in TetrisBrick.cc"<< endl;
91            }
92            else
93                orxout()<< "tetris_ == NULL in TetrisBrick.cc"<< endl;
94        }
95    }
96
97    /**
98    @brief
99        This function creates the shape of a TetrisBrick. ! Spaghetti-Code !
100    @param i
101        The stone's number.
102    @param stone
103        The TetrisStone that is placed relative to the brick's position.
104    */
105    void TetrisBrick::formBrick(TetrisStone* stone, unsigned int i)
106    {
107        if(i != 0 && this->shapeIndex_ == 0)
108            orxout() << "So it has come to this in TetrisBrick.cc"<< endl;
109        if(i == 0) //setting the first stone as
110        {
111            stone->setPosition(0.0f, 0.0f, 0.0f);
112        }
113        else if(i == 1)
114        {
115            stone->setPosition(0.0f, size_, 0.0f);
116        }
117        else if(i == 2)
118        {
119            if(this->shapeIndex_ == 1 || this->shapeIndex_ == 6 || this->shapeIndex_ == 7)
120            {
121                stone->setPosition(0.0f, 2*size_, 0.0f);
122            }
123            else if(this->shapeIndex_ == 3 || this->shapeIndex_ == 4|| this->shapeIndex_ == 5)
124            {
125                stone->setPosition(size_, 0, 0.0f);
126            }
127            else if(this->shapeIndex_ == 2)
128            {
129                stone->setPosition(-size_, 0, 0.0f);
130            }
131        }
132        else if(i == 3)
133        {
134            if(this->shapeIndex_ == 2 || this->shapeIndex_ == 5)
135            {
136                stone->setPosition(size_, size_, 0.0f);
137            }
138            else if(this->shapeIndex_ == 1)
139            {
140                stone->setPosition(0, 3*size_, 0.0f);
141            }
142            else if(this->shapeIndex_ == 3 || this->shapeIndex_ == 7)
143            {
144                stone->setPosition(-size_, 0, 0.0f);
145            }
146            else if(this->shapeIndex_ == 4)
147            {
148                stone->setPosition(-size_, size_, 0.0f);
149            }
150            else if(this->shapeIndex_ == 6)
151            {
152                stone->setPosition(size_, 0, 0.0f);
153            }
154        }
155    }
156
157    bool TetrisBrick::isValidMove(const Vector3& position, bool isRotation = false)
158    {
159        return this->tetris_->isValidMove(this,position, isRotation);
160    }
161
162    TetrisStone* TetrisBrick::getStone(unsigned int i)
163    {
164        if(i < this->brickStones_.size())
165            return this->brickStones_[i];
166        else return NULL;
167    }
168
169
170    Tetris* TetrisBrick::getTetris()
171    {
172        if (this->getGametype() != NULL && this->getGametype()->isA(Class(Tetris)))
173        {
174            Tetris* tetrisGametype = orxonox_cast<Tetris*>(this->getGametype().get());
175            return tetrisGametype;
176        }
177        return NULL;
178    }
179//TODO: refactor this function; is not needed if brickstones are added to Tetris::stones_ after collision.
180    bool TetrisBrick::contains(TetrisStone* stone)
181    {
182        for(unsigned int i = 0; i < brickStones_.size(); i++)
183        {
184            if(stone == brickStones_[i])
185                return true;
186        }
187        return false;
188    }
189
190    /**
191    @brief
192        Overloaded the function to rotate the Brick.
193    @param value
194        A vector whose first component is the angle by which to rotate.
195    */
196    void TetrisBrick::moveFrontBack(const Vector2& value)
197    {
198        if(value.x < 0) //speedup on key down
199        {
200            this->setVelocity(this->getVelocity()*1.1);
201        }
202        else if(!this->lockRotation_) //rotate when key up is pressed
203        {
204                if(!isValidMove(this->getPosition(), true)) //catch illegal rotations
205                    return;
206            this->lockRotation_ = true; // multiple calls of this function have to be filtered out.
207            this->rotationTimer_.setTimer(0.1f, false, createExecutor(createFunctor(&TetrisBrick::unlockRotation, this)));
208            Quaternion q(Degree(90), Vector3::UNIT_Z);
209            this->setOrientation(this->getOrientation()*q); //rotation: roll 90°
210            this->rotationCount_ = (this->rotationCount_ + 1) % 4;
211        }
212    }
213
214    /**
215    @brief
216        Overloaded the function to steer the Brick right and left
217    @param value
218        A vector whose first component is the direction in which we want to steer the Brick.
219    */
220    void TetrisBrick::moveRightLeft(const Vector2& value)
221    {
222        if(!this->delay_)
223        {
224            const Vector3& position = this->getPosition();
225            Vector3 newPos = Vector3(position.x+value.x/abs(value.x)*this->size_, position.y, position.z);
226            if(!this->isValidMove(newPos))
227                return;
228
229            this->setPosition(newPos);
230            this->delay_ = true;
231            this->delayTimer_.startTimer();
232        }
233    }
234
235    /**
236    @brief
237        Is called when the player changed.
238    */
239    void TetrisBrick::changedPlayer()
240    {
241        this->setVelocity(0.0f, 0.0f, 0.0f);
242    }
243
244    /**
245    @brief
246        Attaches stones to the Centerpoint.
247    */
248    void TetrisBrick::releaseStones(TetrisCenterpoint* center)
249    {
250        assert(this->tetris_);
251        for(unsigned int i = 0; i < brickStones_.size(); i++)
252        {
253            this->brickStones_[i]->detachFromParent();
254            this->brickStones_[i]->attachToParent(center);
255            this->brickStones_[i]->setPosition(this->getPosition()+this->tetris_->rotateVector(this->brickStones_[i]->getPosition(),this->rotationCount_ ));
256        }
257
258    }
259
260}
Note: See TracBrowser for help on using the repository browser.