From 9bfa3e802ae7207c3dddc96f8385bbc5b4b4d4ef Mon Sep 17 00:00:00 2001 From: KikooDX Date: Tue, 1 Jun 2021 23:35:47 +0200 Subject: [PATCH] Prototype done! :D --- include/game.h | 2 +- include/player.h | 4 ++-- include/tiles.h | 3 ++- src/game/init.c | 3 ++- src/grid/draw.c | 5 ++++- src/grid/random_walker.c | 3 ++- src/grid/shop.c | 9 +++++--- src/main.c | 44 +++++++++++++++++++++++++++++++--------- src/player/draw.c | 7 +++++-- src/player/init.c | 3 ++- src/player/update.c | 23 +++++++++++++++++---- 11 files changed, 79 insertions(+), 27 deletions(-) diff --git a/include/game.h b/include/game.h index 2743756..d1b02c8 100644 --- a/include/game.h +++ b/include/game.h @@ -2,7 +2,7 @@ #include "grid.h" #include "player.h" -enum GameState { GameFloor, GameRest }; +enum GameState { GameFloor, GameRest, GameDead }; struct Game { enum GameState state; diff --git a/include/player.h b/include/player.h index 55e89f1..250df97 100644 --- a/include/player.h +++ b/include/player.h @@ -4,7 +4,7 @@ struct Player { int x; int y; - int cash; + int *cash; int left_held; int right_held; int up_held; @@ -12,6 +12,6 @@ struct Player { }; struct Player player_init(int x, int y); -void player_draw(struct Player, int scr_x, int scr_y); +int player_draw(struct Player, int scr_x, int scr_y); /* 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 e9bf8c0..b7145df 100644 --- a/include/tiles.h +++ b/include/tiles.h @@ -10,6 +10,7 @@ enum Tile { TILE_OBSTACLE_3, TILE_SOLID, TILE_ZONE_TRANSITION, - TILE_CONTRACTS, + TILE_CONTRACTS_SPEED_UP, + TILE_CONTRACTS_SLOW_DOWN, }; #define TILE_OOB TILE_SOLID diff --git a/src/game/init.c b/src/game/init.c index 3fab3d9..5f00af4 100644 --- a/src/game/init.c +++ b/src/game/init.c @@ -10,7 +10,8 @@ game_init(void) 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); + game.player_rest = + player_init(game.rest.width / 2, game.rest.height - 1); return game; } diff --git a/src/grid/draw.c b/src/grid/draw.c index 08520e1..f55b180 100644 --- a/src/grid/draw.c +++ b/src/grid/draw.c @@ -68,9 +68,12 @@ tile_draw(enum Tile tile, int x, int y) case TILE_ZONE_TRANSITION: color = C_RGB(0, 31, 31); break; - case TILE_CONTRACTS: + case TILE_CONTRACTS_SPEED_UP: color = C_RGB(31, 31, 0); break; + case TILE_CONTRACTS_SLOW_DOWN: + color = C_RED; + break; default: color = C_RGB(31, 0, 31); break; diff --git a/src/grid/random_walker.c b/src/grid/random_walker.c index 81fe708..ce26aa8 100644 --- a/src/grid/random_walker.c +++ b/src/grid/random_walker.c @@ -58,7 +58,8 @@ grid_random_walker(struct Grid *restrict grid) /* place resting points */ for (x = 64; x < grid->width; x += 64) { for (y = 0; y < grid->height; y += 1) { - if (!grid_get(*grid, x, y)) break; + if (!grid_get(*grid, x, y)) + break; } grid_set(grid, x, y, TILE_ZONE_TRANSITION); } diff --git a/src/grid/shop.c b/src/grid/shop.c index 6a61b27..2cfb7af 100644 --- a/src/grid/shop.c +++ b/src/grid/shop.c @@ -1,10 +1,13 @@ #include "grid.h" #include "tiles.h" -void grid_shop(struct Grid *restrict grid) { +void +grid_shop(struct Grid *restrict grid) +{ const int x_middle = grid->width / 2; const int y_middle = grid->height / 2; - grid_set(grid, x_middle, y_middle, TILE_CONTRACTS); - grid_set(grid, x_middle + 4, y_middle, TILE_ZONE_TRANSITION); + grid_set(grid, x_middle - 2, y_middle, TILE_CONTRACTS_SPEED_UP); + grid_set(grid, x_middle + 2, y_middle, TILE_CONTRACTS_SLOW_DOWN); + grid_set(grid, grid->width - 1, grid->height - 1, TILE_ZONE_TRANSITION); } diff --git a/src/main.c b/src/main.c index 7a2c08b..90c0eba 100644 --- a/src/main.c +++ b/src/main.c @@ -49,37 +49,61 @@ init_rand(void) static int main_loop(struct Game *restrict game) { - static int scroll_x = 1 << 13; - - if (scroll_x == 1 << 13) - scroll_x = 99; + static float scroll_x = 99.0; + static float scroll_speed = 0.6; clearevents(); - switch(game->state) { + switch (game->state) { case GameFloor: if (player_update(&game->player, &game->floor)) { game->state = GameRest; grid_shop(&game->rest); } - scroll_x -= 1; + scroll_x -= scroll_speed; + scroll_speed += 0.0003; break; case GameRest: - if (player_update(&game->player_rest, &game->rest)) + switch (player_update(&game->player_rest, &game->rest)) { + case -1: + scroll_speed += 0.2; + break; + case -2: + scroll_speed -= 0.6; + if (scroll_speed < 0.6) + scroll_speed = 0.6; + break; + case 1: game->state = GameFloor; + break; + default: + break; + } break; + case GameDead: default: break; } dclear(C_BLACK); - switch(game->state) { + 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); + if (!player_draw(game->player, + (scroll_x < 0) ? (scroll_x) : (0), 0)) + game->state = GameDead; break; case GameRest: grid_draw(game->rest, -2, 0); - player_draw(game->player_rest, -2, 0); + if (!player_draw(game->player_rest, -2, 0)) + game->state = GameDead; + break; + case GameDead: + dprint(0, 0, C_WHITE, "You died :("); + dprint(0, 16, C_WHITE, "Collected $%d", *game->player.cash); + dprint(0, 32, C_WHITE, "Survived %d feets", + -(int)(scroll_x / 4)); + dprint(0, 48, C_WHITE, "Thanks for playing :)"); + dprint(0, 64, C_WHITE, "(Press EXIT)"); break; default: break; diff --git a/src/player/draw.c b/src/player/draw.c index 5894892..c4e5ee7 100644 --- a/src/player/draw.c +++ b/src/player/draw.c @@ -2,11 +2,14 @@ #include "tiles.h" #include -void +int player_draw(struct Player player, int scr_x, int scr_y) { const int x = scr_x + player.x * TILE_WIDTH; const int y = scr_y + player.y * TILE_HEIGHT; + if (x < -TILE_WIDTH * 4) + return 0; drect(x + 2, y + 2, x + TILE_WIDTH - 3, y + TILE_HEIGHT - 3, C_GREEN); - dprint(0, 0, C_RGB(31, 31, 0), "$%d", player.cash); + dprint(0, 0, C_RGB(31, 31, 0), "$%d", *player.cash); + return 1; } diff --git a/src/player/init.c b/src/player/init.c index c0a86c6..804f579 100644 --- a/src/player/init.c +++ b/src/player/init.c @@ -3,9 +3,10 @@ struct Player player_init(int x, int y) { + static int cash = 0; struct Player player; player.x = x; player.y = y; - player.cash = 0; + player.cash = &cash; return player; } diff --git a/src/player/update.c b/src/player/update.c index ed870e6..d91918b 100644 --- a/src/player/update.c +++ b/src/player/update.c @@ -1,6 +1,7 @@ #include "grid.h" #include "player.h" #include +#include int player_update(struct Player *restrict player, struct Grid *restrict grid) @@ -51,11 +52,25 @@ player_update(struct Player *restrict player, struct Grid *restrict grid) case TILE_ZONE_TRANSITION: grid_set(grid, new_x, new_y, TILE_VOID); return 1; - case TILE_CONTRACTS: - /* start contracts sequence */ - break; + case TILE_CONTRACTS_SPEED_UP: + player->x = new_x; + player->y = new_y; + grid_set(grid, new_x, new_y, TILE_VOID); + grid_set(grid, new_x + 4, new_y, TILE_VOID); + *player->cash += 50; + return -1; + case TILE_CONTRACTS_SLOW_DOWN: + player->x = new_x; + player->y = new_y; + if (*player->cash < 50) + break; + grid_set(grid, new_x, new_y, TILE_VOID); + grid_set(grid, new_x - 4, new_y, TILE_VOID); + *player->cash -= 50; + return -2; case TILE_OBSTACLE_1: - player->cash += 1; + /* rand between 1 and 4 */ + *player->cash += 1 + rand() % 4; /* fallthrough */ case TILE_OBSTACLE_2: case TILE_OBSTACLE_3: