Sounds (through driver object).

This commit is contained in:
Auke Kok 2016-05-01 16:29:34 -07:00
parent 3a87cafee0
commit e3928fa56d
11 changed files with 66 additions and 30 deletions

View File

@ -3,6 +3,7 @@
-- Driver class - manage driver execution -- Driver class - manage driver execution
-- --
-- Class definition -- Class definition
Driver = {} Driver = {}
Driver.__index = Driver Driver.__index = Driver
@ -13,6 +14,8 @@ setmetatable(Driver, {
end, end,
}) })
-- private functions
local function driver_setup(self, driver) local function driver_setup(self, driver)
self.name = driver self.name = driver
self.driver = drivers[driver] self.driver = drivers[driver]
@ -25,7 +28,20 @@ local function driver_setup(self, driver)
end end
end end
-- constructor local function driver_start(self)
local script = self.object.script
local sounds = script[self.name].sounds
if sounds and sounds.start then
local spec = script.sounds[sounds.start][1]
local params = {max_hear_distance = script.sounds[sounds.start][2].max_hear_distance,
object = self.object.object}
minetest.sound_play(spec, params)
end
self.driver.start(self.object)
end
--- constructor
function Driver.new(object, driver) function Driver.new(object, driver)
local self = setmetatable({}, Driver) local self = setmetatable({}, Driver)
self.object = object self.object = object
@ -33,16 +49,26 @@ function Driver.new(object, driver)
return self return self
end end
-- public functions
function Driver:switch(driver) function Driver:switch(driver)
driver_setup(self, driver) driver_setup(self, driver)
self.driver.start(self.object) driver_start(self)
end end
function Driver:start() function Driver:start()
self.driver.start(self.object) driver_start(self)
end end
function Driver:step(dtime) function Driver:step(dtime)
local script = self.object.script
local sounds = script[self.name].sounds
if math.random(1, 200) == 1 and sounds and sounds.random then
local spec = script.sounds[sounds.random][1]
local params = {max_hear_distance = script.sounds[sounds.random][2].max_hear_distance,
object = self.object.object}
minetest.sound_play(spec, params)
end
self.driver.step(self.object, dtime) self.driver.step(self.object, dtime)
end end

View File

