logic
[carveJwlIkooP6JGAAIwe30JlM.git] / player_skate.c
index 3b9c7642b3fa4664f013f45748fc8ff096484048..744b5eb050ca27c7fb5c040c21cffb2843b0aabf 100644 (file)
@@ -2,6 +2,7 @@
 #define PLAYER_SKATE_C
 
 #include "player.h"
+#include "audio.h"
 
 VG_STATIC void player__skate_bind( player_instance *player )
 {
@@ -851,7 +852,7 @@ VG_STATIC void skate_apply_jump_model( player_instance *player )
       v2f steer = { player->input_js1h->axis.value,
                     player->input_js1v->axis.value };
       v2_normalize_clamp( steer );
-
+      skate_apply_air_model( player );
 
 #if 0
       float maxspin = k_steer_air * k_rb_delta * k_spin_boost;
@@ -860,15 +861,9 @@ VG_STATIC void skate_apply_jump_model( player_instance *player )
       s->state.lift_frames ++;
 #endif
 
-      /* FIXME audio events */
-#if 0
       audio_lock();
-      audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
-      audio_player_set_position( &audio_player_extra, player.rb.co );
-      audio_player_set_vol( &audio_player_extra, 20.0f );
-      audio_player_playclip( &audio_player_extra, &audio_jumps[rand()%2] );
+      audio_oneshot_3d( &audio_jumps[rand()%2], player->rb.co, 40.0f, 1.0f );
       audio_unlock();
-#endif
    }
 }
 
@@ -1077,6 +1072,70 @@ VG_STATIC void player__skate_post_update( player_instance *player )
 #if 0
    vg_line_pt3( s->state.apex, 0.030f, 0xff0000ff );
 #endif
+
+   audio_lock();
+
+   float air   = s->state.activity == k_skate_activity_air? 1.0f: 0.0f,
+         speed = v3_length( player->rb.v ),
+         attn  = vg_minf( 1.0f, speed*0.1f ),
+         slide = vg_clampf( fabsf(s->state.slip), 0.0f, 1.0f ),
+
+         vol_main    = sqrtf( (1.0f-air)*attn*(1.0f-slide) * 0.4f ),
+         vol_air     = sqrtf(       air *attn * 0.5f ),
+         vol_slide   = sqrtf( (1.0f-air)*attn*slide * 0.25f );
+
+   const u32 flags = AUDIO_FLAG_SPACIAL_3D|AUDIO_FLAG_LOOP;
+   if( !s->aud_main )
+      s->aud_main = audio_request_channel( &audio_board[0], flags );
+
+   if( !s->aud_air )
+      s->aud_air = audio_request_channel( &audio_board[1], flags );
+
+   if( !s->aud_slide )
+      s->aud_slide = audio_request_channel( &audio_board[2], flags );
+
+
+   /* brrrrrrrrrrrt sound for tiles and stuff 
+    * --------------------------------------------------------*/
+   float sidechain_amt = 0.0f,
+         hz            = speed * 2.0f;
+
+   if( s->surface == k_surface_prop_tiles )
+      sidechain_amt = 1.0f;
+   else
+      sidechain_amt = 0.0f;
+
+   audio_set_lfo_frequency( 0, hz );
+   audio_set_lfo_wave( 0, k_lfo_polynomial_bipolar, 
+                        vg_lerpf( 250.0f, 80.0f, attn ) );
+
+   if( s->aud_main )
+   {
+      s->aud_main->colour = 0x00103efe;
+      audio_channel_set_spacial( s->aud_main, player->rb.co, 40.0f );
+      audio_channel_slope_volume( s->aud_main, 0.05f, vol_main );
+      audio_channel_sidechain_lfo( s->aud_main, 0, sidechain_amt );
+
+      float rate = 1.0f + (attn-0.5f)*0.2f;
+      audio_channel_set_sampling_rate( s->aud_main, rate );
+   }
+
+   if( s->aud_slide )
+   {
+      s->aud_slide->colour = 0x00103efe;
+      audio_channel_set_spacial( s->aud_slide, player->rb.co, 40.0f );
+      audio_channel_slope_volume( s->aud_slide, 0.05f, vol_slide );
+      audio_channel_sidechain_lfo( s->aud_slide, 0, sidechain_amt );
+   }
+
+   if( s->aud_air )
+   {
+      s->aud_air->colour = 0x00103efe;
+      audio_channel_set_spacial( s->aud_air, player->rb.co, 40.0f );
+      audio_channel_slope_volume( s->aud_air, 0.05f, vol_air );
+   }
+
+   audio_unlock();
 }
 
 /*
@@ -2214,6 +2273,17 @@ begin_collision:;
     * --------------------------------------------------------------------------
     */
 
