Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/tetris/TetrisBrick.cc @ 9771

Last change on this file since 9771 was 9756, checked in by jo, 11 years ago

Disabling the single tetris brick. Now all tetris bricks consists of four stones.

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