From ec51d32ea0a30c138e690f637f38d83c086e02b9 Mon Sep 17 00:00:00 2001 From: quou Date: Sat, 6 May 2023 15:50:31 +1000 Subject: Add the main mechanic. --- game.c | 91 ++++++++++++++++++++++++++++++++++++++------------- game_config.h | 3 ++ intermediate/map.bmp | Bin 8330 -> 8330 bytes map.c | 12 ++++--- player.c | 1 + sprite.c | 17 +++++++++- sprite.h | 5 ++- world.c | 3 ++ world.h | 1 + 9 files changed, 104 insertions(+), 29 deletions(-) diff --git a/game.c b/game.c index 2cba35c..7eebb8b 100644 --- a/game.c +++ b/game.c @@ -96,47 +96,76 @@ static void menu_deinit(Game* game) { } static void gameplay_init(Game* game) { + World* world; + + world = &game->world; + seed_rng(500); init_world(&game->world); - init_map(&game->world.map, &game->world); - init_player(&game->world.player, &game->world); - new_skull(&game->world, 0, 0); + init_map(&world->map, world); + init_player(&world->player, world); + new_skull(world, 0, 0); + + world->gmemory = gmemory_max; } static void render_hud(Game* game) { - int i; - const Sprite* heart_sprite; - const Bitmap* heart_bitmap; + int i, g; + const Sprite* sprite; + const Bitmap* bitmap; World* world; Player* player; world = &game->world; player = &world->player; - heart_sprite = get_sprite(sprite_heart_full); - heart_bitmap = get_bitmap(heart_sprite->bitmap); + sprite = get_sprite(sprite_heart_full); + bitmap = get_bitmap(sprite->bitmap); for (i = 0; i < player->hp; i++) { render_bitmap( - heart_bitmap, - 1 + i * (heart_sprite->rect.w + 1), + bitmap, + 1 + i * (sprite->rect.w + 1), 1, - &heart_sprite->rect + &sprite->rect ); } - heart_sprite = get_sprite(sprite_heart_empty); - heart_bitmap = get_bitmap(heart_sprite->bitmap); + sprite = get_sprite(sprite_heart_empty); + bitmap = get_bitmap(sprite->bitmap); for (; i < player_max_hp; i++) { render_bitmap( - heart_bitmap, - 1 + i * (heart_sprite->rect.w + 1), + bitmap, + 1 + i * (sprite->rect.w + 1), 1, - &heart_sprite->rect + &sprite->rect + ); + } + + sprite = get_sprite(sprite_ram_full); + bitmap = get_bitmap(sprite->bitmap); + g = world->gmemory / gmemory_units; + for (i = 0; i < g; i++) { + render_bitmap( + bitmap, + 1 + i * (sprite->rect.w + 1), + 8, + &sprite->rect + ); + } + sprite = get_sprite(sprite_ram_empty); + bitmap = get_bitmap(sprite->bitmap); + g = gmemory_max / gmemory_units; + for (; i < g; i++) { + render_bitmap( + bitmap, + 1 + i * (sprite->rect.w + 1), + 8, + &sprite->rect ); } } static void gameplay_update(Game* game) { - int cx, cy, i; + int cx, cy; World* world; Player* player; cx = game->world.cam_x; @@ -155,6 +184,11 @@ static void gameplay_update(Game* game) { sprite_system(world); render_hud(game); + + if (world->gmemory <= 0) { + world->oom = 1; + game_change_state(game, game_state_dead); + } } static void gameplay_deinit(Game* game) { @@ -183,8 +217,10 @@ static void dead_init(Game* game) { static void dead_update(Game* game) { const BM_Font* font; int cx, cy; + World* world; font = get_default_font(); + world = &game->world; update_menu(&game->menu); @@ -193,12 +229,21 @@ static void dead_update(Game* game) { render_map(&game->world.map, cx, cy); sprite_system(&game->world); - rfont_text( - font, - renderer_w / 2 - 40, - game->menu.y - 15, - "You died" - ); + if (world->oom) { + rfont_text( + font, + renderer_w / 2 - 65, + game->menu.y - 15, + "Out of Memory" + ); + } else { + rfont_text( + font, + renderer_w / 2 - 40, + game->menu.y - 15, + "You died" + ); + } render_menu(&game->menu, get_default_font()); } diff --git a/game_config.h b/game_config.h index 8609814..5094cf8 100644 --- a/game_config.h +++ b/game_config.h @@ -17,4 +17,7 @@ #define mbmp_w (map_width * map_tile_size) #define mbmp_h (map_height * map_tile_size) +#define gmemory_max 100 +#define gmemory_units 10 + #endif diff --git a/intermediate/map.bmp b/intermediate/map.bmp index e6a66ee..112f29b 100644 Binary files a/intermediate/map.bmp and b/intermediate/map.bmp differ diff --git a/map.c b/map.c index d8b6e1c..b9eb0ac 100644 --- a/map.c +++ b/map.c @@ -7,13 +7,13 @@ void init_map(Map* map, World* world) { const Sprite* floor, * bricks; const Bitmap* fbmp, * bbmp; Bitmap bitmap; - int i, x, y; + int i, x, y, r; bitmap.pixels = map->pixels; bitmap.w = mbmp_w; bitmap.h = mbmp_h; - floor = get_sprite(sprite_floor_tile); + floor = get_sprite(sprite_floor_tile_1); bricks = get_sprite(sprite_bricks); fbmp = get_bitmap(floor->bitmap); bbmp = get_bitmap(bricks->bitmap); @@ -22,8 +22,12 @@ void init_map(Map* map, World* world) { bitmap.pixels[i] = make_black(); } - for (x = 1; x < map_width - 1; x++) { - for (y = 1; y < map_height - 1; y++) { + for (y = 1; y < map_height - 1; y++) { + for (x = 1; x < map_width - 1; x++) { + r = rng() % 60; + if (r > 50) { + floor = get_sprite((r % 4) + sprite_floor_tile_1); + } blit( &bitmap, fbmp, diff --git a/player.c b/player.c index 4a82cff..7135557 100644 --- a/player.c +++ b/player.c @@ -130,6 +130,7 @@ void update_player(Player* player, World* world) { } if (player->hp <= 0) { + world->oom = 0; game_change_state(&game, game_state_dead); } diff --git a/sprite.c b/sprite.c index 9663cbd..e2914ce 100644 --- a/sprite.c +++ b/sprite.c @@ -71,11 +71,26 @@ static const Sprite sprites[] = { asset_id_usr, { 0, 80, 171, 10 } }, - /* sprite_floor_tile */ + /* sprite_floor_tile_1 */ { asset_id_map, { 0, 0, 16, 16 } }, + /* sprite_floor_tile_2 */ + { + asset_id_map, + { 0, 16, 16, 16 } + }, + /* sprite_floor_tile_3 */ + { + asset_id_map, + { 16, 16, 16, 16 } + }, + /* sprite_floor_tile_4 */ + { + asset_id_map, + { 16, 16, 16, 16 } + }, /* sprite_bricks */ { asset_id_map, diff --git a/sprite.h b/sprite.h index 852fa4c..5fd6b2a 100644 --- a/sprite.h +++ b/sprite.h @@ -20,7 +20,10 @@ typedef enum { sprite_enemy_bullet, sprite_author, sprite_free, - sprite_floor_tile, + sprite_floor_tile_1, + sprite_floor_tile_2, + sprite_floor_tile_3, + sprite_floor_tile_4, sprite_bricks, sprite_heart_empty, sprite_heart_full, diff --git a/world.c b/world.c index 39673d4..4995351 100644 --- a/world.c +++ b/world.c @@ -29,12 +29,15 @@ Entity new_entity(World* world) { world->bitmask[e] = 0; + world->gmemory--; + return e; } void destroy_entity(World* world, Entity e) { world->recycle_bin[world->recycle_bin_count++] = e; world->bitmask[e] = 0; + world->gmemory++; } void add_components(World* world, Entity e, CType bits) { diff --git a/world.h b/world.h index fdc19c9..8494509 100644 --- a/world.h +++ b/world.h @@ -28,6 +28,7 @@ struct World { Map map; int cam_x, cam_y; + int gmemory, oom; }; void init_world(World* world); -- cgit v1.2.3-54-g00ecf