mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Prevent unnecessary running of code
This commit is contained in:
parent
aafd13508d
commit
c4239e074f
@ -48,6 +48,12 @@ impl AmbientMgr {
|
||||
let sfx_volume = audio.get_sfx_volume();
|
||||
// iterate through each tag
|
||||
for tag in AmbientChannelTag::iter() {
|
||||
// check if current conditions necessitate the current tag at all
|
||||
let should_create: bool = match tag {
|
||||
AmbientChannelTag::Wind => self.check_wind_necessity(client, camera),
|
||||
AmbientChannelTag::Rain => self.check_rain_necessity(client),
|
||||
};
|
||||
if should_create {
|
||||
// iterate through the supposed number of channels - one for each tag
|
||||
for index in 0..AmbientChannelTag::iter().len() {
|
||||
// if index would exceed current number of channels, create a new one with
|
||||
@ -75,7 +81,8 @@ impl AmbientMgr {
|
||||
|
||||
// set the duration of the loop to whatever the current value is (0.0 by
|
||||
// default)
|
||||
let next_track_change = audio.ambient_channels[index].get_next_track_change();
|
||||
let next_track_change =
|
||||
audio.ambient_channels[index].get_next_track_change();
|
||||
|
||||
// if the sound should loop at this point:
|
||||
if audio.ambient_channels[index]
|
||||
@ -92,7 +99,8 @@ impl AmbientMgr {
|
||||
// set loop duration to the one specified in the ron
|
||||
audio.ambient_channels[index].set_next_track_change(track.length);
|
||||
// play the file of the current tag at the current multiplier
|
||||
let current_multiplier = audio.ambient_channels[index].get_multiplier();
|
||||
let current_multiplier =
|
||||
audio.ambient_channels[index].get_multiplier();
|
||||
audio.play_ambient(tag, &track.path, current_multiplier);
|
||||
}
|
||||
};
|
||||
@ -109,8 +117,38 @@ impl AmbientMgr {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// no need to run code at all, move on to the next tag
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_wind_necessity(&mut self, client: &Client, camera: &Camera) -> bool {
|
||||
let focus_off = camera.get_focus_pos().map(f32::trunc);
|
||||
let cam_pos = camera.dependents().cam_pos + focus_off;
|
||||
|
||||
let (terrain_alt, tree_density) = if let Some(chunk) = client.current_chunk() {
|
||||
(chunk.meta().alt(), chunk.meta().tree_density())
|
||||
} else {
|
||||
(0.0, 0.0)
|
||||
};
|
||||
|
||||
// Wind volume increases with altitude
|
||||
let alt_multiplier = (cam_pos.z / 1200.0).abs();
|
||||
|
||||
// Tree density factors into wind volume. The more trees,
|
||||
// the lower wind volume. The trees make more of an impact
|
||||
// the closer the camera is to the ground.
|
||||
let tree_multiplier =
|
||||
((1.0 - tree_density) + ((cam_pos.z - terrain_alt).abs() / 150.0).powi(2)).min(1.0);
|
||||
|
||||
return alt_multiplier * tree_multiplier > 0.0;
|
||||
}
|
||||
|
||||
fn check_rain_necessity(&mut self, client: &Client) -> bool {
|
||||
client.current_weather().rain * 5.0 > 0.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AmbientChannel {
|
||||
|
@ -1070,6 +1070,7 @@ impl Scene {
|
||||
self.figure_mgr.clean(scene_data.tick);
|
||||
|
||||
// Maintain audio
|
||||
if audio.sfx_enabled() {
|
||||
self.sfx_mgr.maintain(
|
||||
audio,
|
||||
scene_data.state,
|
||||
@ -1078,9 +1079,14 @@ impl Scene {
|
||||
&self.terrain,
|
||||
client,
|
||||
);
|
||||
self.music_mgr.maintain(audio, scene_data.state, client);
|
||||
self.ambient_mgr
|
||||
.maintain(audio, scene_data.state, client, &self.camera);
|
||||
}
|
||||
|
||||
if audio.music_enabled() {
|
||||
self.music_mgr.maintain(audio, scene_data.state, client);
|
||||
}
|
||||
|
||||
// self.ambient_wind_mgr
|
||||
// .maintain(audio, scene_data.state, client, &self.camera);
|
||||
// self.ambient_rain_mgr
|
||||
|
Loading…
Reference in New Issue
Block a user