diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/all.c | 52 | ||||
| -rw-r--r-- | src/types.c | 2 | 
2 files changed, 37 insertions, 17 deletions
| @@ -199,11 +199,22 @@ static DrawList *render(State *state, UI *ui, Arena *a) {  		if (levels[state->currentLevel].placeableCells[i] == EMPTY)  			continue; +		// TODO - FIXME (how to tell how many cells we have left of a colour? Maybe placedCells is a bad idea!) +		int canContinue = 1; +		for (int j = 0; j < MAX_PLACEABLE_CELLS; j++) { +			if (levels[state->currentLevel].placeableCells[i] == state->placedCells[j]) { +				canContinue = 0; +				break; +			} +		} +		if (canContinue == 0) +			continue; +  		for (int x = 0; x < 2; x++) {  			for (int y = 0; y < 2; y++) {  				drawList->els[drawList->len++] = (DrawElement) {  					.x = BUTTON_SIZE / 2 + x * (BUTTON_SIZE / 2), -					.y = (BUTTON_SIZE / 2 + y * (BUTTON_SIZE / 2)) * (cellCount + 1), // TODO - padding +					.y = (BUTTON_SIZE / 2 + y * (BUTTON_SIZE / 2) + BUTTON_SPACING * (cellCount % 2 + 1)) * (cellCount + 1), // TODO - padding  					.w = BUTTON_SIZE / 2,  					.h = BUTTON_SIZE / 2,  					.fill = colors[cellCount + 1][x + 2 * y], @@ -217,7 +228,16 @@ static DrawList *render(State *state, UI *ui, Arena *a) {  	return drawList;  } +static void restart_level(Game* game) { +	const int level = game->state.currentLevel; +	xmemcpy(&game->state.grid, &levels[level].grid, sizeof(game->state.grid)); +	game->state.goalx = levels[level].goalx; +	game->state.goaly = levels[level].goaly; +	game->state.playing = 0; +} +  static void update(Game *game, uint64_t now, Arena a) { +	// TODO - offset some more because of right black bar, dependent on ui size and window size?  	const int offset_width = game->ui.width - GRID_OFFSET_X;  	switch (game->input) { @@ -225,8 +245,11 @@ static void update(Game *game, uint64_t now, Arena a) {  		case INPUT_CLICK:  			x = (game->mousex - GRID_OFFSET_X) * GRIDWIDTH / offset_width;  			y = game->mousey * GRIDHEIGHT / game->ui.height; -			if (x >= 0) +			if (x >= 0) { // TODO - 0 isn't far left of grid for some reason, it's slightly more  				game->state.grid[x + y * GRIDWIDTH] = (game->state.grid[x + y * GRIDWIDTH] + 1) % (sizeof(colors) / sizeof(colors[0])); +				// TODO - some ignore list for which cells we have left to place +				game->state.placedCells[0] = BLACK; +			}  			for (int i = 0; i < N_BUTTONS; i++) {  				if ( @@ -237,27 +260,16 @@ static void update(Game *game, uint64_t now, Arena a) {  					// TODO - CLICK THINGS  					//game->state.buttonStates[i] = BUTTON_STATE_PRESSED;  					switch (i) { -						case BUTTON_RETRY: { -							// reset to initial values -							int currentLevel = game->state.currentLevel; -							xmemcpy(&game->state.grid, &levels[currentLevel].grid, sizeof(game->state.grid)); -							game->state.goalx = levels[currentLevel].goalx; -							game->state.goaly = levels[currentLevel].goaly; -							game->state.playing = 0; +						case BUTTON_RETRY: +							restart_level(game);  							break; -						} -						case BUTTON_CONTINUE: { +						case BUTTON_CONTINUE:  							// TODO - don't allow if level has not been completed  							if (game->state.currentLevel + 1 >= 2) // TODO - get size of levels array  								break;  							game->state.currentLevel++; -							int currentLevel = game->state.currentLevel; -							xmemcpy(&game->state.grid, &levels[currentLevel].grid, sizeof(game->state.grid)); -							game->state.goalx = levels[currentLevel].goalx; -							game->state.goaly = levels[currentLevel].goaly; -							game->state.playing = 0; +							restart_level(game);  							break; -						}  						case BUTTON_BACK:  							break;  						case BUTTON_PLAY: @@ -275,6 +287,9 @@ static void update(Game *game, uint64_t now, Arena a) {  		case INPUT_PAUSE_PLAY:  			game->state.playing = !game->state.playing;  			break; +		case INPUT_RESTART: +			restart_level(game); +			break;  		default:  			break;  	} @@ -377,6 +392,9 @@ int main(int argc, char **argv) {  						case SDLK_SPACE:  							game->input = INPUT_PAUSE_PLAY;  							break; +						case SDLK_R: +							game->input = INPUT_RESTART; +							break;  					}  					break;  				case SDL_EVENT_WINDOW_RESIZED: diff --git a/src/types.c b/src/types.c index 4c7a3cb..bd6b858 100644 --- a/src/types.c +++ b/src/types.c @@ -93,6 +93,7 @@ typedef struct {  	int goalx, goaly;  	ButtonState buttonStates[N_BUTTONS];  	int currentLevel; +	int placedCells[MAX_PLACEABLE_CELLS]; // indices into Level placeableCells  } State;  // Mirror these in src/index.html.in @@ -101,6 +102,7 @@ enum {  	INPUT_CLICK,  	INPUT_RCLICK,  	INPUT_PAUSE_PLAY, +	INPUT_RESTART,  };  typedef struct { | 
