Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9082 was 9082, checked in by jo, 12 years ago

Trying to create tetris bricks. Rough implementation done. Still having a nasty camera bug. (see the debug output concerning the cameraIndex)

File size: 7.8 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_ = 6; //<! 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
63
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        orxout()<< "TetrisBrick::createBrick" << endl;
75        if(this->shapeIndex_ == 0)
76            this->stonesPerBrick_ = 1;
77        for (unsigned int i = 0; i < this->stonesPerBrick_; i++)
78        {
79            // Create a new stone and add it to the brick.
80            TetrisStone* stone = new TetrisStone(this);
81            stone->setHealth(1.0f); //TODO: is this value low enough ?
82            this->brickStones_.push_back(stone);
83            this->attach(stone);
84            this->formBrick(stone, i);
85            if(this->tetris_ != NULL)
86            {
87                stone->setGame(this->tetris_);
88                if(this->tetris_->getCenterpoint() != NULL)
89                    stone->addTemplate(this->tetris_->getCenterpoint()->getStoneTemplate());
90                else
91                        orxout()<< "tetris_->getCenterpoint == NULL in TetrisBrick.cc"<< endl;
92            }
93            else
94                orxout()<< "tetris_ == NULL in TetrisBrick.cc"<< endl;
95        }
96    }
97
98    /**
99    @brief
100        This function creates the shape of a TetrisBrick. ! Spaghetti-Code !
101    @param i
102        The stone's number.
103    @param stone
104        The TetrisStone that is placed relative to the brick's position.
105    */
106    void TetrisBrick::formBrick(TetrisStone* stone, unsigned int i)
107    {
108        if(i != 0 && this->shapeIndex_ == 0)
109            orxout() << "So it has come to this in TetrisBrick.cc"<< endl;
110        if(i == 0) //setting the first stone as
111        {
112            stone->setPosition(0.0f, 0.0f, 0.0f);
113        }
114        else if(i == 1)
115        {
116            stone->setPosition(0.0f, size_, 0.0f);
117        }
118        else if(i == 2)
119        {
120            if(this->shapeIndex_ == 1 || this->shapeIndex_ == 6 || this->shapeIndex_ == 7)
121            {
122                stone->setPosition(0.0f, 2*size_, 0.0f);
123            }
124            else if(this->shapeIndex_ == 3 || this->shapeIndex_ == 4|| this->shapeIndex_ == 5)
125            {
126                stone->setPosition(size_, 0, 0.0f);
127            }
128            else if(this->shapeIndex_ == 2)
129            {
130                stone->setPosition(-size_, 0, 0.0f);
131            }
132        }
133        else if(i == 3)
134        {
135            if(this->shapeIndex_ == 2 || this->shapeIndex_ == 5)
136            {
137                stone->setPosition(size_, size_, 0.0f);
138            }
139            else if(this->shapeIndex_ == 1)
140            {
141                stone->setPosition(0, 3*size_, 0.0f);
142            }
143            else if(this->shapeIndex_ == 3 || this->shapeIndex_ == 7)
144            {
145                stone->setPosition(-size_, 0, 0.0f);
146            }
147            else if(this->shapeIndex_ == 4)
148            {
149                stone->setPosition(-size_, size_, 0.0f);
150            }
151            else if(this->shapeIndex_ == 6)
152            {
153                stone->setPosition(size_, 0, 0.0f);
154            }
155        }
156    }
157
158    bool TetrisBrick::isValidMove(Vector3& position)
159    {
160
161        for(unsigned int i = 0; i < this->stonesPerBrick_ ; i++)
162        {//TODO: check if isValidMove works with this function,
163            if(this->tetris_->isValidMove(this->brickStones_[i], position))
164                continue;
165            else
166                return false;
167        }
168        return true;
169    }
170
171    TetrisStone* TetrisBrick::getStone(unsigned int i)
172    {
173        if(i < this->brickStones_.size())
174            return this->brickStones_[i];
175        else return NULL;
176    }
177
178
179    Tetris* TetrisBrick::getTetris()
180    {
181        if (this->getGametype() != NULL && this->getGametype()->isA(Class(Tetris)))
182        {
183            Tetris* tetrisGametype = orxonox_cast<Tetris*>(this->getGametype().get());
184            return tetrisGametype;
185        }
186        return NULL;
187    }
188
189    bool TetrisBrick::contains(TetrisStone* stone)
190    {
191        for(unsigned int i = 0; i < brickStones_.size(); i++)
192        {
193            if(stone == brickStones_[i])
194                return true;
195        }
196        return false;
197    }
198
199    /**
200    @brief
201        Overloaded the function to rotate the Brick.
202    @param value
203        A vector whose first component is the angle by which to rotate.
204    */
205    void TetrisBrick::moveFrontBack(const Vector2& value)
206    {
207        if(value.x < 0) //speedup on key down
208        {
209            this->setVelocity(this->getVelocity()*1.1);
210        }
211        else if(!this->lockRotation_) //rotate when key up is pressed
212        {
213            orxout() << "The object should be rolled soon." << endl;
214            this->lockRotation_ = true; // multiple calls of this function have to be filtered out.
215            this->rotationTimer_.setTimer(0.1f, false, createExecutor(createFunctor(&TetrisBrick::unlockRotation, this)));
216            Quaternion q(Degree(90), Vector3::UNIT_Z);
217            this->setOrientation(this->getOrientation()*q); //rotation: roll 90°
218
219        }
220    }
221
222    /**
223    @brief
224        Overloaded the function to steer the Brick right and left
225    @param value
226        A vector whose first component is the direction in which we want to steer the Brick.
227    */
228    void TetrisBrick::moveRightLeft(const Vector2& value)
229    {
230        if(!this->delay_)
231        {
232            const Vector3& position = this->getPosition();
233            Vector3 newPos = Vector3(position.x+value.x/abs(value.x)*this->size_, position.y, position.z);
234            if(!this->isValidMove(newPos))
235                return;
236
237            this->setPosition(newPos);
238            this->delay_ = true;
239            this->delayTimer_.startTimer();
240        }
241    }
242
243    /**
244    @brief
245        Is called when the player changed.
246    */
247    void TetrisBrick::changedPlayer()
248    {
249        this->setVelocity(0.0f, 0.0f, 0.0f);
250    }
251
252}
Note: See TracBrowser for help on using the repository browser.