diff options
author | ywang <foo@example.com> | 2019-11-27 10:20:37 +0100 |
---|---|---|
committer | orwell96 <orwell@bleipb.de> | 2019-11-27 10:23:05 +0100 |
commit | 35a6f14561add77ff96e943b748327041c1c5924 (patch) | |
tree | 0213c00e075f45d74ad3805dd1eb799ab8129589 | |
parent | 7082038f758e0616d7b54ed4e5f9a231103ac887 (diff) | |
download | advtrains-35a6f14561add77ff96e943b748327041c1c5924.tar.gz advtrains-35a6f14561add77ff96e943b748327041c1c5924.tar.bz2 advtrains-35a6f14561add77ff96e943b748327041c1c5924.zip |
Fix steam engine sound playing forever (H#121)
-rwxr-xr-x | advtrains_train_steam/init.lua | 23 |
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({ |