summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/game.cpp79
-rw-r--r--src/game.h3
-rw-r--r--src/piece.cpp70
-rw-r--r--src/piece.h1
4 files changed, 110 insertions, 43 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();
}
diff --git a/src/game.h b/src/game.h
index 87e3f93..022a733 100644
--- a/src/game.h
+++ b/src/game.h
@@ -47,6 +47,9 @@ class GameState{
void update(unsigned int dt);
void render();
+ void newPiece();
+ void clearLines();
+ void finishGame();
bool cellIsEmpty(Point* p);
void pressRotate();
diff --git a/src/piece.cpp b/src/piece.cpp
index 67755f0..d686a32 100644
--- a/src/piece.cpp
+++ b/src/piece.cpp
@@ -1,39 +1,7 @@
#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};
+ restartTo(type, xpos);
}
void Piece::initIterator(){
@@ -75,3 +43,39 @@ void Piece::move_left(){
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};
+}
diff --git a/src/piece.h b/src/piece.h
index ee95539..1c3e820 100644
--- a/src/piece.h
+++ b/src/piece.h
@@ -22,6 +22,7 @@ class Piece {
Piece(PieceType type, int xpos);
Point getPosition();
+ void restartTo(PieceType type, int xpos);
void rotate();
void advance();
void move_left();