diff --git a/src/game/shared/game_world.c b/src/game/shared/game_world.c index 3dfc64e..1e410ea 100644 --- a/src/game/shared/game_world.c +++ b/src/game/shared/game_world.c @@ -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); diff --git a/src/game/shared/game_world.h b/src/game/shared/game_world.h index 2ca87a2..108a14c 100644 --- a/src/game/shared/game_world.h +++ b/src/game/shared/game_world.h @@ -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); diff --git a/src/game/simulation/simulation_world.c b/src/game/simulation/simulation_world.c index 7c92336..6b6f3b2 100644 --- a/src/game/simulation/simulation_world.c +++ b/src/game/simulation/simulation_world.c @@ -14,7 +14,8 @@ Simulation_World simulation_create_world(uint32_t seed, bool online, uint8_t max w.online = online; - w.game_world = game_world_create(seed, width, height); + uint16_t amount_of_food_to_spawn_initially = max_players; // NOTE: SS - One per player. Might change and/or be customizable. + w.game_world = game_world_create(seed, amount_of_food_to_spawn_initially, width, height); assert(w.game_world != NULL); w.match_state = Simulation_Match_State_Waiting_To_Start; diff --git a/src/todo.md b/src/todo.md index 6fa4bdf..91c9e1c 100644 --- a/src/todo.md +++ b/src/todo.md @@ -12,14 +12,10 @@ - Spawn players at a random position. - [x] When initially joining. - [ ] When dead and should respawn. -- Spawn food every N tick(?) - - [ ] Spawn food at a random position every N tick. - - Do we want a "max food on the board" value? - - Why? - - This can easily be implemented later. - - If we have that much food on the board, don't spawn new food. - - Otherwise, spawn new food at a random position. +- [x] Spawn a new food when one was eaten. - [x] When a player disconnects, all children should recursively be destroyed too. Not just the head. +- [ ] Send networked input. +- [ ] Set up session when joining the host. - [ ] Change player-movement? - Only inputs needed are RIGHT and LEFT. Turn relative to your head. - Right now we're moving relative to world-up.