summaryrefslogtreecommitdiff
path: root/src/piece.cpp
blob: 05dab127defe1cde51a35fac3885c41200894db8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "piece.h"
#include<cstdlib>

Piece::Piece(int xpos){

    int divisor = static_cast<int>(Piece::PieceType::L);
    auto type = static_cast<Piece::PieceType>(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 &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++;
}
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};
}