From 8afeea3b9a051a836c6cdf463a9ab07546eb65cd Mon Sep 17 00:00:00 2001 From: jacob Date: Wed, 7 Aug 2024 14:40:09 -0500 Subject: [PATCH] store user player aim as active camera focus --- src/game.c | 41 ++++++++++++++++++++++++++++++----------- src/user.c | 33 ++++++++++++--------------------- src/world.h | 2 +- 3 files changed, 43 insertions(+), 33 deletions(-) diff --git a/src/game.c b/src/game.c index 0c49d3fd..fca69f87 100644 --- a/src/game.c +++ b/src/game.c @@ -142,13 +142,15 @@ INTERNAL void spawn_test_entities(void) entity_set_xform(e, xf); e->sprite = sprite_tag_from_path(STR("res/graphics/tim.ase")); - e->sprite_span_name = STR("idle.unarmed"); + //e->sprite_span_name = STR("idle.unarmed"); //e->sprite_span_name = STR("idle.one_handed"); - //e->sprite_span_name = STR("idle.two_handed"); + e->sprite_span_name = STR("idle.two_handed"); e->sprite_tint = RGBA_32_F(1, 0, 0, 0.5); entity_enable_prop(e, ENTITY_PROP_ANIMATING); + entity_enable_prop(e, ENTITY_PROP_PLAYER_CONTROLLED); + //entity_enable_prop(e, ENTITY_PROP_TEST); } @@ -254,6 +256,8 @@ INTERNAL void game_update(void) struct temp_arena scratch = scratch_begin_no_conflict(); + struct entity_store *store = G.world.entity_store; + /* ========================== * * Begin frame cache scopes * ========================== */ @@ -288,16 +292,16 @@ INTERNAL void game_update(void) } break; case GAME_CMD_KIND_PLAYER_AIM: { - G.world.player_aim = cmd.aim; + G.world.camera_focus = cmd.aim; } break; /* Clear level */ case GAME_CMD_KIND_CLEAR_ALL: { logf_info("Clearing level"); - for (u64 i = 0; i < G.world.entity_store->count; ++i) { - struct entity *ent = &G.world.entity_store->entities[i]; + for (u64 i = 0; i < store->count; ++i) { + struct entity *ent = &store->entities[i]; if (ent->valid && !ent->is_root) { - entity_release(G.world.entity_store, ent); + entity_release(store, ent); } } } break; @@ -320,7 +324,7 @@ INTERNAL void game_update(void) /* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */ - struct entity_array entities_array = entity_store_as_array(G.world.entity_store); + struct entity_array entities_array = entity_store_as_array(store); /* ========================== * * Update sprite @@ -392,6 +396,21 @@ INTERNAL void game_update(void) } } + /* Find active camera */ + struct entity *active_camera; + { + enum entity_prop props[] = { ENTITY_PROP_CAMERA, ENTITY_PROP_CAMERA_ACTIVE }; + active_camera = entity_find_first_match_all(store, (struct entity_prop_array) { .count = ARRAY_COUNT(props), .props = props }); + } + + /* Update camera focus */ + if (active_camera->valid) { + active_camera->focus = G.world.camera_focus; + } + + /* Calculate player aim pos from camera focus */ + struct v2 player_aim_pos = v2_add(entity_get_xform(active_camera).og, G.world.camera_focus);; + for (u64 entity_index = 0; entity_index < entities_array.count; ++entity_index) { struct entity *ent = &entities_array.entities[entity_index]; if (!ent->valid) continue; @@ -412,11 +431,11 @@ INTERNAL void game_update(void) * ========================== */ if (entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED)) { - /* Update focus */ - ent->focus = G.world.player_aim; - struct xform xf = entity_get_xform(ent); + /* Update focus */ + ent->focus = v2_sub(player_aim_pos, xf.og); + /* Solve for final angle using law of sines */ f32 final_xf_angle; { @@ -549,7 +568,7 @@ INTERNAL void game_update(void) /* Camera follow */ if (entity_has_prop(ent, ENTITY_PROP_CAMERA)) { - struct entity *follow = entity_from_handle(G.world.entity_store, ent->camera_follow); + struct entity *follow = entity_from_handle(store, ent->camera_follow); struct xform xf = entity_get_xform(ent); diff --git a/src/user.c b/src/user.c index 4bfdfc3c..f9cd99cd 100644 --- a/src/user.c +++ b/src/user.c @@ -393,6 +393,8 @@ INTERNAL void user_update(void) .arena = scratch.arena }; + struct entity_store *store = G.world.entity_store; + /* Get time */ f64 cur_time = sys_timestamp_seconds(sys_timestamp()); G.dt = max_f64(0.0, cur_time - G.time); @@ -438,12 +440,12 @@ INTERNAL void user_update(void) /* Blend world globals */ G.world.time = math_lerp64(t0->time, t1->time, (f64)tick_blend); - G.world.player_aim = v2_lerp(t0->player_aim, t1->player_aim, tick_blend); + G.world.camera_focus = v2_lerp(t0->camera_focus, t1->camera_focus, tick_blend); /* Blend entities */ struct entity_array t0_entities = entity_store_as_array(t0->entity_store); struct entity_array t1_entities = entity_store_as_array(t1->entity_store); - struct entity_array world_entities = entity_store_as_array(G.world.entity_store); + struct entity_array world_entities = entity_store_as_array(store); u64 num_entities = min_u64(t0_entities.count, t1_entities.count); { @@ -477,28 +479,17 @@ INTERNAL void user_update(void) tick_is_first_frame = G.world.tick_id == 0; #endif } - struct entity_array entities_array = entity_store_as_array(G.world.entity_store); + + struct entity_array entities_array = entity_store_as_array(store); /* ========================== * * Find important entities * ========================== */ - struct entity *player = entity_nil(); - struct entity *active_camera = entity_nil(); - - for (u64 entity_index = 0; entity_index < entities_array.count; ++entity_index) { - struct entity *ent = &entities_array.entities[entity_index]; - if (!ent->valid) continue; - - /* Player */ - if (entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED)) { - player = ent; - } - - /* Active camera */ - if (entity_has_prop(ent, ENTITY_PROP_CAMERA) && entity_has_prop(ent, ENTITY_PROP_CAMERA_ACTIVE)) { - active_camera = ent; - } + struct entity *active_camera; + { + enum entity_prop props[] = { ENTITY_PROP_CAMERA, ENTITY_PROP_CAMERA_ACTIVE }; + active_camera = entity_find_first_match_all(store, (struct entity_prop_array) { .count = ARRAY_COUNT(props), .props = props }); } /* ========================== * @@ -775,7 +766,7 @@ INTERNAL void user_update(void) if (!ent->valid) continue; if (ent->is_root) continue; - struct entity *parent = entity_from_handle(G.world.entity_store, ent->parent); + struct entity *parent = entity_from_handle(store, ent->parent); struct xform xf = entity_get_xform(ent); struct xform parent_xf = entity_get_xform(parent); @@ -993,7 +984,7 @@ INTERNAL void user_update(void) /* Queue aim cmd */ if (!G.debug_camera) { - struct v2 input_aim = v2_sub(G.world_cursor, entity_get_xform(player).og); + struct v2 input_aim = v2_sub(G.world_cursor, entity_get_xform(active_camera).og); queue_game_cmd(&cmd_list, (struct game_cmd) { .kind = GAME_CMD_KIND_PLAYER_AIM, .aim = input_aim, diff --git a/src/world.h b/src/world.h index 6d6ede55..bbe24f7b 100644 --- a/src/world.h +++ b/src/world.h @@ -13,7 +13,7 @@ struct world { f64 time; struct v2 player_move_dir; - struct v2 player_aim; + struct v2 camera_focus; struct entity_store *entity_store; };