diff options
Diffstat (limited to 'src/game.cpp')
-rw-r--r-- | src/game.cpp | 102 |
1 files changed, 100 insertions, 2 deletions
diff --git a/src/game.cpp b/src/game.cpp index aae7e7f..1ed8cc0 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,8 +1,30 @@ #include "game.h" -GameState::GameState(): +GameState::GameState(Renderer* renderer, float scale_): closed_ {false}, - paused_ {false} { + paused_ {false}, + timeAdvance_ {1000}, + timeCounterAdvance_ {1000}, + timeLevelUp_ {20000}, + renderer_ {renderer}, + piece_ {Piece::PieceType::LINE, 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; + } + } + grid[1][1] = 1; + grid[0][1] = 1; + grid[0][2] = 1; + grid[9][9] = 1; + + xdirection_ = Direction::NONE; + yspeed_ = 1; } void GameState::close(){ @@ -21,3 +43,79 @@ bool GameState::isPaused(){ 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; + tmp.initIterator(); + + // Check if there's space to move forward + bool canadvance; + while ( p = tmp.nextAbsBlockPos() ){ + if ( grid[p->y][p->x] == 0 ){ + canadvance = true; + } else { + canadvance = false; + } + } + if ( canadvance ){ + piece_ = tmp; + } + } + + // 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::pressRotate(){} +void GameState::pressDown(){ + yspeed_ = 4; +} +void GameState::releaseDown(){ + yspeed_ = 1; +} + +void GameState::pressLeft(){} +void GameState::releaseLeft(){} +void GameState::pressRight(){} +void GameState::releaseRight(){} |