+   s->surface = k_surface_prop_concrete;
+
+   for( int i=0; i<manifold_len; i++ )
+   {
+      rb_ct *ct = &manifold[i];
+      struct world_material *surface_mat = world_contact_material( world, ct );
+
+      if( surface_mat->info.surface_prop != k_surface_prop_concrete )
+         s->surface = surface_mat->info.surface_prop;
+   }
+
    for( int i=0; i<k_wheel_count; i++ )
    {
       m4x3f mtx;
@@ -2248,13 +2318,36 @@ begin_collision:;
       s->state_gate_storage = s->state;
       player__pass_gate( player, &hit );
    }
+
+   /* FIXME: Rate limit */
+   static int stick_frames = 0;
+
+   if( s->state.activity == k_skate_activity_ground )
+      stick_frames ++;
+   else
+      stick_frames = 0;
+
+
+   if( stick_frames == 4 )
+   {
+      audio_lock();
+      if( (fabsf(s->state.slip) > 0.75f) )
+      {
+         audio_oneshot_3d( &audio_lands[rand()%2+3], player->rb.co, 
+                           40.0f, 1.0f );
+      }
+      else
+      {
+         audio_oneshot_3d( &audio_lands[rand()%3], player->rb.co, 
+                           40.0f, 1.0f );
+      }
+      audio_unlock();
+   }
 }
 
 VG_STATIC void player__skate_im_gui( player_instance *player )
 {
    struct player_skate *s = &player->_skate;
-
-   /* FIXME: Compression */
    player__debugtext( 1, "V:  %5.2f %5.2f %5.2f",player->rb.v[0],
                                                 player->rb.v[1],
                                                 player->rb.v[2] );
@@ -2583,24 +2676,18 @@ VG_STATIC void player__skate_animate( player_instance *player,
    v3_muladds( dest->root_co, player->rb.to_world[1], -0.1f, dest->root_co );
 
    float substep = vg_clampf( vg.accumulator / VG_TIMESTEP_FIXED, 0.0f, 1.0f );
-#if 0
-   v4f qresy, qresx, qresidual;
-   m3x3f mtx_residual;
-   q_axis_angle( qresy, player->rb.to_world[1], s->state.steery_s*substep );
-   q_axis_angle( qresx, player->rb.to_world[0], s->state.steerx_s*substep );
-
-   q_mul( qresy, qresx, qresidual );
-   q_normalize( qresidual );
-   q_mul( dest->root_q, qresidual, dest->root_q );
-   q_normalize( dest->root_q );
-#endif
 
    v4f qflip;
    if( (s->state.activity == k_skate_activity_air) &&
        (fabsf(s->state.flip_rate) > 0.01f) )
    {
-      float t = s->state.flip_time + s->state.flip_rate*substep*k_rb_delta,
-            angle = vg_clampf( t, -1.0f, 1.0f ) * VG_TAUf,
+      float t     = s->state.flip_time;
+            sign  = vg_signf( t );
+
+      t  = 1.0f - vg_minf( 1.0f, fabsf( t * 1.1f ) );
+      t  = sign * (1.0f-t*t);
+
+      float angle = vg_clampf( t, -1.0f, 1.0f ) * VG_TAUf,
             distm = s->land_dist * fabsf(s->state.flip_rate) * 3.0f,
             blend = vg_clampf( 1.0f-distm, 0.0f, 1.0f );
 
@@ -2628,7 +2715,7 @@ VG_STATIC void player__skate_post_animate( player_instance *player )
 
    player->cam_velocity_influence = 1.0f;
 
-   v3f head = { 0.0f, 1.8f, 0.0f }; /* FIXME: Viewpoint entity */
+   v3f head = { 0.0f, 1.8f, 0.0f };
    m4x3_mulv( av->sk.final_mtx[ av->id_head ], head, s->state.head_position );
    m4x3_mulv( player->rb.to_local, s->state.head_position, 
                                    s->state.head_position );