diff --git a/src/assets/sound/gebet.ogg b/src/assets/sound/gebet.ogg new file mode 100644 index 0000000..d840e42 Binary files /dev/null and b/src/assets/sound/gebet.ogg differ diff --git a/src/engine/init.lua b/src/engine/init.lua index 1ca38df..52c0c7b 100644 --- a/src/engine/init.lua +++ b/src/engine/init.lua @@ -4,9 +4,10 @@ local HC = require 'engine/libs/hardoncollider' -- has to come before the sti initialization so I can add the collision tiles collider = HC(100) -local sti = require "engine/libs/sti" -local object = require "engine/object" -local ui = require "engine/ui" +local sti = require 'engine/libs/sti' +local object = require 'engine/object' +local ui = require 'engine/ui' +local sound = require 'engine/sound' local story = require 'story' Engine = class() @@ -20,6 +21,10 @@ function Engine:__init() story:start(self) end +function Engine:playSound(soundName, loop, finishedFunc) + return love.audio.play('assets/sound/' .. soundName .. '.ogg', 'stream', loop, finishedFunc) +end + function Engine:checkObjectAnimation(key) for _, object in ipairs(self.objects) do if object.relevantKeys then @@ -33,10 +38,12 @@ function Engine:checkObjectAnimation(key) end function Engine:checkObjectKeys(dt) + for _, object in ipairs(self.objects) do + object.controller.animation:update(dt) + end + if not ui.active then for _, object in ipairs(self.objects) do - object.controller.animation:update(dt) - if object.relevantKeys then for _, relevantKey in ipairs(object.relevantKeys) do if love.keyboard.isDown(relevantKey) then @@ -58,6 +65,7 @@ function Engine:update(dt) self:checkObjectKeys(dt) collider:update(dt) self.map:update(dt) + love.audio.update() end function Engine:draw() @@ -143,3 +151,12 @@ function Engine:animate(objectName, animationName) end end end + +function Engine:stopAnimate(objectName) + for _, object in ipairs(self.objects) do + if object.name == objectName then + object.controller:stopAnimation() + break + end + end +end diff --git a/src/engine/sound.lua b/src/engine/sound.lua new file mode 100644 index 0000000..c17916b --- /dev/null +++ b/src/engine/sound.lua @@ -0,0 +1,43 @@ +--will hold the currently playing sources +local sources = {} + +-- check for sources that finished playing and remove them +-- add to love.update +function love.audio.update() + local remove = {} + for _,s in pairs(sources) do + if s.audio:isStopped() then + if s.finishedFunc then s.finishedFunc() end + + remove[#remove + 1] = s + end + end + + for i,s in ipairs(remove) do + sources[s] = nil + end +end + +-- overwrite love.audio.play to create and register source if needed +local play = love.audio.play +function love.audio.play(what, how, loop, finishedFunc) + local src = {} + if type(what) ~= "userdata" or not what:typeOf("Source") then + src.audio = love.audio.newSource(what, how) + src.finishedFunc = finishedFunc + src.audio:setLooping(loop or false) + end + + play(src.audio) + sources[src] = src + + return src +end + +-- stops a source +local stop = love.audio.stop +function love.audio.stop(src) + if not src then return end + stop(src.audio) + sources[src] = nil +end diff --git a/src/story.lua b/src/story.lua index 946e72d..80ebbdc 100644 --- a/src/story.lua +++ b/src/story.lua @@ -2,8 +2,15 @@ local story = {} function story:start(engine) engine:loadLevel('01') - engine:showMessage('Du befindest dich auf einer sturmgebeutelten Insel. Wo ist denn nur die Milch?') + + local stopRomata = function() + engine:stopAnimate('romata') + end + engine:animate('romata', 'play') + engine:playSound('gebet', false, stopRomata) + + engine:showMessage('Du befindest dich auf einer sturmgebeutelten Insel. Wo ist denn nur die Milch?') end return story \ No newline at end of file