Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 148 lines (98 sloc) 2.436 kb
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
#include "states.h"

#include "system/keyboard.h"
#include "utils/timer.h"
#include "scene.h"
#include "label.h"

extern Scene scene;


void st_idle(){

}

static void trans_title_falling(){

float* y = &scene.cam->mat_world[13];

if (*y > -19){
*y -= 0.25;
}else{
scene.state = st_falling;
}


}


static void trans_falling_title(){

float* y = &scene.cam->mat_world[13];

if (*y < 30){
*y += 0.25;
}else{
scene.state = st_title;
}


}


void st_title(){

if (keyboard.keyHit('\r')){
scene.state = trans_title_falling;
}

}


void st_falling(){


static Timer timer = {1, 0, 0}; // initial 1 second
static Timer wall_clock = {1, 0, 0};
static int score = 0;


Board* board = (Board*)scene.objects->data[0];
Label* lbl_time = (Label*)scene.objects->data[3];
Label* lbl_score = (Label*)scene.objects->data[4];

timerTick(&wall_clock);
sprintf(lbl_time->text, "time:\n%04d", wall_clock.count);
sprintf(lbl_score->text, "score:\n%04d", score);

int right_left =
keyboard.keyHit(keyboard.RIGHT) -
keyboard.keyHit(keyboard.LEFT);

if (keyboard.keyHit(keyboard.UP)){
if (boardPieceCanRotate(board)){
boardPieceRotate(board);
}
}


if ( boardPieceCanMove(board, 0, right_left) ){
boardPieceMove(board, 0, right_left);
}


if (keyboard.keyHit(keyboard.DOWN)){
boardPieceDrop(board);
wavePosSet(scene.wave, &board->piece);
scene.wave->time = 0;
timer.tick = 0; // next timerTick(&timer) will return 1
}



if (timerTick(&timer)){


if ( boardPieceCanMove(board, 1, 0) ){

boardPieceMove(board, 1, 0);

}else{
int complete_rows = boardCompleteRows(board);
if (complete_rows){
score += complete_rows * complete_rows;
if (timer.time > 0.1){
timer.time = 1 + (0.1f - 1) / 150 * score;
}
scene.state = st_clearing;
return;
}
if (! boardPieceNew(board) ){
timerInit(&timer, 1);
timerInit(&wall_clock, wall_clock.time);
score = 0;
boardClear(board);
boardPieceNew(board);
scene.state = trans_falling_title;
}
}


}

}


void st_clearing(){

static Timer timer = {0.11, 0, 0};

Board* board = (Board*)scene.objects->data[0];

if (timerTick(&timer)){
if (timer.count % 2){
boardHiddenRowsShow(board);
}else{
boardCompleteRowsHide(board);
}
}

if (timer.count > 10){
boardClearComplete(board);
if ( boardPieceNew(board) ){
timerInit(&timer, timer.time);
scene.state = st_falling;
}
}

}
Something went wrong with that request. Please try again.