store user player aim as active camera focus

This commit is contained in:
jacob 2024-08-07 14:40:09 -05:00
parent 0f113f640f
commit 8afeea3b9a
3 changed files with 43 additions and 33 deletions

View File

@ -142,13 +142,15 @@ INTERNAL void spawn_test_entities(void)
entity_set_xform(e, xf); entity_set_xform(e, xf);
e->sprite = sprite_tag_from_path(STR("res/graphics/tim.ase")); 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.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); e->sprite_tint = RGBA_32_F(1, 0, 0, 0.5);
entity_enable_prop(e, ENTITY_PROP_ANIMATING); entity_enable_prop(e, ENTITY_PROP_ANIMATING);
entity_enable_prop(e, ENTITY_PROP_PLAYER_CONTROLLED);
//entity_enable_prop(e, ENTITY_PROP_TEST); //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 temp_arena scratch = scratch_begin_no_conflict();
struct entity_store *store = G.world.entity_store;
/* ========================== * /* ========================== *
* Begin frame cache scopes * Begin frame cache scopes
* ========================== */ * ========================== */
@ -288,16 +292,16 @@ INTERNAL void game_update(void)
} break; } break;
case GAME_CMD_KIND_PLAYER_AIM: { case GAME_CMD_KIND_PLAYER_AIM: {
G.world.player_aim = cmd.aim; G.world.camera_focus = cmd.aim;
} break; } break;
/* 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 < G.world.entity_store->count; ++i) { for (u64 i = 0; i < store->count; ++i) {
struct entity *ent = &G.world.entity_store->entities[i]; struct entity *ent = &store->entities[i];
if (ent->valid && !ent->is_root) { if (ent->valid && !ent->is_root) {
entity_release(G.world.entity_store, ent); entity_release(store, ent);
} }
} }
} break; } 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 * 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) { for (u64 entity_index = 0; entity_index < entities_array.count; ++entity_index) {
struct entity *ent = &entities_array.entities[entity_index]; struct entity *ent = &entities_array.entities[entity_index];
if (!ent->valid) continue; if (!ent->valid) continue;
@ -412,11 +431,11 @@ INTERNAL void game_update(void)
* ========================== */ * ========================== */
if (entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED)) { if (entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED)) {
/* Update focus */
ent->focus = G.world.player_aim;
struct xform xf = entity_get_xform(ent); 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 */ /* Solve for final angle using law of sines */
f32 final_xf_angle; f32 final_xf_angle;
{ {
@ -549,7 +568,7 @@ INTERNAL void game_update(void)
/* Camera follow */ /* Camera follow */
if (entity_has_prop(ent, ENTITY_PROP_CAMERA)) { 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); struct xform xf = entity_get_xform(ent);

View File

@ -393,6 +393,8 @@ INTERNAL void user_update(void)
.arena = scratch.arena .arena = scratch.arena
}; };
struct entity_store *store = G.world.entity_store;
/* Get time */ /* Get time */
f64 cur_time = sys_timestamp_seconds(sys_timestamp()); f64 cur_time = sys_timestamp_seconds(sys_timestamp());
G.dt = max_f64(0.0, cur_time - G.time); G.dt = max_f64(0.0, cur_time - G.time);
@ -438,12 +440,12 @@ INTERNAL void user_update(void)
/* Blend world globals */ /* Blend world globals */
G.world.time = math_lerp64(t0->time, t1->time, (f64)tick_blend); 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 */ /* Blend entities */
struct entity_array t0_entities = entity_store_as_array(t0->entity_store); 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 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); 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; tick_is_first_frame = G.world.tick_id == 0;
#endif #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 * Find important entities
* ========================== */ * ========================== */
struct entity *player = entity_nil(); struct entity *active_camera;
struct entity *active_camera = entity_nil(); {
enum entity_prop props[] = { ENTITY_PROP_CAMERA, ENTITY_PROP_CAMERA_ACTIVE };
for (u64 entity_index = 0; entity_index < entities_array.count; ++entity_index) { active_camera = entity_find_first_match_all(store, (struct entity_prop_array) { .count = ARRAY_COUNT(props), .props = props });
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;
}
} }
/* ========================== * /* ========================== *
@ -775,7 +766,7 @@ INTERNAL void user_update(void)
if (!ent->valid) continue; if (!ent->valid) continue;
if (ent->is_root) 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 xf = entity_get_xform(ent);
struct xform parent_xf = entity_get_xform(parent); struct xform parent_xf = entity_get_xform(parent);
@ -993,7 +984,7 @@ INTERNAL void user_update(void)
/* Queue aim cmd */ /* Queue aim cmd */
if (!G.debug_camera) { 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) { queue_game_cmd(&cmd_list, (struct game_cmd) {
.kind = GAME_CMD_KIND_PLAYER_AIM, .kind = GAME_CMD_KIND_PLAYER_AIM,
.aim = input_aim, .aim = input_aim,

View File

@ -13,7 +13,7 @@ struct world {
f64 time; f64 time;
struct v2 player_move_dir; struct v2 player_move_dir;
struct v2 player_aim; struct v2 camera_focus;
struct entity_store *entity_store; struct entity_store *entity_store;
}; };