local pd = playdate local gfx = playdate.graphics import "player" import "ground" import "backgroundSprite" import "balebaSprite" import "dangerSprite" 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 Level.balebas = {} for i=1, 3 do Level.balebas[i] = Baleba(math.random(410, 900), math.random(10, 210), player) Level.balebas[i]:add() end t = playdate.timer.new(10000) t.repeats = true t.timerEndedCallback = function() if #Level.balebas >= 6 then return end local k = #Level.balebas+1 Level.balebas[k] = Baleba(math.random(410, 900), math.random(10, 210), player) Level.balebas[k]:add() print("Baleba added") end 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