diff options
| author | Charlie Stanton <charlie@shtanton.xyz> | 2025-04-06 18:53:05 +0100 | 
|---|---|---|
| committer | Charlie Stanton <charlie@shtanton.xyz> | 2025-04-06 18:53:05 +0100 | 
| commit | 7064baa135947cb0321db111ad510ba832966d68 (patch) | |
| tree | 824ec2a4c73778831408049d930226ee0737aa01 | |
| parent | 133d07d5e7781c86cc11cd86e80ad1ff5325fc36 (diff) | |
| download | ldjam57-7064baa135947cb0321db111ad510ba832966d68.tar | |
only allow placing stuff available in the level
| -rw-r--r-- | src/all.c | 59 | ||||
| -rw-r--r-- | src/levels.c | 10 | ||||
| -rw-r--r-- | src/tick.c | 4 | ||||
| -rw-r--r-- | src/types.c | 9 | 
4 files changed, 44 insertions, 38 deletions
| @@ -135,14 +135,14 @@ static DrawList *render(State *state, UI *ui, Arena *a) {  	for (int x = 0; x < GRIDWIDTH; x++) {  		for (int y = 0; y < GRIDHEIGHT; y++) { -			if (colorImages[state->grid[x + GRIDWIDTH * y]]) { +			if (colorImages[(int) state->grid[x + GRIDWIDTH * y]]) {  				drawList->els[drawList->len++] = (DrawElement) {  					.x = cellWidth * x + GRID_OFFSET_X,  					.y = cellHeight * y,  					.w = cellWidth,  					.h = cellHeight,  					.image = (ImageRef) { -						.index = colorImages[state->grid[x + GRIDWIDTH * y]], +						.index = colorImages[(int) state->grid[x + GRIDWIDTH * y]],  						.opacity = 255,  					},  				}; @@ -154,7 +154,7 @@ static DrawList *render(State *state, UI *ui, Arena *a) {  							.y = cellHeight * y + suby * cellHeight / 2,  							.w = cellWidth / 2 + (1 - subx),  							.h = cellHeight / 2 + (1 - suby), -							.fill = colors[state->grid[x + GRIDWIDTH * y]][subx + 2 * suby], +							.fill = colors[(int) state->grid[x + GRIDWIDTH * y]][subx + 2 * suby],  						};  					}  				} @@ -279,11 +279,16 @@ static DrawList *render(State *state, UI *ui, Arena *a) {  				image = state->playing ? IMAGE_PAUSE : IMAGE_PLAY;  				break;  		} +		Color fill = {0}; +		if (i == BUTTON_CONTINUE && state->levelComplete) { +			fill = (Color) {127, 255, 127, 200}; +		};  		drawList->els[drawList->len++] = (DrawElement) {  			.x = buttons[i].x,  			.y = ui->height - buttons[i].y,  			.w = buttons[i].w,  			.h = buttons[i].h, +			.fill = fill,  			.image = (ImageRef) {  				.index = image,  				.opacity = 255, @@ -291,36 +296,24 @@ static DrawList *render(State *state, UI *ui, Arena *a) {  		};  	} -	// render placeable cells -	int cellCount = 0; // doesn't increment when ignoring empty cells +	// Placeable cells  	for (int i = 0; i < MAX_PLACEABLE_CELLS; i++) { -		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 (state->placeableCells[i] == EMPTY) { +			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) + BUTTON_SPACING * (cellCount % 2 + 1)) * (cellCount + 1), // TODO - padding +					// .y = (BUTTON_SIZE / 2 + y * (BUTTON_SIZE / 2) + BUTTON_SPACING * (cellCount % 2 + 1)) * (cellCount + 1), // TODO - padding +					.y = BUTTON_SIZE / 2 + i * (BUTTON_SIZE + BUTTON_SPACING) + y * (BUTTON_SIZE / 2),  					.w = BUTTON_SIZE / 2,  					.h = BUTTON_SIZE / 2, -					.fill = colors[cellCount + 1][x + 2 * y], -					.border = {0, 0, 0, 0}, +					.fill = colors[(int) state->placeableCells[i]][x + 2 * y],  				};  			}  		} -		cellCount++;  	}  	return drawList; @@ -332,6 +325,7 @@ static void restart_level(Game* game) {  	game->state.goalx = levels[level].goalx;  	game->state.goaly = levels[level].goaly;  	game->state.playing = 0; +	xmemcpy(&game->state.placeableCells, &levels[level].placeableCells, sizeof(game->state.placeableCells));  }  static void update(Game *game, uint64_t now, Arena a) { @@ -350,17 +344,27 @@ static void update(Game *game, uint64_t now, Arena a) {  				int celly = game->ui.mousey - y * cellHeight;  				// Keeping this around for testing purposes  				// game->state.grid[x + y * GRIDWIDTH] = (game->state.grid[x + y * GRIDWIDTH] + 1) % (sizeof(colors) / sizeof(colors[0])); -				game->state.grid[x + y * GRIDWIDTH] = getPlaceAction( +				char placed = getPlaceAction(  					game->state.grid[x + y * GRIDWIDTH],  					cellx,  					celly,  					cellWidth,  					cellHeight  				); -			} -			// TODO - some ignore list for which cells we have left to place -			game->state.placedCells[0] = BLACK; +				if (placed != EMPTY) { +					for (int i = 0; i < MAX_PLACEABLE_CELLS; i++) { +						if (game->state.placeableCells[i] == placed) { +							game->state.grid[x + y * GRIDWIDTH] = placed; +							for (int j = i; j < MAX_PLACEABLE_CELLS - 1; j++) { +								game->state.placeableCells[j] = game->state.placeableCells[j + 1]; +							} +							game->state.placeableCells[MAX_PLACEABLE_CELLS - 1] = EMPTY; +							break; +						} +					} +				} +			}  			for (int i = 0; i < N_BUTTONS; i++) {  				if ( @@ -375,9 +379,10 @@ static void update(Game *game, uint64_t now, Arena a) {  							restart_level(game);  							break;  						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 +							// TODO - get size of levels array +							if (!game->state.levelComplete || game->state.currentLevel + 1 >= 2)  								break; +							game->state.levelComplete = 0;  							game->state.currentLevel++;  							restart_level(game);  							break; diff --git a/src/levels.c b/src/levels.c index 7f49fb1..e6f4d3a 100644 --- a/src/levels.c +++ b/src/levels.c @@ -16,7 +16,7 @@ static Level levels[] = {  			_  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  			_  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  			_  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _ -			_  _  _  _  B  O  _  _  _  _  _  _  _  _  _  _  _  _  _  _ +			_  _  _  _  B  O  _  _  _  _  _  _  _  _  _  _  _  _  _  B  			_  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  			_  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  			_  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _ @@ -29,11 +29,11 @@ static Level levels[] = {  		.goalx = 18,  		.goaly = 7,  		.placeableCells = { -			BLACK,  			RED, -			EMPTY, +			BLACK,  			YELLOW, -			RED_RIGHT +			RED_DOWN, +			RED_LEFT,  		},  	},  	{ @@ -48,7 +48,7 @@ static Level levels[] = {  			_  _  _  _  B  O  _  _  _  _  _  _  _  _  _  _  _  _  _  _  			_  _  _  _  B  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  			_  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _ -			_  _  _  _  B  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _ +			_  _  _  _  B  _  _  _  _  _  _  _  _  _  _  _  _  _  _  B  			_  _  _  _  _  B  _  _  _  _  _  _  _  _  _  _  _  _  _  _  			_  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  			_  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _ @@ -341,8 +341,8 @@ static void tick(Game *game, Arena a) {  		}  	} -	if (isBlue(game->state.grid[game->state.goalx + game->state.goaly * GRIDWIDTH])) { -		// TODO: Win conditions +	if (game->state.grid[game->state.goalx + game->state.goaly * GRIDWIDTH] == BLUE) { +		game->state.levelComplete = 1;  	}  } diff --git a/src/types.c b/src/types.c index fdb9aa0..b2f1ee6 100644 --- a/src/types.c +++ b/src/types.c @@ -101,19 +101,20 @@ typedef struct {  } Button;  typedef struct { -	int grid[GRIDWIDTH * GRIDHEIGHT]; +	char grid[GRIDWIDTH * GRIDHEIGHT];  	int goalx, goaly; -	int placeableCells[MAX_PLACEABLE_CELLS]; // Color +	char placeableCells[MAX_PLACEABLE_CELLS]; // Color  } Level;  typedef struct {  	uint64_t lastTick;  	char playing; -	int grid[GRIDWIDTH * GRIDHEIGHT]; +	char levelComplete; +	char grid[GRIDWIDTH * GRIDHEIGHT];  	int goalx, goaly;  	ButtonState buttonStates[N_BUTTONS];  	int currentLevel; -	int placedCells[MAX_PLACEABLE_CELLS]; // indices into Level placeableCells +	char placeableCells[MAX_PLACEABLE_CELLS];  } State;  // Mirror these in src/index.html.in | 
