| 30 | } |
| 31 | |
| 32 | void ObjectsSoundsProcessor::Update( const MapState& map_state, const m_Vec3& head_pos ) |
| 33 | { |
| 34 | const float c_change_time_s= 0.5f; |
| 35 | |
| 36 | const Time current_tick_time= Time::CurrentTime(); |
| 37 | const float tick_delta_s= ( current_tick_time - prev_tick_time_ ).ToSeconds(); |
| 38 | prev_tick_time_= current_tick_time; |
| 39 | |
| 40 | const MapState::StaticModel* nearest_model= nullptr; |
| 41 | float nearest_model_square_distance= Constants::max_float; |
| 42 | m_Vec3 nearest_model_pos; |
| 43 | |
| 44 | // Search nearest sound. |
| 45 | for( const MapState::StaticModel& model : map_state.GetStaticModels() ) |
| 46 | { |
| 47 | if( model.model_id >= map_data_->models_description.size() ) |
| 48 | continue; |
| 49 | |
| 50 | const MapData::ModelDescription& description= map_data_->models_description[ model.model_id ]; |
| 51 | if( description.ambient_sfx_number == 0u ) |
| 52 | continue; |
| 53 | |
| 54 | const Model& model_geometry= map_data_->models[ model.model_id ]; |
| 55 | |
| 56 | const float z= model.pos.z + ( model_geometry.z_min + model_geometry.z_max ) * 0.5f; |
| 57 | const m_Vec3 sound_pos( model.pos.xy(), z ); |
| 58 | |
| 59 | const float square_distance= ( head_pos - sound_pos ).SquareLength(); |
| 60 | if( square_distance < nearest_model_square_distance ) |
| 61 | { |
| 62 | nearest_model= &model; |
| 63 | nearest_model_square_distance= square_distance; |
| 64 | nearest_model_pos= sound_pos; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | unsigned int sound_number= 0u; |
| 69 | |
| 70 | if( nearest_model != nullptr ) |
| 71 | { |
| 72 | sound_number= map_data_->models_description[ nearest_model->model_id ].ambient_sfx_number; |
| 73 | |
| 74 | if( sound_number == current_sound_ ) |
| 75 | nearest_source_pos_= nearest_model_pos; |
| 76 | } |
| 77 | |
| 78 | const float abs_volume_change= tick_delta_s / c_change_time_s; |
| 79 | if( sound_number != current_sound_ ) |
| 80 | { |
| 81 | volume_-= abs_volume_change; |
| 82 | if( volume_ <= 0.0f ) |
| 83 | { |
| 84 | current_sound_= sound_number; |
| 85 | volume_= 0.0f; |
| 86 | } |
| 87 | } |
| 88 | else |
| 89 | { |
no test coverage detected