local pd = playdate local gfx = playdate.graphics import "player" import "ground" import "backgroundSprite" class("Level").extends() -- This function relies on the use of timers, so the timer core library -- must be imported, and updateTimers() must be called in the update loop local function screenShake(shakeTime, shakeMagnitude) -- Creating a value timer that goes from shakeMagnitude to 0, over -- the course of 'shakeTime' milliseconds local shakeTimer = playdate.timer.new(shakeTime, shakeMagnitude, 0) -- Every frame when the timer is active, we shake the screen shakeTimer.updateCallback = function(timer) -- Using the timer value, so the shaking magnitude -- gradually decreases over time local magnitude = math.floor(timer.value) local shakeX = math.random(-magnitude, magnitude) local shakeY = math.random(-magnitude, magnitude) playdate.display.setOffset(shakeX, shakeY) end -- Resetting the display offset at the end of the screen shake shakeTimer.timerEndedCallback = function() playdate.display.setOffset(0, 0) end end function Level:init() player = Player(30, 30) player:add() ground = Ground(400, 225, player) ground:add() BackGround() local fp = playdate.sound.fileplayer.new( "audio/war" ) local hello = playdate.sound.fileplayer.new( "audio/hello" ) hello:play(1) fp:setVolume(0.7) fp:play(0) Level.telemLostSound = playdate.sound.fileplayer.new( "audio/telemko" ) Level.telemLostSoundPlayed = false end function Level:update() if player.isDead() then if not Level.telemLostSoundPlayed then Level.telemLostSound:play(1) Level.telemLostSoundPlayed = true screenShake(500, 5) end gfx.drawText("Telemetry Lost", 100, 110) end local bat = player.getBat() bat = math.floor(bat/100) gfx.drawText("Bat: " .. bat, 10, 215) -- gfx.drawText("Dis: " .. player.dischargeRate, 100, 215) end