#include "piece.h" #include Piece::Piece(int xpos){ int divisor = static_cast(Piece::PieceType::L); auto type = static_cast(rand() % divisor); restartTo(type, xpos); } Piece::Piece(Piece::PieceType type, int xpos){ restartTo(type, xpos); } void Piece::initIterator(){ iterator = 0; } Point* Piece::nextAbsBlockPos(){ current_block_ = blocks[iterator++]; current_block_.x += position_.x; current_block_.y += position_.y; if( iterator > SIZE ){ iterator = 0; return nullptr; } return ¤t_block_; } void Piece::rotate(){ int tmp; // FLIP for( int i = 0; i < SIZE; i++ ){ tmp = blocks[i].y; blocks[i].y = blocks[i].x; blocks[i].x = tmp; } // MIRROR for( int i = 0; i < SIZE; i++ ){ blocks[i].x = blocks[i].x * (-1) + (SIZE-1); } } void Piece::advance(){ position_.y++; } void Piece::move_left(){ position_.x--; } void Piece::move_right(){ position_.x++; } void Piece::restartTo(PieceType type, int xpos){ switch (type){ case PieceType::LINE: blocks[0] = { 1, 0 }; blocks[1] = { 1, 1 }; blocks[2] = { 1, 2 }; blocks[3] = { 1, 3 }; break; case PieceType::BLOCK: blocks[0] = { 1, 1 }; blocks[1] = { 1, 2 }; blocks[2] = { 2, 1 }; blocks[3] = { 2, 2 }; break; case PieceType::S: blocks[0] = { 1, 0 }; blocks[1] = { 1, 1 }; blocks[2] = { 2, 1 }; blocks[3] = { 2, 2 }; break; case PieceType::T: blocks[0] = { 1, 0 }; blocks[1] = { 1, 1 }; blocks[2] = { 1, 2 }; blocks[3] = { 2, 1 }; break; case PieceType::L: blocks[0] = { 1, 0 }; blocks[1] = { 1, 1 }; blocks[2] = { 1, 2 }; blocks[3] = { 2, 2 }; break; } position_ = {xpos, 0}; }