diff --git a/include/game.h b/include/game.h index 98fe63a..2743756 100644 --- a/include/game.h +++ b/include/game.h @@ -2,9 +2,14 @@ #include "grid.h" #include "player.h" +enum GameState { GameFloor, GameRest }; + struct Game { + enum GameState state; struct Grid floor; + struct Grid rest; struct Player player; + struct Player player_rest; }; struct Game game_init(void); diff --git a/include/player.h b/include/player.h index ff7b7b0..55e89f1 100644 --- a/include/player.h +++ b/include/player.h @@ -13,4 +13,5 @@ struct Player { struct Player player_init(int x, int y); void player_draw(struct Player, int scr_x, int scr_y); -void player_update(struct Player *restrict, struct Grid *restrict); +/* return 1 on zone transition */ +int player_update(struct Player *restrict, struct Grid *restrict); diff --git a/include/tiles.h b/include/tiles.h index 0b0bc17..18851a5 100644 --- a/include/tiles.h +++ b/include/tiles.h @@ -9,5 +9,6 @@ enum Tile { TILE_OBSTACLE_2, TILE_OBSTACLE_3, TILE_SOLID, + TILE_ZONE_TRANSITION, }; #define TILE_OOB TILE_SOLID diff --git a/src/game/init.c b/src/game/init.c index 09e19d7..3fab3d9 100644 --- a/src/game/init.c +++ b/src/game/init.c @@ -5,9 +5,12 @@ struct Game game_init(void) { struct Game game; + game.state = GameFloor; game.floor = grid_new(1024, 14); grid_random_walker(&game.floor); game.player = player_init(0, game.floor.height / 2); + game.rest = grid_new(25, 14); + game.player_rest = player_init(game.rest.width / 2, game.rest.height - 1); return game; } @@ -15,4 +18,5 @@ void game_deinit(struct Game game) { grid_free(game.floor); + grid_free(game.rest); } diff --git a/src/main.c b/src/main.c index 26bc65e..df857fb 100644 --- a/src/main.c +++ b/src/main.c @@ -55,11 +55,32 @@ main_loop(struct Game *restrict game) scroll_x = 99; clearevents(); - player_update(&game->player, &game->floor); + switch(game->state) { + case GameFloor: + if (player_update(&game->player, &game->floor)) + game->state = GameRest; + break; + case GameRest: + if (player_update(&game->player_rest, &game->rest)) + game->state = GameFloor; + break; + default: + break; + } dclear(C_BLACK); - grid_draw(game->floor, (scroll_x < 0) ? (scroll_x) : (0), 0); - player_draw(game->player, (scroll_x < 0) ? (scroll_x) : (0), 0); + switch(game->state) { + case GameFloor: + grid_draw(game->floor, (scroll_x < 0) ? (scroll_x) : (0), 0); + player_draw(game->player, (scroll_x < 0) ? (scroll_x) : (0), 0); + break; + case GameRest: + grid_draw(game->rest, -2, 0); + player_draw(game->player_rest, -2, 0); + break; + default: + break; + } dupdate(); scroll_x -= 1; diff --git a/src/player/update.c b/src/player/update.c index 420b72e..7f1f45f 100644 --- a/src/player/update.c +++ b/src/player/update.c @@ -2,7 +2,7 @@ #include "player.h" #include -void +int player_update(struct Player *restrict player, struct Grid *restrict grid) { int new_x; @@ -40,13 +40,27 @@ player_update(struct Player *restrict player, struct Grid *restrict grid) if (new_y >= grid->height) new_y = 0; - if (!grid_get(*grid, new_x, new_y)) { + if (new_x == player->x && new_y == player->y) + return 0; + + switch (grid_get(*grid, new_x, new_y)) { + case TILE_VOID: player->x = new_x; player->y = new_y; - } else if (grid_get(*grid, new_x, new_y) != TILE_SOLID) { + break; + case TILE_ZONE_TRANSITION: + return 1; + case TILE_OBSTACLE_1: + player->cash += 1; + /* fallthrough */ + case TILE_OBSTACLE_2: + case TILE_OBSTACLE_3: grid_set(grid, new_x, new_y, grid_get(*grid, new_x, new_y) - 1); - /* get rich! */ - if (!grid_get(*grid, new_x, new_y)) - player->cash += 1; + break; + case TILE_SOLID: + default: + break; } + + return 0; }