diff options
author | Ekaitz Zarraga <ekaitz@elenq.tech> | 2022-05-22 14:19:16 +0200 |
---|---|---|
committer | Ekaitz Zarraga <ekaitz@elenq.tech> | 2022-05-22 14:19:16 +0200 |
commit | 8578eca2b996bbcf62399202757325d4c6038f29 (patch) | |
tree | ef5a731fe4fce94785f047f9c3d161ffad8570ef /src/game.cpp | |
parent | d324ad963c24edf6d19dffc305a38e0b3607b5c1 (diff) |
WIP: Add game finish, line clearing and new piece
Diffstat (limited to 'src/game.cpp')
-rw-r--r-- | src/game.cpp | 79 |
1 files changed, 69 insertions, 10 deletions
diff --git a/src/game.cpp b/src/game.cpp index d897aa4..7d42f57 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -18,10 +18,6 @@ GameState::GameState(Renderer* renderer, float scale_): grid[j][i] = 0; } } - grid[1][1] = 1; - grid[0][1] = 1; - grid[0][2] = 1; - grid[9][9] = 1; xdirection_ = Direction::NONE; yspeed_ = 1; @@ -55,10 +51,11 @@ void GameState::update( unsigned int dt ){ tmp.initIterator(); while ( p = tmp.nextAbsBlockPos() ){ if(!cellIsEmpty(p)){ + newPiece(); return; } } - piece_ = tmp; + piece_.advance(); } // If timer finished, level up @@ -100,12 +97,68 @@ void GameState::render(){ 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_.restartTo( Piece::PieceType::LINE, 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 && grid[p->y][p->x] == 0 ); + return( p->x < GRID_WIDTH && + p->x >= 0 && + p->y < GRID_HEIGHT && + grid[p->y][p->x] == 0 ); } void GameState::pressDown(){ - yspeed_ = 4; + yspeed_ = 8; } void GameState::releaseDown(){ yspeed_ = 1; @@ -120,10 +173,12 @@ void GameState::pressLeft(){ tmp.initIterator(); while ( p = tmp.nextAbsBlockPos() ){ if(!cellIsEmpty(p)){ + // TODO: indicate that it's not possible to move + // sound and visual return; } } - piece_ = tmp; + piece_.move_left(); } void GameState::pressRight(){ Piece tmp = piece_; @@ -133,10 +188,12 @@ void GameState::pressRight(){ tmp.initIterator(); while ( p = tmp.nextAbsBlockPos() ){ if(!cellIsEmpty(p)){ + // TODO: indicate that it's not possible to move + // sound and visual return; } } - piece_ = tmp; + piece_.move_right(); } void GameState::pressRotate(){ @@ -147,8 +204,10 @@ void GameState::pressRotate(){ tmp.initIterator(); while ( p = tmp.nextAbsBlockPos() ){ if(!cellIsEmpty(p)){ + // TODO: indicate that it's not possible to move + // sound and visual return; } } - piece_ = tmp; + piece_.rotate(); } |