From 8d336ea2cde7c596296dbaf0d3ce27a82c6c6cf0 Mon Sep 17 00:00:00 2001 From: hgn Date: Wed, 20 Mar 2024 10:55:16 +0000 Subject: [PATCH] TODO about preupdate call style --- ent_challenge.c | 11 +++++++---- ent_challenge.h | 2 +- ent_npc.c | 7 ++++--- ent_npc.h | 2 +- ent_route.c | 10 ++++++---- ent_route.h | 2 +- ent_skateshop.c | 9 +++++---- ent_skateshop.h | 2 +- world_entity.c | 37 +++++++++++++++++++------------------ world_entity.h | 8 ++++++++ 10 files changed, 53 insertions(+), 37 deletions(-) diff --git a/ent_challenge.c b/ent_challenge.c index 9499d0f..861edbd 100644 --- a/ent_challenge.c +++ b/ent_challenge.c @@ -46,15 +46,18 @@ entity_call_result ent_challenge_call( world_instance *world, ent_call *call ) return k_entity_call_result_unhandled; } -void ent_challenge_preupdate( ent_challenge *challenge, int active ) +void ent_challenge_preupdate( ent_focus_context *ctx ) { - world_instance *world = world_current_instance(); + world_instance *world = ctx->world; + ent_challenge *challenge = mdl_arritm( &world->ent_challenge, ctx->index ); /* maximum distance from active challenge */ - if( !active ){ + if( !ctx->active ) + { f32 min_dist2 = 999999.9f; - if( mdl_entity_id_type( challenge->first ) == k_ent_objective ){ + if( mdl_entity_id_type( challenge->first ) == k_ent_objective ) + { u32 next = challenge->first; while( mdl_entity_id_type(next) == k_ent_objective ){ u32 index = mdl_entity_id_id( next ); diff --git a/ent_challenge.h b/ent_challenge.h index d5108a8..f53c956 100644 --- a/ent_challenge.h +++ b/ent_challenge.h @@ -1,5 +1,5 @@ #pragma once #include "entity.h" -void ent_challenge_preupdate( ent_challenge *challenge, int active ); +void ent_challenge_preupdate( ent_focus_context *ctx ); entity_call_result ent_challenge_call( world_instance *world, ent_call *call ); diff --git a/ent_npc.c b/ent_npc.c index 8fccd29..7a4b484 100644 --- a/ent_npc.c +++ b/ent_npc.c @@ -177,11 +177,12 @@ entity_call_result ent_npc_call( world_instance *world, ent_call *call ) } } -void ent_npc_preupdate( ent_npc *ent, int active ) +void ent_npc_preupdate( ent_focus_context *ctx ) { - world_instance *world = world_current_instance(); + world_instance *world = ctx->world; + ent_npc *ent = mdl_arritm( &world->ent_npc, ctx->index ); - if( !active ) + if( !ctx->active ) { if( button_down(k_srbind_maccept) ) { diff --git a/ent_npc.h b/ent_npc.h index 648f447..c20cc97 100644 --- a/ent_npc.h +++ b/ent_npc.h @@ -21,7 +21,7 @@ enum npc_id }; void npc_load_model( struct npc *npc, const char *path ); -void ent_npc_preupdate( ent_npc *ent, int active ); +void ent_npc_preupdate( ent_focus_context *context ); entity_call_result ent_npc_call( world_instance *world, ent_call *call ); void npc_update( ent_npc *ent ); void npc_render( ent_npc *ent, world_instance *world, vg_camera *cam ); diff --git a/ent_route.c b/ent_route.c index 026bf70..f5b51c5 100644 --- a/ent_route.c +++ b/ent_route.c @@ -37,12 +37,14 @@ entity_call_result ent_route_call( world_instance *world, ent_call *call ) return k_entity_call_result_unhandled; } -/* TODO: these should recieve the world instance */ -void ent_route_preupdate( ent_route *route, int active ) +void ent_route_preupdate( ent_focus_context *ctx ) { - if( !active ) return; + if( !ctx->active ) + return; + + world_instance *world = ctx->world; + ent_route *route = mdl_arritm( &world->ent_route, ctx->index ); - world_instance *world = world_current_instance(); u32 cam_id = 0; if( __builtin_expect( world->meta.info.version >= 103, 1 ) ) diff --git a/ent_route.h b/ent_route.h index 7c5dfa3..cbf61b2 100644 --- a/ent_route.h +++ b/ent_route.h @@ -8,4 +8,4 @@ struct global_ent_route extern global_ent_route; entity_call_result ent_route_call( world_instance *world, ent_call *call ); -void ent_route_preupdate( ent_route *route, int active ); +void ent_route_preupdate( ent_focus_context *ctx ); diff --git a/ent_skateshop.c b/ent_skateshop.c index 1065d4b..05999f9 100644 --- a/ent_skateshop.c +++ b/ent_skateshop.c @@ -209,12 +209,13 @@ static void skateshop_server_helper_update(void){ * VG event preupdate */ void temp_update_playermodel(void); -void ent_skateshop_preupdate( ent_skateshop *shop, int active ) +void ent_skateshop_preupdate( ent_focus_context *ctx ) { - if( !active ) return; + if( !ctx->active ) + return; - /* input filter */ - world_instance *world = world_current_instance(); + world_instance *world = ctx->world; + ent_skateshop *shop = mdl_arritm( &world->ent_skateshop, ctx->index ); /* camera positioning */ ent_camera *ref = mdl_arritm( &world->ent_camera, diff --git a/ent_skateshop.h b/ent_skateshop.h index ed03ecd..2f8e3a6 100644 --- a/ent_skateshop.h +++ b/ent_skateshop.h @@ -45,7 +45,7 @@ struct global_skateshop extern global_skateshop; void skateshop_init(void); -void ent_skateshop_preupdate( ent_skateshop *shop, int active ); +void ent_skateshop_preupdate( ent_focus_context *ctx ); void skateshop_render( ent_skateshop *shop ); void skateshop_render_nonfocused( world_instance *world, vg_camera *cam ); void skateshop_autostart_loading(void); diff --git a/world_entity.c b/world_entity.c index 13c89a0..d525572 100644 --- a/world_entity.c +++ b/world_entity.c @@ -115,31 +115,32 @@ void world_entity_focus_preupdate(void) vg_slewf( &world_static.focus_strength, active, vg.time_frame_delta * (1.0f/0.5f) ); + if( world_static.focused_entity == 0 ) + return; + u32 type = mdl_entity_id_type( world_static.focused_entity ), index = mdl_entity_id_id( world_static.focused_entity ); + world_instance *world = world_current_instance(); - /* TODO: Table. */ - if( type == k_ent_skateshop ) - { - ent_skateshop *skateshop = mdl_arritm( &world->ent_skateshop, index ); - ent_skateshop_preupdate( skateshop, active ); - } - else if( type == k_ent_challenge ) - { - ent_challenge *challenge = mdl_arritm( &world->ent_challenge, index ); - ent_challenge_preupdate( challenge, active ); - } - else if( type == k_ent_route ) + static void (*table[])( ent_focus_context *ctx ) = { - ent_route *route = mdl_arritm( &world->ent_route, index ); - ent_route_preupdate( route, active ); - } - else if( type == k_ent_npc ) + [ k_ent_skateshop ] = ent_skateshop_preupdate, + [ k_ent_challenge ] = ent_challenge_preupdate, + [ k_ent_route ] = ent_route_preupdate, + [ k_ent_npc ] = ent_npc_preupdate, + }; + + if( (type > vg_list_size(table)) || (table[type] == NULL) ) { - ent_npc *npc = mdl_arritm( &world->ent_npc, index ); - ent_npc_preupdate( npc, active ); + vg_fatal_error( "No pre-update method set for entity (%u#%u)\n", + type, index ); } + + table[type]( &(ent_focus_context){ + .world = world, + .index = index, + .active = active } ); } /* additional renderings like text etc.. */ diff --git a/world_entity.h b/world_entity.h index 5f784d2..c954052 100644 --- a/world_entity.h +++ b/world_entity.h @@ -4,6 +4,14 @@ #include "vg/vg_bvh.h" #include "vg/vg_msg.h" +typedef struct ent_focus_context ent_focus_context; +struct ent_focus_context +{ + world_instance *world; + u32 index; /* Array index of the focused entity */ + bool active; +}; + void world_gen_entities_init( world_instance *world ); ent_spawn *world_find_spawn_by_name( world_instance *world, const char *name ); -- 2.25.1