Spawn a new food when one was eaten. Initially, there's one food per player spawned.

This commit is contained in:
2025-12-18 13:50:27 +01:00
parent 49732d8b27
commit 5e6b042921
4 changed files with 38 additions and 30 deletions

View File

@@ -7,7 +7,7 @@
#define MIN_LEVEL_WIDTH 8 // TODO: SS - Move these out of here.
#define MIN_LEVEL_HEIGHT 8
Game_World *game_world_create(uint32_t seed, uint16_t level_width, uint16_t level_height) {
Game_World *game_world_create(uint32_t seed, uint8_t amount_of_food_to_spawn_initially, uint16_t level_width, uint16_t level_height) {
assert(level_width >= MIN_LEVEL_WIDTH); // We should probably have failed earlier.
assert(level_height >= MIN_LEVEL_HEIGHT);
@@ -28,21 +28,20 @@ Game_World *game_world_create(uint32_t seed, uint16_t level_width, uint16_t leve
grid_initialize(&w->grid, level_width, level_height);
{ // TEMP: SS - Testing ..
for(uint16_t i = 0; i < 8; i++) {
Entity_ID entity_food;
// Spawn initial food(s).
for(uint16_t i = 0; i < amount_of_food_to_spawn_initially; i++) {
Entity_ID entity_food;
uint16_t x = 0;
uint16_t y = 0;
assert(game_world_find_position_to_spawn(w, &x, &y));
assert(game_world_create_entity(
w,
Entity_Type_Food,
x, y,
&entity_food
));
}
uint16_t x = 0;
uint16_t y = 0;
assert(game_world_find_position_to_spawn(w, &x, &y));
assert(game_world_create_entity(
w,
Entity_Type_Food,
x, y,
&entity_food
));
}
return w;
@@ -270,7 +269,21 @@ void game_world_tick(Game_World *world) {
e->child = child_entity;
{ // Spawn food.
uint16_t food_spawn_x = 0;
uint16_t food_spawn_y = 0;
if(!game_world_find_position_to_spawn(world, &food_spawn_x, &food_spawn_y)) {
printf("Failed to find a position to spawn food.\n");
}
else {
Entity_ID entity_food;
assert(game_world_create_entity(
world,
Entity_Type_Food,
food_spawn_x, food_spawn_y,
&entity_food
));
}
}
}
}
@@ -295,10 +308,10 @@ bool game_world_find_position_to_spawn(Game_World *world, uint16_t *out_x, uint1
assert(out_x != NULL);
assert(out_y != NULL);
uint8_t attempts = MAX_ATTEMPTS_AT_FINDING_POSITION_TO_SPAWN;
uint32_t attempts = world->grid.width * world->grid.height;
while(attempts > 0) {
uint16_t potential_x = random_u32_range(&world->random_generator, 0, (uint32_t) world->grid.width);
uint16_t potential_y = random_u32_range(&world->random_generator, 0, (uint32_t) world->grid.height);
uint16_t potential_x = random_u32_range(&world->random_generator, 0, (uint32_t) world->grid.width - 1);
uint16_t potential_y = random_u32_range(&world->random_generator, 0, (uint32_t) world->grid.height - 1);
Grid_Cell *cell = grid_get_cell(&world->grid, potential_x, potential_y);
assert(cell != NULL);

View File

@@ -19,7 +19,7 @@ typedef struct {
} Game_World;
Game_World *game_world_create(uint32_t seed, uint16_t level_width, uint16_t level_height);
Game_World *game_world_create(uint32_t seed, uint8_t amount_of_food_to_spawn_initially, uint16_t level_width, uint16_t level_height);
void game_world_destroy(Game_World *world);
bool game_world_create_entity(Game_World *world, Entity_Type type, uint16_t x, uint16_t y, Entity_ID *out_entity_id);
@@ -31,8 +31,6 @@ void game_world_tick(Game_World *world);
Entity *game_world_try_get_entity_by_id(Game_World *world, Entity_ID id);
#define MAX_ATTEMPTS_AT_FINDING_POSITION_TO_SPAWN 8
bool game_world_find_position_to_spawn(Game_World *world, uint16_t *out_x, uint16_t *out_y);
Entity_Movement_Direction game_world_choose_initial_move_direction_based_on_coords(Game_World *world, uint16_t x, uint16_t y);