move separate game updates into individual iterations

This commit is contained in:
jacob 2024-08-12 18:00:11 -05:00
parent c0517a7df6
commit 065986384e
2 changed files with 55 additions and 60 deletions

View File

@ -15,6 +15,7 @@ enum entity_prop {
ENTITY_PROP_WEAPON,
ENTITY_PROP_TRIGGERING_EQUIPPED,
ENTITY_PROP_TRIGGERED_THIS_TICK,
ENTITY_PROP_TRIGGERED_NEXT_TICK,
/* Test props */

View File

@ -222,7 +222,8 @@ INTERNAL void game_update(void)
/* Clear level */
case GAME_CMD_KIND_CLEAR_ALL: {
logf_info("Clearing level");
for (u64 i = 0; i < store->count; ++i) {
u64 count = store->count;
for (u64 i = 0; i < count; ++i) {
struct entity *ent = &store->entities[i];
if (ent->valid && !ent->is_root) {
entity_release(store, ent);
@ -249,9 +250,7 @@ INTERNAL void game_update(void)
if (!ent->valid) continue;
if (sprite_tag_is_nil(ent->sprite)) continue;
/* ========================== *
* Update sprite animation
* ========================== */
/* Update animation */
if (entity_has_prop(ent, ENTITY_PROP_ANIMATING)) {
struct sprite_sheet *sheet = sprite_sheet_from_tag_await(sprite_frame_scope, ent->sprite);
@ -278,9 +277,7 @@ INTERNAL void game_update(void)
ent->animation_frame = frame_index;
}
/* ========================== *
* Update sprite xform
* ========================== */
/* Update sprite xform */
struct sprite_sheet *sheet = sprite_sheet_from_tag_await(sprite_frame_scope, ent->sprite);
struct sprite_sheet_slice slice = sprite_sheet_get_slice(sheet, STR("pivot"), ent->animation_frame);
@ -297,28 +294,13 @@ INTERNAL void game_update(void)
}
/* ========================== *
* Update entities from cmds
* Update control from player cmds
* ========================== */
for (u64 entity_index = 0; entity_index < store->count; ++entity_index) {
struct entity *ent = &store->entities[entity_index];
if (!ent->valid) continue;
/* ========================== *
* Initialize test
* ========================== */
/* ENTITY_PROP_TEST */
if (entity_has_prop(ent, ENTITY_PROP_TEST) && !ent->test_initialized) {
ent->test_initialized = true;
ent->test_start_local_xform = entity_get_local_xform(ent);
ent->test_start_sprite_xform = ent->sprite_local_xform;
}
/* ========================== *
* Update control from player cmds
* ========================== */
if (entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED)) {
/* Process cmds */
struct v2 move = ent->control.move;
@ -368,13 +350,23 @@ INTERNAL void game_update(void)
entity_disable_prop(ent, ENTITY_PROP_TRIGGERING_EQUIPPED);
}
}
}
/* ========================== *
* Test
* ========================== */
/* ENTITY_PROP_TEST */
for (u64 entity_index = 0; entity_index < store->count; ++entity_index) {
struct entity *ent = &store->entities[entity_index];
if (!ent->valid) continue;
if (entity_has_prop(ent, ENTITY_PROP_TEST)) {
if (!ent->test_initialized) {
ent->test_initialized = true;
ent->test_start_local_xform = entity_get_local_xform(ent);
ent->test_start_sprite_xform = ent->sprite_local_xform;
}
f32 t = ((f32)G.tick.time);
struct v2 og = v2_mul(V2(math_cos(t), math_sin(t)), 3);
f32 r = t * 3;
@ -413,6 +405,21 @@ INTERNAL void game_update(void)
}
/* ========================== *
* Fire weapons
* ========================== */
for (u64 entity_index = 0; entity_index < store->count; ++entity_index) {
struct entity *ent = &store->entities[entity_index];
if (!ent->valid) continue;
if (entity_has_prop(ent, ENTITY_PROP_WEAPON)) {
if (entity_has_prop(ent, ENTITY_PROP_TRIGGERED_THIS_TICK)) {
ent->sprite_tint = RGBA_32_F(1, 0, 0, 1);
}
}
}
/* ========================== *
* Simulate entity physics
* ========================== */
@ -421,21 +428,7 @@ INTERNAL void game_update(void)
struct entity *ent = &store->entities[entity_index];
if (!ent->valid) continue;
/* ========================== *
* Weapon firing
* ========================== */
if (entity_has_prop(ent, ENTITY_PROP_WEAPON)) {
if (entity_has_prop(ent, ENTITY_PROP_TRIGGERED_THIS_TICK)) {
ent->sprite_tint = RGBA_32_F(1, 0, 0, 1);
} else {
ent->sprite_tint = RGBA_32_F(1, 1, 1, 1);
}
}
/* ========================== *
* Player angle
* ========================== */
/* Player angle */
if (entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED)) {
struct xform xf = entity_get_xform(ent);
@ -490,9 +483,7 @@ INTERNAL void game_update(void)
}
}
/* ========================== *
* Player movement
* ========================== */
/* Player movement */
if (entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED)) {
f32 max_speed = ent->player_max_speed;
@ -503,9 +494,7 @@ INTERNAL void game_update(void)
ent->acceleration = v2_mul(target_acceleration, acceleration_rate);
}
/* ========================== *
* Integrate acceleration & velocity
* ========================== */
/* Integrate acceleration & velocity */
{
f32 dt = (f32)G.tick.dt;
@ -522,17 +511,13 @@ INTERNAL void game_update(void)
}
/* ========================== *
* Update entities post-simulation
* Update camera position
* ========================== */
for (u64 entity_index = 0; entity_index < store->count; ++entity_index) {
struct entity *ent = &store->entities[entity_index];
if (!ent->valid) continue;
/* ========================== *
* Update camera position
* ========================== */
/* Camera follow */
if (entity_has_prop(ent, ENTITY_PROP_CAMERA)) {
struct entity *follow = entity_from_handle(store, ent->camera_follow);
@ -568,10 +553,16 @@ INTERNAL void game_update(void)
entity_set_xform(ent, xf);
}
}
/* ========================== *
* Update sound emitters
* ========================== */
for (u64 entity_index = 0; entity_index < store->count; ++entity_index) {
struct entity *ent = &store->entities[entity_index];
if (!ent->valid) continue;
if (entity_has_prop(ent, ENTITY_PROP_TEST_SOUND_EMITTER)) {
struct mixer_desc desc = ent->sound_desc;
desc.speed = G.tick.timescale;
@ -647,7 +638,10 @@ INTERNAL void game_update(void)
struct entity *ent = &store->entities[entity_index];
if (!ent->valid) continue;
if (entity_has_prop(ent, ENTITY_PROP_TRIGGERED_THIS_TICK)) {
if (entity_has_prop(ent, ENTITY_PROP_TRIGGERED_NEXT_TICK)) {
entity_disable_prop(ent, ENTITY_PROP_TRIGGERED_NEXT_TICK);
entity_enable_prop(ent, ENTITY_PROP_TRIGGERED_THIS_TICK);
} else if (entity_has_prop(ent, ENTITY_PROP_TRIGGERED_THIS_TICK)) {
entity_disable_prop(ent, ENTITY_PROP_TRIGGERED_THIS_TICK);
}
}