@ -209,7 +209,7 @@ drivers.roam = {
return return
end end
minetest.add_particle({ --[[ minetest.add_particle({
pos = {x = pick.x, y = pick.y - 0.1, z = pick.z}, pos = {x = pick.x, y = pick.y - 0.1, z = pick.z},
velocity = vector.new(), velocity = vector.new(),
acceleration = vector.new(), acceleration = vector.new(),
@ -220,6 +220,7 @@ drivers.roam = {
texture = "wool_red.png", texture = "wool_red.png",
playername = nil playername = nil
}) })
--]]
self.path = Path(self, pick) self.path = Path(self, pick)
if not self.path:find() then if not self.path:find() then
@ -232,8 +233,6 @@ drivers.roam = {
animation_select(self, "move") animation_select(self, "move")
end, end,
step = function(self, dtime) step = function(self, dtime)
animation_loop(self, dtime)
consider_factors(self, dtime)
-- handle movement stuff -- handle movement stuff
local state = self.entity_ai_state local state = self.entity_ai_state
if state.roam_ttl > 0 then if state.roam_ttl > 0 then
@ -262,8 +261,6 @@ drivers.idle = {
state.idle_ttl = math.random(2, 20) state.idle_ttl = math.random(2, 20)
end, end,
step = function(self, dtime) step = function(self, dtime)
animation_loop(self, dtime)
consider_factors(self, dtime)
local state = self.entity_ai_state local state = self.entity_ai_state
state.idle_ttl = state.idle_ttl - dtime state.idle_ttl = state.idle_ttl - dtime
if state.idle_ttl <= 0 then if state.idle_ttl <= 0 then
@ -290,8 +287,6 @@ drivers.startle = {
state.factors.anim_end = nil state.factors.anim_end = nil
end, end,
step = function(self, dtime) step = function(self, dtime)
animation_loop(self, dtime)
consider_factors(self, dtime)
end, end,
stop = function(self) stop = function(self)
-- play out remaining animations -- play out remaining animations
@ -309,8 +304,6 @@ drivers.eat = {
state.eat_ttl = math.random(30, 60) state.eat_ttl = math.random(30, 60)
end, end,
step = function(self, dtime) step = function(self, dtime)
animation_loop(self, dtime)
consider_factors(self, dtime)
local state = self.entity_ai_state local state = self.entity_ai_state
state.eat_ttl = (state.eat_ttl or math.random(30, 30)) - dtime state.eat_ttl = (state.eat_ttl or math.random(30, 30)) - dtime
@ -344,14 +337,12 @@ drivers.flee = {
state.factors.fleed_too_long = nil state.factors.fleed_too_long = nil
end, end,
step = function(self, dtime) step = function(self, dtime)
animation_loop(self, dtime)
-- check timer ourselves -- check timer ourselves
local state = self.entity_ai_state local state = self.entity_ai_state
if (minetest.get_us_time() - state.flee_start) > (15 * 1000000) then if (minetest.get_us_time() - state.flee_start) > (15 * 1000000) then
state.factors.got_hit = nil state.factors.got_hit = nil
state.factors.fleed_too_long = true state.factors.fleed_too_long = true
end end
consider_factors(self, dtime)
-- are we fleeing yet? -- are we fleeing yet?
if self.path and self.path.distance then if self.path and self.path.distance then
@ -417,7 +408,7 @@ drivers.flee = {
print("no path found!") print("no path found!")
return return
end end
--[[
minetest.add_particle({ minetest.add_particle({
pos = {x = pick.x, y = pick.y - 0.1, z = pick.z}, pos = {x = pick.x, y = pick.y - 0.1, z = pick.z},
velocity = vector.new(), velocity = vector.new(),
@ -429,6 +420,7 @@ drivers.flee = {
texture = "wool_red.png", texture = "wool_red.png",
playername = nil playername = nil
}) })
--]]
self.path = Path(self, pick) self.path = Path(self, pick)
if not self.path:find() then if not self.path:find() then
@ -451,7 +443,6 @@ drivers.death = {
animation_select(self, "idle") animation_select(self, "idle")
end, end,
step = function(self, dtime) step = function(self, dtime)
animation_loop(self, dtime)
end, end,
stop = function(self) stop = function(self)
-- play out remaining animations -- play out remaining animations
@ -516,6 +507,15 @@ local function entity_ai_on_activate(self, staticdata)
local state = self.entity_ai_state local state = self.entity_ai_state
-- driver class, has to come before path
if state.driver_save then
driver = state.driver_save
state.driver_save = nil
else
driver = self.script.driver
end
self.driver = Driver(self, driver)
-- path class -- path class
if state.path_save then if state.path_save then
self.path = Path(self, state.path_save.target) self.path = Path(self, state.path_save.target)
@ -524,17 +524,11 @@ local function entity_ai_on_activate(self, staticdata)
state.path_save = {} state.path_save = {}
end end
-- driver class
if state.driver_save then
driver = state.driver_save
state.driver_save = nil
else
driver = self.script.driver
end
print("loaded: " .. self.name .. ", driver=" .. driver ) print("loaded: " .. self.name .. ", driver=" .. driver )
else else
-- set initial mob driver -- set initial mob driver
driver = self.script.driver driver = self.script.driver
self.driver = Driver(self, driver)
print("activate: " .. self.name .. ", driver=" .. driver) print("activate: " .. self.name .. ", driver=" .. driver)
end end
@ -542,11 +536,12 @@ local function entity_ai_on_activate(self, staticdata)
self.object:setacceleration({x = 0, y = -9.81, z = 0}) self.object:setacceleration({x = 0, y = -9.81, z = 0})
-- init driver -- init driver
self.driver = Driver(self, driver)
self.driver:start() self.driver:start()
end end
local function entity_ai_on_step(self, dtime) local function entity_ai_on_step(self, dtime)
animation_loop(self, dtime)
consider_factors(self, dtime)
self.driver:step(dtime) self.driver:step(dtime)
end end
@ -624,9 +619,9 @@ local sheep_script = {
}, },
-- sound samples -- sound samples
sounds = { sounds = {
chatter = {name = "sheep_chatter", gain = 1.0}, chatter = {{name = "sheep_chatter", gain = 0.2}, {max_hear_distance = 12}},
footsteps = {name = "sheep_steps", gain = 1.0}, footsteps = {{name = "sheep_steps", gain = 0.2}, {max_hear_distance = 12}},
hurt = {name = "sheep_hurt", gain = 1.0}, hurt = {{name = "sheep_hurt", gain = 0.5}, {max_hear_distance = 18}},
}, },
-- mob script states: -- mob script states:
roam = { roam = {
@ -636,7 +631,7 @@ local sheep_script = {
attractor_nearby = "attracted", attractor_nearby = "attracted",
}, },
sounds = { sounds = {
random = "chatter", random = "footsteps",
}, },
}, },
idle = { idle = {
@ -679,7 +674,7 @@ local sheep_script = {
fleed_too_long = "roam", fleed_too_long = "roam",
}, },
sounds = { sounds = {
random = "hurt", random = "footsteps",
}, },
}, },
attracted = { attracted = {

Binary file not shown.

View File

@ -65,7 +65,7 @@ function Path:find()
local config = self.config local config = self.config
self.path = minetest.find_path(pos, vector.round(self.target), config.distance, config.jump, self.path = minetest.find_path(pos, vector.round(self.target), config.distance, config.jump,
config.fall, config.algorithm) config.fall, config.algorithm)
--[[
if self.path ~= nil then if self.path ~= nil then
for k, v in pairs(self.path) do for k, v in pairs(self.path) do
minetest.add_particle({ minetest.add_particle({
@ -81,6 +81,7 @@ function Path:find()
}) })
end end
end end
--]]
return self.path ~= nil return self.path ~= nil
end end
@ -111,6 +112,7 @@ function Path:step(dtime)
self.path[ii] = nil self.path[ii] = nil
end end
-- done pruning -- done pruning
--[[
minetest.add_particle({ minetest.add_particle({
pos = {x = v.x, y = v.y + 0.2, z = v.z}, pos = {x = v.x, y = v.y + 0.2, z = v.z},
velocity = vector.new(), velocity = vector.new(),
@ -122,6 +124,7 @@ function Path:step(dtime)
texture = "wool_yellow.png", texture = "wool_yellow.png",
playername = nil playername = nil
}) })
--]]
local vo = {x = v.x, y = v.y - 0.5, z = v.z} local vo = {x = v.x, y = v.y - 0.5, z = v.z}
local vec = vector.subtract(vo, pos) local vec = vector.subtract(vo, pos)
local len = vector.length(vec) local len = vector.length(vec)

12
sounds/README.txt Normal file
View File

@ -0,0 +1,12 @@
sheep_chatter.1.ogg:
- http://www.freesound.org/people/n_audioman/sounds/321967/ n_audioman CC-BY-3.0
sheep_chatter.2.ogg:
- http://www.freesound.org/people/confusion_music/sounds/103442/ ion_music CC-BY-3.0
sheep_chatter.3.ogg:
sheep_chatter.4.ogg:
- http://www.freesound.org/people/reinsamba/sounds/57796/ reinsamba CC-BY-3.0
sheep_hurt.1.ogg:
- http://www.freesound.org/people/confusion_music/sounds/103441/ confusion_music CC-BY-3.0
sheep_steps.1.ogg:
- http://www.freesound.org/people/newagesoup/sounds/342475/ newagesoup CC-BY-3.0

BIN
sounds/sheep_chatter.1.ogg Normal file

Binary file not shown.

BIN
sounds/sheep_chatter.2.ogg Normal file

Binary file not shown.

BIN
sounds/sheep_chatter.3.ogg Normal file

Binary file not shown.

BIN
sounds/sheep_chatter.4.ogg Normal file

Binary file not shown.

BIN
sounds/sheep_hurt.1.ogg Normal file

Binary file not shown.

BIN
sounds/sheep_steps.1.ogg Normal file

Binary file not shown.