diff --git a/asteroids/actors.lua b/asteroids/actors.lua index 31f2cf4..df15fd4 100644 --- a/asteroids/actors.lua +++ b/asteroids/actors.lua @@ -91,6 +91,9 @@ function actors.buildActors(game) for k, v in pairs(ship) do instance[k] = v end + instance.engineSound = love.audio.newSource(instance.engineSound) + instance.engineSound:setVolume(0) + instance.engineSound:setLooping(true) setmetatable(instance, ships_mt) return instance end diff --git a/asteroids/main.lua b/asteroids/main.lua index e45fcb5..f2cfbf8 100644 --- a/asteroids/main.lua +++ b/asteroids/main.lua @@ -18,6 +18,7 @@ local function initGame() }, sprite = love.graphics.newImage('resources/playerShip1_blue.png'), maxThrust = 200, + engineSound = love.sound.newSoundData('resources/enginehum3.ogg'), }, asteroids = { stages = { @@ -78,6 +79,12 @@ end local state = {} local function reset() + if state.ship then + state.ship.engineSound:stop() + for _, bullet in ipairs(state.bullets) do + bullet.sound:stop() + end + end state = initState() end @@ -98,6 +105,17 @@ function love.update(dt) if state.ship.throttle > 0 then local speed = state.ship.throttle * state.ship.maxThrust * dt phys.apply(state.ship, { speed = speed, angle = state.ship.facing }) + if not state.ship.engineSound:isPlaying() then + state.ship.engineSound:play() + end + end + state.ship.engineSound:setVolume(state.ship.throttle * 0.5) + -- TODO which is better, keeping the sound playing looping at 0 or checking the + -- state each update? + if state.ship.throttle == 0 then + if state.ship.engineSound:isPlaying() then + state.ship.engineSound:stop() + end end local spin = state.controls.joystick:getGamepadAxis('leftx') * game.controls.ship.turnRate diff --git a/asteroids/resources/enginehum3.ogg b/asteroids/resources/enginehum3.ogg new file mode 100644 index 0000000..26d8990 Binary files /dev/null and b/asteroids/resources/enginehum3.ogg differ