summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorywang <foo@example.com>2019-11-27 10:20:37 +0100
committerorwell96 <orwell@bleipb.de>2019-11-27 10:23:05 +0100
commitc15985883c060b1e3ede53e50e1c4538f98923cb (patch)
treee9d6aab4ba1cd72a2dc5615c39599a1ef91b45d4
parent5497aaaa8b2f9df382f3071d6574b001353c44df (diff)
downloadbasic_trains-c15985883c060b1e3ede53e50e1c4538f98923cb.tar.gz
basic_trains-c15985883c060b1e3ede53e50e1c4538f98923cb.tar.bz2
basic_trains-c15985883c060b1e3ede53e50e1c4538f98923cb.zip
Fix steam engine sound playing forever (H#121)
-rwxr-xr-xadvtrains_train_steam/init.lua23
1 files changed, 16 insertions, 7 deletions
diff --git a/advtrains_train_steam/init.lua b/advtrains_train_steam/init.lua
index db2d087..5d6955c 100755
--- a/advtrains_train_steam/init.lua
+++ b/advtrains_train_steam/init.lua
@@ -5,6 +5,9 @@ else
S = function(s,a,...)a={a,...}return s:gsub("@(%d+)",function(n)return a[tonumber(n)]end)end
end
+-- length of the steam engine loop sound
+local SND_LOOP_LEN = 5
+
advtrains.register_wagon("newlocomotive", {
mesh="advtrains_engine_steam.b3d",
textures = {"advtrains_engine_steam.png"},
@@ -111,14 +114,20 @@ advtrains.register_wagon("detailed_steam_engine", {
self.object:set_animation({x=1,y=80}, advtrains.abs_ceil(velocity)*15, 0, true)
self.old_anim_velocity=advtrains.abs_ceil(velocity)
end
- if velocity > 0 and not self.sound_loop_handle then
- self.sound_loop_handle = minetest.sound_play({name="advtrains_steam_loop", gain=2}, {object = self.object, loop=true})
- elseif velocity==0 then
- if self.sound_loop_handle then
- minetest.sound_stop(self.sound_loop_handle)
- self.sound_loop_handle = nil
+ end,
+ custom_on_step=function(self, dtime)
+ if self:train().velocity > 0 then -- First make sure that the train isn't standing
+ if not self.sound_loop_tmr or self.sound_loop_tmr <= 0 then
+ -- start the sound if it was never started or has expired
+ self.sound_loop_handle = minetest.sound_play({name="advtrains_steam_loop", gain=2}, {object=self.object})
+ self.sound_loop_tmr = SND_LOOP_LEN
end
- end
+ --decrease the sound timer
+ self.sound_loop_tmr = self.sound_loop_tmr - dtime
+ else
+ -- If the train is standing, the sound will be stopped in some time. We do not need to interfere with it.
+ self.sound_loop_tmr = nil
+ end
end,
custom_on_activate = function(self, staticdata_table, dtime_s)
minetest.add_particlespawner({