summaryrefslogtreecommitdiff
path: root/src/piece.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/piece.cpp')
-rw-r--r--src/piece.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/piece.cpp b/src/piece.cpp
new file mode 100644
index 0000000..51e8dc7
--- /dev/null
+++ b/src/piece.cpp
@@ -0,0 +1,71 @@
+#include "piece.h"
+
+Piece::Piece(Piece::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] = { 1, 2 };
+ blocks[3] = { 1, 3 };
+ break;
+ case PieceType::T:
+ blocks[0] = { 1, 0 };
+ blocks[1] = { 1, 1 };
+ blocks[2] = { 1, 2 };
+ blocks[3] = { 1, 3 };
+ break;
+ case PieceType::L:
+ blocks[0] = { 1, 0 };
+ blocks[1] = { 1, 1 };
+ blocks[2] = { 1, 2 };
+ blocks[3] = { 1, 3 };
+ break;
+ }
+ position_ = {xpos, 0};
+}
+
+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 &current_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++;
+}