[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 | |
---|
| 44 | namespace 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); |
---|
[10765] | 83 | if(this->tetris_ != nullptr) |
---|
[9082] | 84 | { |
---|
| 85 | stone->setGame(this->tetris_); |
---|
[10765] | 86 | if(this->tetris_->getCenterpoint() != nullptr) |
---|
[9082] | 87 | stone->addTemplate(this->tetris_->getCenterpoint()->getStoneTemplate()); |
---|
| 88 | else |
---|
[10765] | 89 | orxout()<< "tetris_->getCenterpoint == nullptr in TetrisBrick.cc"<< endl; |
---|
[9082] | 90 | } |
---|
| 91 | else |
---|
[10765] | 92 | orxout()<< "tetris_ == nullptr in TetrisBrick.cc"<< endl; |
---|
[9082] | 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 | { |
---|
[9945] | 118 | stone->setPosition(0.0f, 2*size_, 0.0f); |
---|
[9082] | 119 | } |
---|
| 120 | else if(this->shapeIndex_ == 3 || this->shapeIndex_ == 4|| this->shapeIndex_ == 5) |
---|
| 121 | { |
---|
[9945] | 122 | stone->setPosition(size_, 0, 0.0f); |
---|
[9082] | 123 | } |
---|
| 124 | else if(this->shapeIndex_ == 2) |
---|
| 125 | { |
---|
[9945] | 126 | stone->setPosition(-size_, 0, 0.0f); |
---|
[9082] | 127 | } |
---|
| 128 | } |
---|
| 129 | else if(i == 3) |
---|
| 130 | { |
---|
| 131 | if(this->shapeIndex_ == 2 || this->shapeIndex_ == 5) |
---|
| 132 | { |
---|
[9945] | 133 | stone->setPosition(size_, size_, 0.0f); |
---|
[9082] | 134 | } |
---|
| 135 | else if(this->shapeIndex_ == 1) |
---|
| 136 | { |
---|
[9945] | 137 | stone->setPosition(0, 3*size_, 0.0f); |
---|
[9082] | 138 | } |
---|
| 139 | else if(this->shapeIndex_ == 3 || this->shapeIndex_ == 7) |
---|
| 140 | { |
---|
[9945] | 141 | stone->setPosition(-size_, 0, 0.0f); |
---|
[9082] | 142 | } |
---|
| 143 | else if(this->shapeIndex_ == 4) |
---|
| 144 | { |
---|
[9945] | 145 | stone->setPosition(-size_, size_, 0.0f); |
---|
[9082] | 146 | } |
---|
| 147 | else if(this->shapeIndex_ == 6) |
---|
| 148 | { |
---|
[9945] | 149 | stone->setPosition(size_, 0, 0.0f); |
---|
[9082] | 150 | } |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
[9084] | 154 | bool TetrisBrick::isValidMove(const Vector3& position, bool isRotation = false) |
---|
[9082] | 155 | { |
---|
[9945] | 156 | return this->tetris_->isValidMove(this,position, isRotation); |
---|
[9082] | 157 | } |
---|
| 158 | |
---|
| 159 | TetrisStone* TetrisBrick::getStone(unsigned int i) |
---|
| 160 | { |
---|
[9945] | 161 | if(i < this->brickStones_.size()) |
---|
[9082] | 162 | return this->brickStones_[i]; |
---|
[10765] | 163 | else return nullptr; |
---|
[9082] | 164 | } |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | Tetris* TetrisBrick::getTetris() |
---|
| 168 | { |
---|
[10765] | 169 | if (this->getGametype() != nullptr && this->getGametype()->isA(Class(Tetris))) |
---|
[9082] | 170 | { |
---|
[10624] | 171 | Tetris* tetrisGametype = orxonox_cast<Tetris*>(this->getGametype()); |
---|
[9082] | 172 | return tetrisGametype; |
---|
| 173 | } |
---|
[10765] | 174 | return nullptr; |
---|
[9082] | 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 | { |
---|
[9803] | 187 | Vector3 v_new = this->getVelocity()*1.2; |
---|
| 188 | if (v_new.y < -400.0f) //limiting the speed to prevent break throughs. |
---|
| 189 | v_new.y = -400.0f; |
---|
| 190 | this->setVelocity(v_new); |
---|
[9082] | 191 | } |
---|
| 192 | else if(!this->lockRotation_) //rotate when key up is pressed |
---|
| 193 | { |
---|
[9945] | 194 | if(!isValidMove(this->getPosition(), true)) //catch illegal rotations |
---|
| 195 | return; |
---|
[9082] | 196 | this->lockRotation_ = true; // multiple calls of this function have to be filtered out. |
---|
| 197 | this->rotationTimer_.setTimer(0.1f, false, createExecutor(createFunctor(&TetrisBrick::unlockRotation, this))); |
---|
| 198 | Quaternion q(Degree(90), Vector3::UNIT_Z); |
---|
| 199 | this->setOrientation(this->getOrientation()*q); //rotation: roll 90° |
---|
[9084] | 200 | this->rotationCount_ = (this->rotationCount_ + 1) % 4; |
---|
[9082] | 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | /** |
---|
| 205 | @brief |
---|
| 206 | Overloaded the function to steer the Brick right and left |
---|
| 207 | @param value |
---|
| 208 | A vector whose first component is the direction in which we want to steer the Brick. |
---|
| 209 | */ |
---|
| 210 | void TetrisBrick::moveRightLeft(const Vector2& value) |
---|
| 211 | { |
---|
| 212 | if(!this->delay_) |
---|
| 213 | { |
---|
| 214 | const Vector3& position = this->getPosition(); |
---|
| 215 | Vector3 newPos = Vector3(position.x+value.x/abs(value.x)*this->size_, position.y, position.z); |
---|
| 216 | if(!this->isValidMove(newPos)) |
---|
| 217 | return; |
---|
| 218 | |
---|
| 219 | this->setPosition(newPos); |
---|
| 220 | this->delay_ = true; |
---|
| 221 | this->delayTimer_.startTimer(); |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | /** |
---|
| 226 | @brief |
---|
| 227 | Is called when the player changed. |
---|
| 228 | */ |
---|
| 229 | void TetrisBrick::changedPlayer() |
---|
| 230 | { |
---|
| 231 | this->setVelocity(0.0f, 0.0f, 0.0f); |
---|
| 232 | } |
---|
[9086] | 233 | |
---|
[9085] | 234 | /** |
---|
| 235 | @brief |
---|
| 236 | Attaches stones to the Centerpoint. |
---|
| 237 | */ |
---|
| 238 | void TetrisBrick::releaseStones(TetrisCenterpoint* center) |
---|
| 239 | { |
---|
[9086] | 240 | assert(this->tetris_); |
---|
[10916] | 241 | for(TetrisStone* stone : this->brickStones_) |
---|
[9085] | 242 | { |
---|
[10916] | 243 | stone->detachFromParent(); |
---|
| 244 | stone->attachToParent(center); |
---|
| 245 | stone->setPosition(this->getPosition()+this->tetris_->rotateVector(stone->getPosition(),this->rotationCount_ )); |
---|
[9085] | 246 | } |
---|
[9328] | 247 | this->brickStones_.clear(); |
---|
[9085] | 248 | } |
---|
| 249 | |
---|
[9082] | 250 | } |
---|