+#include "vg/vg_msg.h"
+#include "menu.h"
+#include "ent_challenge.h"
+#include "ent_skateshop.h"
+#include "ent_route.h"
+#include "ent_traffic.h"
+#include "ent_glider.h"
+#include "ent_region.h"
+#include "ent_npc.h"
+#include "input.h"
+#include "player_walk.h"
+
+bh_system bh_system_entity_list =
+{
+ .expand_bound = entity_bh_expand_bound,
+ .item_centroid = entity_bh_centroid,
+ .item_closest = entity_bh_closest,
+ .item_swap = entity_bh_swap,
+ .item_debug = entity_bh_debug,
+ .cast_ray = NULL
+};
+
+void world_entity_set_focus( u32 entity_id )
+{
+ if( world_static.focused_entity )
+ {
+ vg_warn( "Entity %u#%u tried to take focus from %u#%u\n",
+ mdl_entity_id_type( entity_id ),
+ mdl_entity_id_id( entity_id ),
+ mdl_entity_id_type( world_static.focused_entity ),
+ mdl_entity_id_id( world_static.focused_entity ) );
+ return;
+ }
+
+ world_static.focused_entity = entity_id;
+}
+
+void world_entity_focus_modal(void)
+{
+ localplayer.immobile = 1;
+ menu.disable_open = 1;
+ srinput.state = k_input_state_resume;
+
+ v3_zero( localplayer.rb.v );
+ v3_zero( localplayer.rb.w );
+ player_walk.move_speed = 0.0f;
+ skaterift.activity = k_skaterift_ent_focus;
+}
+
+void world_entity_exit_modal(void)
+{
+ if( skaterift.activity != k_skaterift_ent_focus )
+ {
+ vg_warn( "Entity %u#%u tried to exit modal when we weren't in one\n",
+ mdl_entity_id_type( world_static.focused_entity ),
+ mdl_entity_id_id( world_static.focused_entity ) );
+ return;
+ }
+
+ localplayer.immobile = 0;
+ menu.disable_open = 0;
+ srinput.state = k_input_state_resume;
+ skaterift.activity = k_skaterift_default;
+}
+
+void world_entity_clear_focus(void)
+{
+ if( skaterift.activity == k_skaterift_ent_focus )
+ {
+ vg_warn( "Entity %u#%u tried to clear focus before exiting modal\n",
+ mdl_entity_id_type( world_static.focused_entity ),
+ mdl_entity_id_id( world_static.focused_entity ) );
+ return;
+ }
+
+ world_static.focused_entity = 0;
+}
+
+void world_entity_focus_camera( world_instance *world, u32 uid )
+{
+ if( mdl_entity_id_type( uid ) == k_ent_camera ){
+ u32 index = mdl_entity_id_id( uid );
+ ent_camera *cam = mdl_arritm( &world->ent_camera, index );
+
+ v3f dir = {0.0f,-1.0f,0.0f};
+ mdl_transform_vector( &cam->transform, dir, dir );
+ v3_angles( dir, world_static.focus_cam.angles );
+ v3_copy( cam->transform.co, world_static.focus_cam.pos );
+ world_static.focus_cam.fov = cam->fov;
+ }
+ else {
+ vg_camera_copy( &localplayer.cam, &world_static.focus_cam );
+
+ /* TODO ? */
+ world_static.focus_cam.nearz = localplayer.cam.nearz;
+ world_static.focus_cam.farz = localplayer.cam.farz;
+ }
+}
+
+/* logic preupdate */
+void world_entity_focus_preupdate(void)
+{
+ f32 rate = vg_minf( 1.0f, vg.time_frame_delta * 2.0f );
+ int active = 0;
+ if( skaterift.activity == k_skaterift_ent_focus )
+ active = 1;
+
+ 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();
+
+ static void (*table[])( ent_focus_context *ctx ) =
+ {
+ [ 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) )
+ {
+ 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 } );
+}