1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-25 07:33:02 +01:00

Add ability to toggle colours in game of life

This commit is contained in:
jfreegman 2021-06-03 14:45:03 -04:00
parent 4514ecd839
commit 321f694bb8
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63

View File

@ -66,6 +66,7 @@ typedef struct LifeState {
int y_bottom_bound; int y_bottom_bound;
short display_candy; short display_candy;
int colour;
} LifeState; } LifeState;
@ -117,6 +118,46 @@ static void life_toggle_display_candy(LifeState *state)
state->display_candy = (state->display_candy + 1) % 3; // magic number depends on life_get_display_char() state->display_candy = (state->display_candy + 1) % 3; // magic number depends on life_get_display_char()
} }
static void life_cycle_colour(LifeState *state)
{
switch (state->colour) {
case RED: {
state->colour = YELLOW;
break;
}
case YELLOW: {
state->colour = GREEN;
break;
}
case GREEN: {
state->colour = CYAN;
break;
}
case CYAN: {
state->colour = BLUE;
break;
}
case BLUE: {
state->colour = MAGENTA;
break;
}
case MAGENTA: {
state->colour = RED;
break;
}
default: {
state->colour = RED;
break;
}
}
}
static Cell *life_get_cell_at_coords(const LifeState *state, const int x, const int y) static Cell *life_get_cell_at_coords(const LifeState *state, const int x, const int y)
{ {
const int i = y - (state->y_top_bound - (LIFE_BOUNDARY_BUFFER / 2)); const int i = y - (state->y_top_bound - (LIFE_BOUNDARY_BUFFER / 2));
@ -131,18 +172,20 @@ static Cell *life_get_cell_at_coords(const LifeState *state, const int x, const
static void life_draw_cells(const GameData *game, WINDOW *win, LifeState *state) static void life_draw_cells(const GameData *game, WINDOW *win, LifeState *state)
{ {
wattron(win, A_BOLD | COLOR_PAIR(state->colour));
for (int i = LIFE_BOUNDARY_BUFFER / 2; i < state->num_rows - (LIFE_BOUNDARY_BUFFER / 2); ++i) { for (int i = LIFE_BOUNDARY_BUFFER / 2; i < state->num_rows - (LIFE_BOUNDARY_BUFFER / 2); ++i) {
for (int j = LIFE_BOUNDARY_BUFFER / 2; j < state->num_columns + 1 - (LIFE_BOUNDARY_BUFFER / 2); ++j) { for (int j = LIFE_BOUNDARY_BUFFER / 2; j < state->num_columns + 1 - (LIFE_BOUNDARY_BUFFER / 2); ++j) {
Cell *cell = &state->cells[i][j]; Cell *cell = &state->cells[i][j];
if (cell->alive) { if (cell->alive) {
Coords coords = cell->coords; Coords coords = cell->coords;
wattron(win, A_BOLD | COLOR_PAIR(LIFE_CELL_DEFAULT_COLOUR));
mvwaddch(win, coords.y, coords.x, cell->display_char); mvwaddch(win, coords.y, coords.x, cell->display_char);
wattroff(win, A_BOLD | COLOR_PAIR(LIFE_CELL_DEFAULT_COLOUR));
} }
} }
} }
wattroff(win, A_BOLD | COLOR_PAIR(state->colour));
} }
static void life_toggle_cell(LifeState *state) static void life_toggle_cell(LifeState *state)
@ -157,7 +200,9 @@ static void life_toggle_cell(LifeState *state)
} }
/* /*
* Returns the number of live neighbours of `idx` cell. * Returns the number of live neighbours of cell at `i` `j` position.
*
* Returns NULL if cell is touching a border.
*/ */
static int life_get_live_neighbours(const LifeState *state, const int i, const int j) static int life_get_live_neighbours(const LifeState *state, const int i, const int j)
{ {
@ -484,6 +529,11 @@ void life_cb_on_keypress(GameData *game, int key, void *cb_data)
break; break;
} }
case '`': {
life_cycle_colour(state);
break;
}
default: { default: {
return; return;
} }
@ -581,6 +631,7 @@ static int life_init_state(GameData *game, LifeState *state)
} }
state->speed = LIFE_DEFAULT_SPEED; state->speed = LIFE_DEFAULT_SPEED;
state->colour = LIFE_CELL_DEFAULT_COLOUR;
life_restart(game, state); life_restart(game, state);