summaryrefslogtreecommitdiff
path: root/drive_entity.lua
diff options
context:
space:
mode:
authorcheapie <no-email-for-you@example.com>2024-04-11 19:53:20 -0500
committercheapie <no-email-for-you@example.com>2024-04-11 19:53:20 -0500
commit5fa200dc576a8389f1ba237a301f339764b7a7a9 (patch)
tree46dfc0d21fc95bee0047acf4f176fb66b2cab1d0 /drive_entity.lua
parent12c3ce32dc9d6111b54a981cf39cb22f57c3800e (diff)
downloadcelevator-5fa200dc576a8389f1ba237a301f339764b7a7a9.tar
celevator-5fa200dc576a8389f1ba237a301f339764b7a7a9.tar.gz
celevator-5fa200dc576a8389f1ba237a301f339764b7a7a9.tar.bz2
celevator-5fa200dc576a8389f1ba237a301f339764b7a7a9.tar.xz
celevator-5fa200dc576a8389f1ba237a301f339764b7a7a9.zip
Add motor sounds
Diffstat (limited to 'drive_entity.lua')
-rw-r--r--drive_entity.lua130
1 files changed, 112 insertions, 18 deletions
diff --git a/drive_entity.lua b/drive_entity.lua
index 8c9f27c..4b6dff3 100644
--- a/drive_entity.lua
+++ b/drive_entity.lua
@@ -2,7 +2,9 @@ celevator.drives.entity = {
name = "Drive",
description = "Normal entity-based drive",
nname = "celevator:drive",
- soundhandles = {},
+ buzzsoundhandles = {},
+ movementsoundhandles = {},
+ movementsoundstate = {},
entityinfo = {},
}
@@ -26,8 +28,8 @@ end
local function playbuzz(pos)
local hash = minetest.hash_node_position(pos)
- if celevator.drives.null.soundhandles[hash] == "cancel" then return end
- celevator.drives.null.soundhandles[hash] = minetest.sound_play("celevator_drive_run",{
+ if celevator.drives.entity.buzzsoundhandles[hash] == "cancel" then return end
+ celevator.drives.entity.buzzsoundhandles[hash] = minetest.sound_play("celevator_drive_run",{
pos = pos,
loop = true,
gain = 0.4,
@@ -36,27 +38,103 @@ end
local function startbuzz(pos)
local hash = minetest.hash_node_position(pos)
- if celevator.drives.null.soundhandles[hash] == "cancel" then
- celevator.drives.null.soundhandles[hash] = nil
+ if celevator.drives.entity.buzzsoundhandles[hash] == "cancel" then
+ celevator.drives.entity.buzzsoundhandles[hash] = nil
return
end
- if celevator.drives.null.soundhandles[hash] then return end
- celevator.drives.null.soundhandles[hash] = "pending"
+ if celevator.drives.entity.buzzsoundhandles[hash] then return end
+ celevator.drives.entity.buzzsoundhandles[hash] = "pending"
minetest.after(0.5,playbuzz,pos)
end
local function stopbuzz(pos)
local hash = minetest.hash_node_position(pos)
- if not celevator.drives.null.soundhandles[hash] then return end
- if celevator.drives.null.soundhandles[hash] == "pending" then
- celevator.drives.null.soundhandles[hash] = "cancel"
+ if not celevator.drives.entity.buzzsoundhandles[hash] then return end
+ if celevator.drives.entity.buzzsoundhandles[hash] == "pending" then
+ celevator.drives.entity.buzzsoundhandles[hash] = "cancel"
end
- if type(celevator.drives.null.soundhandles[hash]) ~= "string" then
- minetest.sound_stop(celevator.drives.null.soundhandles[hash])
- celevator.drives.null.soundhandles[hash] = nil
+ if type(celevator.drives.entity.buzzsoundhandles[hash]) ~= "string" then
+ minetest.sound_stop(celevator.drives.entity.buzzsoundhandles[hash])
+ celevator.drives.entity.buzzsoundhandles[hash] = nil
end
end
+local function motorsound(pos,newstate)
+ local hash = minetest.hash_node_position(pos)
+ local oldstate = celevator.drives.entity.movementsoundstate[hash]
+ oldstate = oldstate or "idle"
+ if oldstate == newstate then return end
+ local carid = minetest.get_meta(pos):get_int("carid")
+ local carinfo = minetest.deserialize(celevator.storage:get_string(string.format("car%d",carid)))
+ if not (carinfo and carinfo.machinepos) then return end
+ local oldhandle = celevator.drives.entity.movementsoundhandles[hash]
+ if newstate == "slow" then
+ if oldstate == "idle" then
+ minetest.sound_play("celevator_brake_release",{
+ pos = carinfo.machinepos,
+ gain = 1,
+ },true)
+ celevator.drives.entity.movementsoundhandles[hash] = minetest.sound_play("celevator_motor_slow",{
+ pos = carinfo.machinepos,
+ loop = true,
+ gain = 1,
+ })
+ elseif oldstate == "accel" or oldstate == "fast" or oldstate == "decel" then
+ if oldhandle then minetest.sound_stop(oldhandle) end
+ celevator.drives.entity.movementsoundhandles[hash] = minetest.sound_play("celevator_motor_slow",{
+ pos = carinfo.machinepos,
+ loop = true,
+ gain = 1,
+ })
+ end
+ elseif newstate == "accel" then
+ if oldhandle then minetest.sound_stop(oldhandle) end
+ celevator.drives.entity.movementsoundhandles[hash] = minetest.sound_play("celevator_motor_accel",{
+ pos = carinfo.machinepos,
+ gain = 1,
+ })
+ elseif newstate == "fast" then
+ if oldhandle then minetest.sound_stop(oldhandle) end
+ celevator.drives.entity.movementsoundhandles[hash] = minetest.sound_play("celevator_motor_fast",{
+ pos = carinfo.machinepos,
+ loop = true,
+ gain = 1,
+ })
+ elseif newstate == "decel" then
+ if oldhandle then minetest.sound_stop(oldhandle) end
+ celevator.drives.entity.movementsoundhandles[hash] = minetest.sound_play("celevator_motor_decel",{
+ pos = carinfo.machinepos,
+ gain = 1,
+ })
+ elseif newstate == "idle" then
+ if oldhandle then minetest.sound_stop(oldhandle) end
+ minetest.sound_play("celevator_brake_apply",{
+ pos = carinfo.machinepos,
+ gain = 1,
+ },true)
+ end
+ celevator.drives.entity.movementsoundstate[hash] = newstate
+end
+
+local function compareexchangesound(pos,compare,new)
+ local hash = minetest.hash_node_position(pos)
+ local oldstate = celevator.drives.entity.movementsoundstate[hash]
+ if oldstate == compare then
+ motorsound(pos,new)
+ end
+end
+
+local function accelsound(pos)
+ motorsound(pos,"slow")
+ minetest.after(1,compareexchangesound,pos,"slow","accel")
+ minetest.after(4,compareexchangesound,pos,"accel","fast")
+end
+
+local function decelsound(pos)
+ motorsound(pos,"decel")
+ minetest.after(2,compareexchangesound,pos,"decel","slow")
+end
+
minetest.register_node("celevator:drive",{
description = celevator.drives.entity.name,
groups = {
@@ -215,7 +293,14 @@ function celevator.drives.entity.step(dtime)
return
end
if state == "start" then
- sound = true
+ if math.abs(dpos-startpos) > 0.1 then
+ sound = true
+ if maxvel > 0.2 then
+ accelsound(pos)
+ else
+ motorsound(pos,"slow")
+ end
+ end
local startv = vector.add(origin,vector.new(0,startpos,0))
local hashes = celevator.drives.entity.gathercar(startv,minetest.dir_to_yaw(minetest.fourdir_to_dir(minetest.get_node(startv).param2)))
local nodes = {}
@@ -251,6 +336,7 @@ function celevator.drives.entity.step(dtime)
if dremain < 0.05 then
vel = 0
meta:set_string("state","stopped")
+ motorsound(pos,"idle")
local ok = celevator.drives.entity.entitiestonodes(handles,carid)
if not ok then
local carparam2 = meta:get_int("carparam2")
@@ -259,8 +345,11 @@ function celevator.drives.entity.step(dtime)
apos = math.floor(apos+0.5)
elseif dremain < 0.2 then
vel = 0.2
- elseif dremain < maxvel and dremain < dmoved then
- vel = dremain
+ elseif dremain < 2*maxvel and dremain < dmoved then
+ vel = math.min(dremain,maxvel)
+ if celevator.drives.entity.movementsoundstate[hash] == "fast" or celevator.drives.entity.movementsoundstate[hash] == "accel" then
+ decelsound(pos)
+ end
elseif dmoved+0.1 > maxvel then
vel = maxvel
else
@@ -281,13 +370,17 @@ function celevator.drives.entity.step(dtime)
if dremain < 0.05 then
vel = 0
meta:set_string("state","stopped")
+ motorsound(pos,"idle")
local carparam2 = meta:get_int("carparam2")
celevator.car.spawncar(vector.round(vector.add(origin,vector.new(0,apos,0))),minetest.dir_to_yaw(minetest.fourdir_to_dir(carparam2)),carid)
apos = math.floor(apos+0.5)
elseif dremain < 0.2 then
vel = 0.2
- elseif dremain < maxvel and dremain < dmoved then
- vel = dremain
+ elseif dremain < 2*maxvel and dremain < dmoved then
+ vel = math.min(dremain,maxvel)
+ if celevator.drives.entity.movementsoundstate[hash] == "fast" or celevator.drives.entity.movementsoundstate[hash] == "accel" then
+ decelsound(pos)
+ end
elseif dmoved+0.1 > maxvel then
vel = maxvel
else
@@ -354,6 +447,7 @@ function celevator.drives.entity.estop(pos)
meta:set_string("vel","0")
celevator.drives.entity.entitiestonodes(handles)
stopbuzz(pos)
+ motorsound(pos,"idle")
end