fpv/source/scenes/Game.lua
2024-06-12 19:02:03 +03:00

182 lines
4.8 KiB
Lua

Game = {}
class("Game").extends(BaseScene)
local scene = Game
local font = Graphics.font.new('assets/fonts/Mini Sans 2X')
local function screenShake(shakeTime, shakeMagnitude)
local shakeTimer = playdate.timer.new(shakeTime, shakeMagnitude, 0)
shakeTimer.updateCallback = function(timer)
local magnitude = math.floor(timer.value)
local shakeX = math.random(-magnitude, magnitude)
local shakeY = math.random(-magnitude, magnitude)
playdate.display.setOffset(shakeX, shakeY)
end
shakeTimer.timerEndedCallback = function()
playdate.display.setOffset(0, 0)
end
end
function scene:drawBackground()
local speed = 0.1
if scene.ground ~= nil then
speed = scene.ground.moveSpeed * 0.4
end
if scene.distance > 0 then
scene.bgX = scene.bgX - speed
end
if scene.bgX <= -400 then
scene.bgX = 0
end
self.bg:draw(scene.bgX or 0, 0)
end
function scene:setValues()
self.bg = Graphics.image.new("assets/sprites/bg1")
scene.bgX = 0
scene.telemLostSound = playdate.sound.fileplayer.new("assets/audio/telemko")
scene.telemLostSoundPlayed = false
scene.resultShowed = false
scene.musicEnabled = Noble.Settings.get("music")
scene.difficulty = Noble.Settings.get("difficulty")
scene.balebas = {}
scene.balebaSpawner = playdate.timer.new(10000)
scene.balebaSpawner.repeats = true
scene.levelAudio = playdate.sound.fileplayer.new("assets/audio/game")
scene.levelAudio:setVolume(0.7)
scene.helloAudio = playdate.sound.fileplayer.new("assets/audio/hello")
scene.tank = nil
scene.distance = DifficultySettings[scene.difficulty].distance
end
function scene:init()
scene.super.init(self)
scene:setValues()
end
function scene:start()
scene.super.start(self)
playdate.ui.crankIndicator:draw() -- not sure why this is not working
self.optionsMenu:addMenuItem("Main Menu", function() Noble.transition(Menu) end)
Noble.showFPS = false
end
function scene:spawnBaleba()
local balebaCount = #scene.balebas
if balebaCount >= 6 then
scene.balebaSpawner:remove()
return
end
scene.balebas[balebaCount + 1] = Baleba(math.random(410, 900), math.random(10, 210), scene.player, true)
end
function scene:enter()
scene.super.enter(self)
scene.player = Player(150, 100)
scene.ground = Ground(0, 225, scene.player)
scene.balebaSpawner.timerEndedCallback = function()
scene:spawnBaleba()
end
for i = 1, 3 do
scene:spawnBaleba()
end
scene.helloAudio:play(1)
if scene.musicEnabled then
scene.levelAudio:play(0)
end
end
function round(number)
local formatted = string.format("%.2f", number)
return formatted
end
function scene:update()
scene.super.update(self)
if scene.player == nil then
return
end
if scene.distance > 0 then
scene.distance = scene.distance - scene.ground.moveSpeed
end
if scene.distance < 0 then -- SHIT
scene.distance = 0
end
if scene.distance < 150 and scene.tank == nil then
scene.tank = Tank(480, 190, scene.ground)
scene:addSprite(scene.tank) -- Raw sprite
end
if scene.player.isDead() then
if scene.resultShowed ~= true then
Noble.Text.draw("Telemetry Lost", 200, 110, Noble.Text.ALIGN_CENTER, false, font)
end
else
local t = scene.player.getBat() / 10000
local lerpBat = playdate.math.lerp(12.0, 16.8, t)
Noble.Text.draw(round(lerpBat) .. "v", 10, 210, Noble.Text.ALIGN_LEFT, false, font)
Noble.Text.draw(math.round(scene.distance) .. "m", 200, 20, Noble.Text.ALIGN_CENTER, false, font)
end
if scene.player.isDead() and not scene.telemLostSoundPlayed then
scene.telemLostSound:play(1)
scene.telemLostSoundPlayed = true
screenShake(1000, 5)
playdate.timer.performAfterDelay(4000, function()
scene.resultShowed = true
local message = "You failed!"
if scene.player.targetDone then
message = "You did it!"
end
c = notify(message, function()
Noble.transition(Menu)
c:remove()
end)
c:moveTo(200, 120)
c:add()
end)
return
end
end
function scene:exit()
scene.super.exit(self)
if scene.tank ~= nil then
scene.tank:remove()
end
scene.telemLostSound:stop()
scene.levelAudio:stop()
scene.balebaSpawner:remove()
Noble.showFPS = false
end
function scene:finish()
scene.super.finish(self)
playdate.display.setScale(1)
end