move separate game updates into individual iterations
This commit is contained in:
parent
c0517a7df6
commit
065986384e
@ -15,6 +15,7 @@ enum entity_prop {
|
|||||||
ENTITY_PROP_WEAPON,
|
ENTITY_PROP_WEAPON,
|
||||||
ENTITY_PROP_TRIGGERING_EQUIPPED,
|
ENTITY_PROP_TRIGGERING_EQUIPPED,
|
||||||
ENTITY_PROP_TRIGGERED_THIS_TICK,
|
ENTITY_PROP_TRIGGERED_THIS_TICK,
|
||||||
|
ENTITY_PROP_TRIGGERED_NEXT_TICK,
|
||||||
|
|
||||||
/* Test props */
|
/* Test props */
|
||||||
|
|
||||||
|
|||||||
114
src/game.c
114
src/game.c
@ -222,7 +222,8 @@ INTERNAL void game_update(void)
|
|||||||
/* Clear level */
|
/* Clear level */
|
||||||
case GAME_CMD_KIND_CLEAR_ALL: {
|
case GAME_CMD_KIND_CLEAR_ALL: {
|
||||||
logf_info("Clearing level");
|
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];
|
struct entity *ent = &store->entities[i];
|
||||||
if (ent->valid && !ent->is_root) {
|
if (ent->valid && !ent->is_root) {
|
||||||
entity_release(store, ent);
|
entity_release(store, ent);
|
||||||
@ -249,9 +250,7 @@ INTERNAL void game_update(void)
|
|||||||
if (!ent->valid) continue;
|
if (!ent->valid) continue;
|
||||||
if (sprite_tag_is_nil(ent->sprite)) continue;
|
if (sprite_tag_is_nil(ent->sprite)) continue;
|
||||||
|
|
||||||
/* ========================== *
|
/* Update animation */
|
||||||
* Update sprite animation
|
|
||||||
* ========================== */
|
|
||||||
|
|
||||||
if (entity_has_prop(ent, ENTITY_PROP_ANIMATING)) {
|
if (entity_has_prop(ent, ENTITY_PROP_ANIMATING)) {
|
||||||
struct sprite_sheet *sheet = sprite_sheet_from_tag_await(sprite_frame_scope, ent->sprite);
|
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;
|
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 *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);
|
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) {
|
for (u64 entity_index = 0; entity_index < store->count; ++entity_index) {
|
||||||
struct entity *ent = &store->entities[entity_index];
|
struct entity *ent = &store->entities[entity_index];
|
||||||
if (!ent->valid) continue;
|
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)) {
|
if (entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED)) {
|
||||||
/* Process cmds */
|
/* Process cmds */
|
||||||
struct v2 move = ent->control.move;
|
struct v2 move = ent->control.move;
|
||||||
@ -368,13 +350,23 @@ INTERNAL void game_update(void)
|
|||||||
entity_disable_prop(ent, ENTITY_PROP_TRIGGERING_EQUIPPED);
|
entity_disable_prop(ent, ENTITY_PROP_TRIGGERING_EQUIPPED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* ========================== *
|
/* ========================== *
|
||||||
* Test
|
* Test
|
||||||
* ========================== */
|
* ========================== */
|
||||||
|
|
||||||
|
for (u64 entity_index = 0; entity_index < store->count; ++entity_index) {
|
||||||
|
struct entity *ent = &store->entities[entity_index];
|
||||||
|
if (!ent->valid) continue;
|
||||||
|
|
||||||
/* ENTITY_PROP_TEST */
|
|
||||||
if (entity_has_prop(ent, ENTITY_PROP_TEST)) {
|
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);
|
f32 t = ((f32)G.tick.time);
|
||||||
struct v2 og = v2_mul(V2(math_cos(t), math_sin(t)), 3);
|
struct v2 og = v2_mul(V2(math_cos(t), math_sin(t)), 3);
|
||||||
f32 r = t * 3;
|
f32 r = t * 3;
|
||||||
@ -389,8 +381,8 @@ INTERNAL void game_update(void)
|
|||||||
|
|
||||||
struct xform xf = entity_get_local_xform(ent);
|
struct xform xf = entity_get_local_xform(ent);
|
||||||
xf.og = og;
|
xf.og = og;
|
||||||
xf= xform_with_rotation(xf, r);
|
xf = xform_with_rotation(xf, r);
|
||||||
xf= xform_with_scale(xf, s);
|
xf = xform_with_scale(xf, s);
|
||||||
entity_set_local_xform(ent, xf);
|
entity_set_local_xform(ent, xf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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
|
* Simulate entity physics
|
||||||
* ========================== */
|
* ========================== */
|
||||||
@ -421,21 +428,7 @@ INTERNAL void game_update(void)
|
|||||||
struct entity *ent = &store->entities[entity_index];
|
struct entity *ent = &store->entities[entity_index];
|
||||||
if (!ent->valid) continue;
|
if (!ent->valid) continue;
|
||||||
|
|
||||||
/* ========================== *
|
/* Player angle */
|
||||||
* 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
|
|
||||||
* ========================== */
|
|
||||||
|
|
||||||
if (entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED)) {
|
if (entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED)) {
|
||||||
struct xform xf = entity_get_xform(ent);
|
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)) {
|
if (entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED)) {
|
||||||
f32 max_speed = ent->player_max_speed;
|
f32 max_speed = ent->player_max_speed;
|
||||||
@ -503,9 +494,7 @@ INTERNAL void game_update(void)
|
|||||||
ent->acceleration = v2_mul(target_acceleration, acceleration_rate);
|
ent->acceleration = v2_mul(target_acceleration, acceleration_rate);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ========================== *
|
/* Integrate acceleration & velocity */
|
||||||
* Integrate acceleration & velocity
|
|
||||||
* ========================== */
|
|
||||||
|
|
||||||
{
|
{
|
||||||
f32 dt = (f32)G.tick.dt;
|
f32 dt = (f32)G.tick.dt;
|
||||||
@ -522,18 +511,14 @@ INTERNAL void game_update(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* ========================== *
|
/* ========================== *
|
||||||
* Update entities post-simulation
|
* Update camera position
|
||||||
* ========================== */
|
* ========================== */
|
||||||
|
|
||||||
for (u64 entity_index = 0; entity_index < store->count; ++entity_index) {
|
for (u64 entity_index = 0; entity_index < store->count; ++entity_index) {
|
||||||
struct entity *ent = &store->entities[entity_index];
|
struct entity *ent = &store->entities[entity_index];
|
||||||
if (!ent->valid) continue;
|
if (!ent->valid) continue;
|
||||||
|
|
||||||
/* ========================== *
|
/* Camera follow */
|
||||||
* Update camera position
|
|
||||||
* ========================== */
|
|
||||||
|
|
||||||
/* Camera follow */
|
|
||||||
if (entity_has_prop(ent, ENTITY_PROP_CAMERA)) {
|
if (entity_has_prop(ent, ENTITY_PROP_CAMERA)) {
|
||||||
struct entity *follow = entity_from_handle(store, ent->camera_follow);
|
struct entity *follow = entity_from_handle(store, ent->camera_follow);
|
||||||
|
|
||||||
@ -568,9 +553,15 @@ INTERNAL void game_update(void)
|
|||||||
entity_set_xform(ent, xf);
|
entity_set_xform(ent, xf);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ========================== *
|
}
|
||||||
* Update sound emitters
|
|
||||||
* ========================== */
|
/* ========================== *
|
||||||
|
* 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)) {
|
if (entity_has_prop(ent, ENTITY_PROP_TEST_SOUND_EMITTER)) {
|
||||||
struct mixer_desc desc = ent->sound_desc;
|
struct mixer_desc desc = ent->sound_desc;
|
||||||
@ -647,7 +638,10 @@ INTERNAL void game_update(void)
|
|||||||
struct entity *ent = &store->entities[entity_index];
|
struct entity *ent = &store->entities[entity_index];
|
||||||
if (!ent->valid) continue;
|
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);
|
entity_disable_prop(ent, ENTITY_PROP_TRIGGERED_THIS_TICK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user