summaryrefslogtreecommitdiff
path: root/src/game.cpp
blob: 10231e7de8d488602d81fbdfdfdcd9512234b2aa (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "game.h"

GameState::GameState(Renderer* renderer, float scale_):
    closed_ {false},
    paused_ {false},
    timeAdvance_ {1000},
    timeCounterAdvance_ {1000},
    timeLevelUp_ {20000},
    renderer_  {renderer},
    piece_ {4},
    blockTexture_ {renderer_->renderer()}  {

    renderer->setScale(scale_, scale_);
    blockTexture_.loadFromFile("assets/block.png");

    for(int i=0; i < GRID_WIDTH; i++){
        for(int j=0; j < GRID_HEIGHT; j++){
            grid[j][i] = 0;
        }
    }

    xdirection_ = Direction::NONE;
    yspeed_ = 1;
}

void GameState::close(){
    closed_ = true;
}
void GameState::pause(){
    paused_ = true;
}
void GameState::resume(){
    paused_ = false;
}

bool GameState::isPaused(){
    return paused_;
}
bool GameState::isClosed(){
    return closed_;
}

void GameState::update( unsigned int dt ){
    timeCounterAdvance_ -= dt * yspeed_;
    if ( timeCounterAdvance_ <= 0 ){
        timeCounterAdvance_ = timeAdvance_;
        Piece tmp = piece_;
        tmp.advance();
        Point* p;
        // Check if there's space to move forward
        tmp.initIterator();
        while ( p = tmp.nextAbsBlockPos() ){
            if(!cellIsEmpty(p)){
                newPiece();
                return;
            }
        }
        piece_.advance();
    }

    // If timer finished, level up
    timeCounterLevelUp_ -= dt;
    if ( timeCounterLevelUp_ <= 0 ){
        // Make pieces go faster
        timeAdvance_ *= 0.95;
        timeCounterLevelUp_ = timeLevelUp_;
    }

}

void GameState::render(){
    // TODO
    renderer_->clear();

    SDL_Rect rect;
    rect.x = 0;
    rect.y = 0;
    rect.w = SQUARE_SIZE;
    rect.h = SQUARE_SIZE;

    // Render piece
    Point* b;
    piece_.initIterator();
    while( b = piece_.nextAbsBlockPos() ){
        blockTexture_.render( b->x * SQUARE_SIZE, b->y * SQUARE_SIZE, &rect );
    }

    // Render grid
    for(int i=0; i < GRID_WIDTH; i++){
        for(int j=0; j < GRID_HEIGHT; j++){
            if( grid[j][i] != 0){
                blockTexture_.render( i * SQUARE_SIZE, j * SQUARE_SIZE , &rect);
            }
        }
    }

    renderer_->update();
}

void GameState::finishGame(){
    // TODO: finish properly
    // FINISH HIM!!
    for(int i=0; i < GRID_WIDTH; i++){
        for(int j=0; j < GRID_HEIGHT; j++){
            grid[j][i] = 1;
        }
    }
    pause();
}
void GameState::clearLines(){
    for(int j=0; j < GRID_HEIGHT; j++){
        bool lineFull = true;
        for(int i=0; i < GRID_WIDTH; i++){
            if( grid[j][i] != 1 ){
                lineFull = false;
                break;
            }
        }
        if ( !lineFull ){
            continue;
        }

        // TODO: Animate and add sound
        // Move all down
        for(int k=j; k >= 0; k--){
            for(int i=0; i < GRID_WIDTH; i++){
                grid[k][i] = k==0 ? 0 : grid[k-1][i];
            }
        }
    }
}

void GameState::newPiece(){
    Point* p;
    piece_.initIterator();
    while ( p = piece_.nextAbsBlockPos() ){
        // Set the piece values as set
        grid[p->y][p->x] = 1;
    }
    // Clear the filled lines
    clearLines();

    piece_ = Piece( 4 );
    piece_.initIterator();
    while ( p = piece_.nextAbsBlockPos() ){
        // Set the piece values as set
        if( !cellIsEmpty(p) ){
            finishGame();
        }
    }
}

bool GameState::cellIsEmpty(Point* p){
    return( p->x < GRID_WIDTH &&
            p->x >= 0 &&
            p->y < GRID_HEIGHT &&
            grid[p->y][p->x] == 0 );
}

void GameState::pressDown(){
    yspeed_ = 8;
}
void GameState::releaseDown(){
    yspeed_ = 1;
}

void GameState::pressLeft(){
    Piece tmp = piece_;
    tmp.move_left();
    Point* p;

    // Check if there's space to move to
    tmp.initIterator();
    while ( p = tmp.nextAbsBlockPos() ){
        if(!cellIsEmpty(p)){
            // TODO: indicate that it's not possible to move
            // sound and visual
            return;
        }
    }
    piece_.move_left();
}
void GameState::pressRight(){
    Piece tmp = piece_;
    tmp.move_right();
    Point* p;
    // Check if there's space to move to
    tmp.initIterator();
    while ( p = tmp.nextAbsBlockPos() ){
        if(!cellIsEmpty(p)){
            // TODO: indicate that it's not possible to move
            // sound and visual
            return;
        }
    }
    piece_.move_right();
}

void GameState::pressRotate(){
    Piece tmp = piece_;
    tmp.rotate();
    Point* p;
    // Check if there's space to move to
    tmp.initIterator();
    while ( p = tmp.nextAbsBlockPos() ){
        if(!cellIsEmpty(p)){
            // TODO: indicate that it's not possible to move
            // sound and visual
            return;
        }
    }
    piece_.rotate();
}