whoopsie
[carveJwlIkooP6JGAAIwe30JlM.git] / player_walk.c
index c86c9dba385d28ac8f3da4b0a22ad0013659c59b..75e3a379261e5ac7b1a8b0e44d9f94aa51e6ca14 100644 (file)
@@ -118,6 +118,7 @@ VG_STATIC void player_walk_drop_in_overhang_transform( player_instance *player,
 VG_STATIC int player_walk_scan_for_drop_in( player_instance *player )
 {
    struct player_walk *w = &player->_walk;
+   world_instance *world = get_active_world();
 
    v3f dir, center;
    q_mulv( player->rb.q, (v3f){0.0f,0.0f,1.0f}, dir );
@@ -140,7 +141,7 @@ VG_STATIC int player_walk_scan_for_drop_in( player_instance *player )
       ray_hit *ray = &samples[ sample_count ];
       ray->dist = 2.0f;
 
-      if( ray_world( pos, ray_dir, ray ) )
+      if( ray_world( world, pos, ray_dir, ray ) )
       {
          vg_line( pos, ray->pos, VG__RED );
          vg_line_pt3( ray->pos, 0.025f, VG__BLACK );
@@ -218,7 +219,7 @@ VG_STATIC int player_walk_scan_for_drop_in( player_instance *player )
          ray_hit ray;
          ray.dist = k_board_length*2.0f + 0.6f;
          
-         if( ray_world( va, v0, &ray ) )
+         if( ray_world( world, va, v0, &ray ) )
          {
             vg_line( va, vb, VG__RED );
             vg_line_pt3( ray.pos, 0.1f, VG__RED );
@@ -227,7 +228,7 @@ VG_STATIC int player_walk_scan_for_drop_in( player_instance *player )
          }
 
          v3_muls( v0, -1.0f, v0 );
-         if( ray_world( vb, v0, &ray ) )
+         if( ray_world( world, vb, v0, &ray ) )
          {
             vg_line( va, vb, VG__RED );
             vg_line_pt3( ray.pos, 0.1f, VG__RED );
@@ -395,8 +396,10 @@ VG_STATIC void player__walk_update( player_instance *player )
    /* 
     * Collision detection
     */
+   world_instance *world = get_active_world();
+
    len = rb_capsule__scene( mtx, &w->collider, NULL, 
-                            &world.rb_geo.inf.scene, manifold );
+                            &world->rb_geo.inf.scene, manifold );
    rb_manifold_filter_coplanar( manifold, len, 0.01f );
    len = rb_manifold_apply_filtered( manifold, len );
 
@@ -405,6 +408,8 @@ VG_STATIC void player__walk_update( player_instance *player )
    if( w->state.activity != k_walk_activity_lockedmove )
       w->state.activity = k_walk_activity_air;
 
+   w->surface = k_surface_prop_concrete;
+
    for( int i=0; i<len; i++ )
    {
       struct contact *ct = &manifold[i];
@@ -416,6 +421,10 @@ VG_STATIC void player__walk_update( player_instance *player )
             w->state.activity = k_walk_activity_ground;
 
          v3_add( surface_avg, ct->n, surface_avg );
+
+         struct world_surface *surf = world_contact_surface( world, ct );
+         if( surf->info.surface_prop > w->surface )
+            w->surface = surf->info.surface_prop;
       }
 
       rb_prepare_contact( ct, k_rb_delta );
@@ -429,15 +438,13 @@ VG_STATIC void player__walk_update( player_instance *player )
    v3_muls( right_dir,  walk[0], movedir );
    v3_muladds( movedir, forward_dir, walk[1], movedir );
 
-   if( w->state.activity == k_walk_activity_ground )
-   {
+   if( w->state.activity == k_walk_activity_ground ){
       v3_normalize( surface_avg );
 
       v3f tx, ty;
       rb_tangent_basis( surface_avg, tx, ty );
 
-      if( v2_length2(walk) > 0.001f )
-      {
+      if( v2_length2(walk) > 0.001f ){
          /* clip movement to the surface */
          float d = v3_dot(surface_avg,movedir);
          v3_muladds( movedir, surface_avg, -d, movedir );
@@ -447,29 +454,22 @@ VG_STATIC void player__walk_update( player_instance *player )
       nominal_speed = k_walkspeed;
 
       /* jump */
-      if( player->input_jump->button.value )
-      {
+      if( player->input_jump->button.value ){
          v3_muladds( player->rb.v, player->basis[1], 5.0f, player->rb.v );
          w->state.activity = k_walk_activity_air;
          accel_speed = k_walk_air_accel;
          nominal_speed = k_airspeed;
       }
-      else
-      {
+      else{
          player_friction( player->rb.v );
-
-         struct world_material *surface_mat = world_contact_material(manifold);
-         w->surface = surface_mat->info.surface_prop;
       }
    }
-   else
-   {
+   else{
       accel_speed = k_walk_air_accel;
       nominal_speed = k_airspeed;
    }
 
-   if( v2_length2(walk) > 0.001f )
-   {
+   if( v2_length2(walk) > 0.001f ){
       player_accelerate( player->rb.v, movedir, nominal_speed, accel_speed );
       v3_normalize( movedir );
    }
@@ -477,10 +477,8 @@ VG_STATIC void player__walk_update( player_instance *player )
    /*
     * Resolve velocity constraints
     */
-   for( int j=0; j<5; j++ )
-   {
-      for( int i=0; i<len; i++ )
-      {
+   for( int j=0; j<5; j++ ){
+      for( int i=0; i<len; i++ ){
          struct contact *ct = &manifold[i];
          
          /*normal */
@@ -565,7 +563,7 @@ VG_STATIC void player__walk_update( player_instance *player )
       float t, sr = w->collider.radius-0.04f;
       v3f n;
 
-      if( spherecast_world( lwr_prev, lwr_now, sr, &t, n ) != -1 )
+      if( spherecast_world( world, lwr_prev, lwr_now, sr, &t, n ) != -1 )
       {
          v3_lerp( lwr_prev, lwr_now, vg_maxf(0.01f,t), player->rb.co );
          player->rb.co[1] -= w->collider.radius;
@@ -576,9 +574,9 @@ VG_STATIC void player__walk_update( player_instance *player )
       }
    }
 
-   teleport_gate *gate;
-   if( (gate = world_intersect_gates( player->rb.co, w->state.prev_pos )) )
-   {
+   ent_gate *gate = 
+      world_intersect_gates( world, player->rb.co, w->state.prev_pos );
+   if( gate ){
       m4x3_mulv( gate->transport, player->rb.co, player->rb.co );
       m3x3_mulv( gate->transport, player->rb.v,  player->rb.v );
 
@@ -638,6 +636,40 @@ VG_STATIC void player__walk_post_update( player_instance *player )
 
    v3_add( player->rb.co, p1, p1 );
    vg_line( player->rb.co, p1, VG__PINK );
+
+
+   int walk_phase = 0;
+   if( vg_fractf(w->walk_timer) > 0.5f )
+      walk_phase = 1;
+   else
+      walk_phase = 0;
+
+   if( (w->step_phase != walk_phase) && 
+       (w->state.activity == k_walk_activity_ground ) )
+   {
+      audio_lock();
+      if( w->surface == k_surface_prop_concrete ){
+         audio_oneshot_3d( 
+               &audio_footsteps[rand()%vg_list_size(audio_footsteps)], 
+               player->rb.co, 40.0f, 1.0f 
+         );
+      }
+      else if( w->surface == k_surface_prop_grass ){
+         audio_oneshot_3d( 
+           &audio_footsteps_grass[rand()%vg_list_size(audio_footsteps_grass)],
+            player->rb.co, 40.0f, 1.0f 
+         );
+      }
+      else if( w->surface == k_surface_prop_wood ){
+         audio_oneshot_3d( 
+           &audio_footsteps_wood[rand()%vg_list_size(audio_footsteps_wood)],
+            player->rb.co, 40.0f, 1.0f 
+         );
+      }
+      audio_unlock();
+   }
+
+   w->step_phase = walk_phase;
 }
 
 VG_STATIC void player__walk_animate( player_instance *player, 
@@ -664,8 +696,7 @@ VG_STATIC void player__walk_animate( player_instance *player,
 
    player_pose apose, bpose;
 
-   if( w->move_speed > 0.025f )
-   {
+   if( w->move_speed > 0.025f ){
       /* TODO move */
       float walk_norm = 30.0f/(float)w->anim_walk->length,
             run_norm  = 30.0f/(float)w->anim_run->length,
@@ -673,8 +704,7 @@ VG_STATIC void player__walk_animate( player_instance *player,
 
       w->walk_timer += walk_adv * vg.time_delta;
    }
-   else
-   {
+   else{
       w->walk_timer = 0.0f;
    }
 
@@ -703,8 +733,7 @@ VG_STATIC void player__walk_animate( player_instance *player,
 
    float walk_yaw = player_get_heading_yaw( player );
 
-   if( w->state.outro_anim )
-   {
+   if( w->state.outro_anim ){
       struct player_avatar *av = player->playeravatar;
       float outro_length = (float)w->state.outro_anim->length /
                                   w->state.outro_anim->rate,
@@ -716,8 +745,7 @@ VG_STATIC void player__walk_animate( player_instance *player,
                                     outro_time, bpose );
       skeleton_lerp_pose( sk, apose, bpose, outro_t * 10.0f, dest->pose );
 
-      if( w->state.outro_type == k_walk_outro_drop_in )
-      {
+      if( w->state.outro_type == k_walk_outro_drop_in ){
          float inv_rate      = 1.0f / w->state.outro_anim->rate,
                anim_frames   = w->state.outro_anim->length * inv_rate,
                step_frames   = 12.0f * inv_rate,
@@ -749,8 +777,7 @@ VG_STATIC void player__walk_animate( player_instance *player,
          
 
          /* the drop in bit */
-         if( step_t >= 1.0f )
-         {
+         if( step_t >= 1.0f ){
             v4f final_q;
             player_walk_drop_in_overhang_transform( player, dop_t, 
                                                     player->rb.co, final_q );
@@ -765,8 +792,7 @@ VG_STATIC void player__walk_animate( player_instance *player,
          }
          return;
       }
-      else
-      {
+      else{
          v3_muladds( dest->root_co, player->rb.to_world[1], 
                      -0.1f * outro_t, dest->root_co );
 
@@ -774,8 +800,7 @@ VG_STATIC void player__walk_animate( player_instance *player,
          player->holdout_time = 1.0f;
       }
    }
-   else
-   {
+   else{
       skeleton_copy_pose( sk, apose, dest->pose );
    }
 
@@ -822,6 +847,13 @@ VG_STATIC void player__walk_im_gui( player_instance *player )
                                              "k_walk_activity_immobile",
                                              "k_walk_activity_lockedmove" }
                                              [w->state.activity] );
+   player__debugtext( 1, "surface: %s\n",
+                           (const char *[]){ "concrete",
+                                             "wood",
+                                             "grass",
+                                             "tiles",
+                                             "metal" }
+                                             [w->surface] );
 
    if( w->state.outro_anim )
    {