Implement world time single-stepping

isolation_bkp/dynres
Dominik Madarász 2021-10-27 01:28:18 +02:00
parent 4be5ee9b61
commit 276f85dee8
5 changed files with 813 additions and 715 deletions

View File

@ -95,6 +95,29 @@ static debug_item items[] = {
} }
} }
}, },
{
.kind = DITEM_LIST,
.name = "world simulation",
.list = {
.items = (debug_item[]) {
{ .kind = DITEM_COND, .on_success = CondIsWorldRunning },
{ .kind = DITEM_BUTTON, .name = "pause", .on_click = ActWorldToggleSim },
{ .kind = DITEM_COND, .on_success = CondIsWorldPaused, .skip = 6 },
{ .kind = DITEM_BUTTON, .name = "resume", .on_click = ActWorldToggleSim },
{ .kind = DITEM_GAP },
{ .kind = DITEM_TEXT, .name = "step size", .proc = DrawWorldStepSize },
{ .kind = DITEM_BUTTON, .name = "single-step", .on_click = ActWorldStep },
{ .kind = DITEM_BUTTON, .name = "increment step size", .on_click = ActWorldIncrementSimStepSize },
{ .kind = DITEM_BUTTON, .name = "decrement step size", .on_click = ActWorldDecrementSimStepSize },
{ .kind = DITEM_END },
},
.is_sp_only = true,
}
},
{ {
.kind = DITEM_LIST, .kind = DITEM_LIST,
.name = "debug actions", .name = "debug actions",

View File

@ -233,3 +233,42 @@ ActDespawnDemoNPCs(void) {
zpl_array_free(demo_npcs); zpl_array_free(demo_npcs);
demo_npcs = 0; demo_npcs = 0;
} }
// NOTE(zaklaus): world simulation controls
#define WORLDSIM_STEPPING 0.01f
static float sim_step_size = 0.1f;
void
ActWorldToggleSim(void) {
if (world_is_paused()) {
world_resume();
} else {
world_pause();
}
}
void
ActWorldIncrementSimStepSize(void) {
sim_step_size += WORLDSIM_STEPPING;
}
void
ActWorldDecrementSimStepSize(void) {
if (sim_step_size > WORLDSIM_STEPPING)
sim_step_size -= WORLDSIM_STEPPING;
}
void
ActWorldStep(void) {
world_step(sim_step_size);
}
uint8_t
CondIsWorldPaused(void) {
return world_is_paused();
}
uint8_t
CondIsWorldRunning(void) {
return !world_is_paused();
}

View File

@ -91,3 +91,11 @@ DrawDemoNPCCount(debug_item *it, float xpos, float ypos) {
(void)it; (void)it;
return DrawFormattedText(xpos, ypos, TextFormat("%d", demo_npcs ? zpl_array_count(demo_npcs) : 0)); return DrawFormattedText(xpos, ypos, TextFormat("%d", demo_npcs ? zpl_array_count(demo_npcs) : 0));
} }
// NOTE(zaklaus): world simulation
static inline debug_draw_result
DrawWorldStepSize(debug_item *it, float xpos, float ypos) {
(void)it;
return DrawFormattedText(xpos, ypos, TextFormat("%d ms", (int16_t)(sim_step_size*1000.f)));
}

View File

@ -147,6 +147,7 @@ int32_t world_init(int32_t seed, uint16_t chunk_size, uint16_t chunk_amount) {
return 0; return 0;
} }
world.is_paused = false;
world.seed = seed; world.seed = seed;
world.chunk_size = chunk_size; world.chunk_size = chunk_size;
world.chunk_amount = chunk_amount; world.chunk_amount = chunk_amount;
@ -349,6 +350,26 @@ librg_world *world_tracker() {
return world.tracker; return world.tracker;
} }
void world_pause(void) {
ecs_set_time_scale(world.ecs, 0.0f);
world.is_paused = true;
}
void world_resume(void) {
ecs_set_time_scale(world.ecs, 1.0f);
world.is_paused = false;
}
bool world_is_paused(void) {
return world.is_paused;
}
void world_step(float step_size) {
world_resume();
ecs_progress(world.ecs, step_size);
world_pause();
}
uint16_t world_chunk_size(void) { uint16_t world_chunk_size(void) {
return world.chunk_size; return world.chunk_size;
} }

View File

@ -30,6 +30,7 @@ typedef WORLD_PKT_READER(world_pkt_reader_proc);
typedef WORLD_PKT_WRITER(world_pkt_writer_proc); typedef WORLD_PKT_WRITER(world_pkt_writer_proc);
typedef struct { typedef struct {
bool is_paused;
uint8_t *data; uint8_t *data;
uint32_t seed; uint32_t seed;
uint32_t size; uint32_t size;
@ -63,6 +64,12 @@ ecs_world_t *world_ecs(void);
void world_set_stage(ecs_world_t *ecs); void world_set_stage(ecs_world_t *ecs);
librg_world *world_tracker(void); librg_world *world_tracker(void);
// NOTE(zaklaus): World simulation time control
void world_pause(void);
void world_resume(void);
bool world_is_paused(void);
void world_step(float step_size);
uint16_t world_chunk_size(void); uint16_t world_chunk_size(void);
uint16_t world_chunk_amount(void); uint16_t world_chunk_amount(void);
uint16_t world_dim(void); uint16_t world_dim(void);