Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Still a long way to go. A short todo list can be found at the beginning of Tetric.cc. A first, buggy clear-row function has been implemented.

File size: 8.2 KB
RevLine 
[9082]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"
[9087]42#include "util/Math.h"
[9082]43
44namespace orxonox
45{
46    CreateFactory(TetrisBrick);
47
48    /**
49    @brief
50        Constructor. Registers and initializes the object.
51    */
52    TetrisBrick::TetrisBrick(BaseObject* creator): ControllableEntity(creator)
53    {
54        RegisterObject(TetrisBrick);
[9087]55        this->shapeIndex_ = static_cast<unsigned int>(rnd(7.0f)); //<! random number between 0 and 7
[9082]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();
[9084]62        this->rotationCount_ = 0;
[9082]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);
[9084]80            stone->setHealth(1.0f);
[9082]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) //setting the first stone as
108        {
109            stone->setPosition(0.0f, 0.0f, 0.0f);
110        }
111        else if(i == 1)
112        {
113            stone->setPosition(0.0f, size_, 0.0f);
114        }
115        else if(i == 2)
116        {
117            if(this->shapeIndex_ == 1 || this->shapeIndex_ == 6 || this->shapeIndex_ == 7)
118            {
119                stone->setPosition(0.0f, 2*size_, 0.0f);
120            }
121            else if(this->shapeIndex_ == 3 || this->shapeIndex_ == 4|| this->shapeIndex_ == 5)
122            {
123                stone->setPosition(size_, 0, 0.0f);
124            }
125            else if(this->shapeIndex_ == 2)
126            {
127                stone->setPosition(-size_, 0, 0.0f);
128            }
129        }
130        else if(i == 3)
131        {
132            if(this->shapeIndex_ == 2 || this->shapeIndex_ == 5)
133            {
134                stone->setPosition(size_, size_, 0.0f);
135            }
136            else if(this->shapeIndex_ == 1)
137            {
138                stone->setPosition(0, 3*size_, 0.0f);
139            }
140            else if(this->shapeIndex_ == 3 || this->shapeIndex_ == 7)
141            {
142                stone->setPosition(-size_, 0, 0.0f);
143            }
144            else if(this->shapeIndex_ == 4)
145            {
146                stone->setPosition(-size_, size_, 0.0f);
147            }
148            else if(this->shapeIndex_ == 6)
149            {
150                stone->setPosition(size_, 0, 0.0f);
151            }
152        }
153    }
154
[9084]155    bool TetrisBrick::isValidMove(const Vector3& position, bool isRotation = false)
[9082]156    {
[9084]157        return this->tetris_->isValidMove(this,position, isRotation);
[9082]158    }
159
160    TetrisStone* TetrisBrick::getStone(unsigned int i)
161    {
162        if(i < this->brickStones_.size())
163            return this->brickStones_[i];
164        else return NULL;
165    }
166
167
168    Tetris* TetrisBrick::getTetris()
169    {
170        if (this->getGametype() != NULL && this->getGametype()->isA(Class(Tetris)))
171        {
172            Tetris* tetrisGametype = orxonox_cast<Tetris*>(this->getGametype().get());
173            return tetrisGametype;
174        }
175        return NULL;
176    }
[9086]177//TODO: refactor this function; is not needed if brickstones are added to Tetris::stones_ after collision.
[9082]178    bool TetrisBrick::contains(TetrisStone* stone)
179    {
180        for(unsigned int i = 0; i < brickStones_.size(); i++)
181        {
182            if(stone == brickStones_[i])
183                return true;
184        }
185        return false;
186    }
187
188    /**
189    @brief
190        Overloaded the function to rotate the Brick.
191    @param value
192        A vector whose first component is the angle by which to rotate.
193    */
194    void TetrisBrick::moveFrontBack(const Vector2& value)
195    {
196        if(value.x < 0) //speedup on key down
197        {
198            this->setVelocity(this->getVelocity()*1.1);
199        }
200        else if(!this->lockRotation_) //rotate when key up is pressed
201        {
[9084]202                if(!isValidMove(this->getPosition(), true)) //catch illegal rotations
203                    return;
[9082]204            this->lockRotation_ = true; // multiple calls of this function have to be filtered out.
205            this->rotationTimer_.setTimer(0.1f, false, createExecutor(createFunctor(&TetrisBrick::unlockRotation, this)));
206            Quaternion q(Degree(90), Vector3::UNIT_Z);
207            this->setOrientation(this->getOrientation()*q); //rotation: roll 90°
[9084]208            this->rotationCount_ = (this->rotationCount_ + 1) % 4;
[9082]209        }
210    }
211
212    /**
213    @brief
214        Overloaded the function to steer the Brick right and left
215    @param value
216        A vector whose first component is the direction in which we want to steer the Brick.
217    */
218    void TetrisBrick::moveRightLeft(const Vector2& value)
219    {
220        if(!this->delay_)
221        {
222            const Vector3& position = this->getPosition();
223            Vector3 newPos = Vector3(position.x+value.x/abs(value.x)*this->size_, position.y, position.z);
224            if(!this->isValidMove(newPos))
225                return;
226
227            this->setPosition(newPos);
228            this->delay_ = true;
229            this->delayTimer_.startTimer();
230        }
231    }
232
233    /**
234    @brief
235        Is called when the player changed.
236    */
237    void TetrisBrick::changedPlayer()
238    {
239        this->setVelocity(0.0f, 0.0f, 0.0f);
240    }
[9086]241
[9085]242    /**
243    @brief
244        Attaches stones to the Centerpoint.
245    */
246    void TetrisBrick::releaseStones(TetrisCenterpoint* center)
247    {
[9086]248        assert(this->tetris_);
[9085]249        for(unsigned int i = 0; i < brickStones_.size(); i++)
250        {
[9086]251            this->brickStones_[i]->detachFromParent();
252            this->brickStones_[i]->attachToParent(center);
253            this->brickStones_[i]->setPosition(this->getPosition()+this->tetris_->rotateVector(this->brickStones_[i]->getPosition(),this->rotationCount_ ));
[9085]254        }
255
256    }
257
[9082]258}
Note: See TracBrowser for help on using the repository browser.