TODO about preupdate call style
[carveJwlIkooP6JGAAIwe30JlM.git] / ent_challenge.c
index bed85870c08d2ce0b1fa0f581bdfc85b8f6ebc36..861edbd6f60e10f9bac4b8ed9c12e093c9fee37a 100644 (file)
@@ -1,16 +1,18 @@
-#ifndef ENT_CHALLENGE_C
-#define ENT_CHALLENGE_C
-
+#include "vg/vg_engine.h"
 #include "entity.h"
 #include "input.h"
 #include "gui.h"
+#include "audio.h"
 
-VG_STATIC void ent_challenge_call( world_instance *world, ent_call *call ){
+entity_call_result ent_challenge_call( world_instance *world, ent_call *call )
+{
    u32 index = mdl_entity_id_id( call->id );
    ent_challenge *challenge = mdl_arritm( &world->ent_challenge, index );
 
-   if( call->function == 0 ){ /* unlock() */
-      if( !challenge->status ){
+   if( call->function == 0 ) /* unlock() */
+   {
+      if( !challenge->status )
+      {
          vg_info( "challenge( '%s' )\n", 
                   mdl_pstr( &world->meta, challenge->pstr_alias) );
          ent_call call;
@@ -20,46 +22,68 @@ VG_STATIC void ent_challenge_call( world_instance *world, ent_call *call ){
          entity_call( world, &call );
       }
       challenge->status = 1;
+      return k_entity_call_result_OK;
    }
-   else if( call->function == 1 ){  /* view() */
+   else if( call->function == 1 ) /* view() */
+   {
       if( (localplayer.subsystem == k_player_subsystem_walk) &&
-          (world_static.challenge_target == NULL) ){
+          (world_static.challenge_target == NULL) )
+      {
          world_static.challenge_target = NULL;
-         world_entity_focus( call->id );
+         world_entity_set_focus( call->id );
+         world_entity_focus_modal();
+
+         gui_helper_clear();
+         vg_str text;
+         if( gui_new_helper( input_button_list[k_srbind_maccept], &text ))
+            vg_strcat( &text, "start" );
+         if( gui_new_helper( input_button_list[k_srbind_mback], &text ))
+            vg_strcat( &text, "exit" );
       }
+      return k_entity_call_result_OK;
    }
-   else {
-      vg_print_backtrace();
-      vg_error( "Unhandled function id: %u\n", call->function );
-   }
+   else 
+      return k_entity_call_result_unhandled;
 }
 
-VG_STATIC void ent_challenge_preupdate( ent_challenge *challenge ){
-   world_instance *world = world_current_instance();
+void ent_challenge_preupdate( ent_focus_context *ctx )
+{
+   world_instance *world = ctx->world;
+   ent_challenge *challenge = mdl_arritm( &world->ent_challenge, ctx->index );
 
-   if( mdl_entity_id_type( challenge->camera ) == k_ent_camera ){
-      u32 index = mdl_entity_id_id( challenge->camera );
-      ent_camera *cam = mdl_arritm( &world->ent_camera, index );
+   /* maximum distance from active challenge */
+   if( !ctx->active )
+   {
+      f32 min_dist2 = 999999.9f;
 
-      /* TODO COMPRESSION */
-      v3f dir = {0.0f,-1.0f,0.0f};
-      mdl_transform_vector( &cam->transform, dir, dir );
-      m3x3_mulv( localplayer.invbasis, dir, dir );
-      player_vector_angles( world_static.focus_cam.angles, dir, 1.0f, 0.0f );
-      v3_copy( cam->transform.co, world_static.focus_cam.pos );
-      world_static.focus_cam.fov = cam->fov;
-   }
-   else {
-      /* TODO COMPRESSION */
-      v3_copy( localplayer.cam.pos, world_static.focus_cam.pos );
-      v3_copy( localplayer.cam.angles, world_static.focus_cam.angles );
-      world_static.focus_cam.fov = localplayer.cam.fov;
-      world_static.focus_cam.nearz = localplayer.cam.nearz;
-      world_static.focus_cam.farz = localplayer.cam.farz;
+      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 );
+            ent_objective *objective = mdl_arritm(&world->ent_objective,index);
+            next = objective->id_next;
+
+            f32 d2 = v3_dist2( localplayer.rb.co, objective->transform.co );
+            if( d2 < min_dist2 ) 
+               min_dist2 = d2;
+         }
+      }
+
+      f32 max_dist = 100.0f;
+      if( min_dist2 > max_dist*max_dist ){
+         world_static.challenge_target = NULL;
+         world_static.challenge_timer = 0.0f;
+         world_entity_clear_focus();
+         audio_lock();
+         audio_oneshot_3d( &audio_challenge[6], localplayer.rb.co,
+                           30.0f, 1.0f );
+         audio_unlock();
+      }
+      return;
    }
 
-   gui_helper_action( button_display_string( k_srbind_maccept ), "start" );
-   gui_helper_action( button_display_string( k_srbind_mback ), "exit" );
+   world_entity_focus_camera( world, challenge->camera );
 
    if( mdl_entity_id_type( challenge->first ) == k_ent_objective ){
       if( button_down( k_srbind_maccept ) ){
@@ -67,7 +91,8 @@ VG_STATIC void ent_challenge_preupdate( ent_challenge *challenge ){
          world_static.challenge_target = mdl_arritm( &world->ent_objective, 
                                                      index );
          world_static.challenge_timer = 0.0f;
-         world_entity_unfocus();
+         world_entity_exit_modal();
+         gui_helper_clear();
 
          u32 next = challenge->first;
          while( mdl_entity_id_type(next) == k_ent_objective ){
@@ -77,19 +102,26 @@ VG_STATIC void ent_challenge_preupdate( ent_challenge *challenge ){
             next = objective->id_next;
             v3_fill( objective->transform.s, 1.0f );
          }
+         audio_lock();
+         audio_oneshot( &audio_challenge[5], 1.0f, 0.0f );
+         audio_unlock();
          return;
       }
    }
 
-   if( button_down( k_srbind_mback ) ){
+   if( button_down( k_srbind_mback ) )
+   {
       world_static.challenge_target = NULL;
-      world_entity_unfocus();
+      world_entity_exit_modal();
+      world_entity_clear_focus();
+      gui_helper_clear();
+      audio_lock();
+      audio_oneshot( &audio_challenge[4], 1.0f, 0.0f );
+      audio_unlock();
       return;
    }
 }
 
-VG_STATIC void ent_challenge_render( ent_challenge *challenge ){
+static void ent_challenge_render( ent_challenge *challenge ){
    
 }
-
-#endif /* ENT_CHALLENGE_C */