oneshot synchronization
[carveJwlIkooP6JGAAIwe30JlM.git] / player_remote.c
index 3e78d506488a3f7603c71d5b2d7b3a7c2851cc55..9a753944e899c19189f768d4d69ac7da85cf76ed 100644 (file)
@@ -166,10 +166,33 @@ static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
        * -------------------------------------------------------------*/
       
       for( u32 i=0; i<frame->sound_effects; i ++ ){
-         /* CHEATING for now */
          struct net_sfx sfx;
          net_sfx_exchange( &ctx, &sfx );
-         net_sfx_play( &sfx );
+
+         f64 t = (frame->timestamp - NETWORK_FRAMERATE) + 
+                 (sfx.subframe*NETWORK_FRAMERATE);
+
+         f32 remaining = t - ib->t;
+
+         if( remaining <= 0.0f )
+            net_sfx_play( &sfx );
+         else{
+            struct net_sfx *dst = NULL;
+
+            for( u32 j=0; j<NETWORK_SFX_QUEUE_LENGTH; j ++ ){
+               struct net_sfx *sj = &netplayers.sfx_queue[j];
+               if( sj->system == k_player_subsystem_invalid ){
+                  dst = sj;
+                  break;
+               }
+
+               if( sj->priority < sfx.priority )
+                  dst = sj;
+            }
+
+            *dst = sfx;
+            dst->subframe = remaining;
+         }
       }
       
       /* animation 
@@ -568,6 +591,22 @@ static void render_remote_players( world_instance *world, camera *cam ){
    SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
 }
 
-/* TODO: Which world is the player in
- *       nametags with occlusion
- */
+static void remote_players_init(void){
+   for( u32 i=0; i<NETWORK_SFX_QUEUE_LENGTH; i ++ ){
+      netplayers.sfx_queue[i].system = k_player_subsystem_invalid;
+   }
+}
+
+static void remote_sfx_pre_update(void){
+   for( u32 i=0; i<NETWORK_SFX_QUEUE_LENGTH; i ++ ){
+      struct net_sfx *si = &netplayers.sfx_queue[i];
+
+      if( si->system != k_player_subsystem_invalid ){
+         si->subframe -= vg.time_frame_delta;
+         if( si->subframe <= 0.0f ){
+            net_sfx_play( si );
+            si->system = k_player_subsystem_invalid;
+         }
+      }
+   }
+}