From 35a6f14561add77ff96e943b748327041c1c5924 Mon Sep 17 00:00:00 2001 From: ywang Date: Wed, 27 Nov 2019 10:20:37 +0100 Subject: Fix steam engine sound playing forever (H#121) --- advtrains_train_steam/init.lua | 23 ++++++++++++++++------- 1 file 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({ -- cgit v1.2.3