minimal sound engine

This commit is contained in:
Sebastian Hugentobler 2014-06-04 15:04:58 +02:00
parent ea83d2431a
commit deb8199f9c
4 changed files with 73 additions and 6 deletions

BIN
src/assets/sound/gebet.ogg Normal file

Binary file not shown.

View File

@ -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

43
src/engine/sound.lua Normal file
View File

@ -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

View File

@ -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