fpv/source/scenes/Game.lua

143 lines
3.8 KiB
Lua
Raw Normal View History

2024-06-01 16:52:11 +03:00
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:setValues()
scene.background = Graphics.image.new("assets/sprites/bg")
2024-06-02 20:25:27 +03:00
scene.telemLostSound = playdate.sound.fileplayer.new("assets/audio/telemko")
2024-06-01 16:52:11 +03:00
scene.telemLostSoundPlayed = false
2024-06-02 20:25:27 +03:00
scene.resultShowed = false
2024-06-01 16:52:11 +03:00
2024-06-02 20:25:27 +03:00
scene.musicEnabled = Noble.Settings.get("music")
2024-06-01 16:52:11 +03:00
2024-06-02 20:25:27 +03:00
scene.balebas = {}
2024-06-01 22:46:36 +03:00
2024-06-02 20:25:27 +03:00
scene.balebaSpawner = playdate.timer.new(10000)
scene.balebaSpawner.repeats = true
2024-06-01 16:52:11 +03:00
2024-06-02 20:25:27 +03:00
scene.levelAudio = playdate.sound.fileplayer.new("assets/audio/war")
scene.levelAudio:setVolume(0.7)
scene.helloAudio = playdate.sound.fileplayer.new("assets/audio/hello")
2024-06-01 16:52:11 +03:00
end
function scene:init()
2024-06-02 20:25:27 +03:00
scene.super.init(self)
scene:setValues()
2024-06-01 16:52:11 +03:00
end
function scene:start()
scene.super.start(self)
2024-06-03 00:31:15 +03:00
playdate.ui.crankIndicator:draw() -- not sure why this is not working
2024-06-01 16:52:11 +03:00
self.optionsMenu:addMenuItem("Main Menu", function() Noble.transition(Menu) end)
2024-06-02 20:25:27 +03:00
Noble.showFPS = true
2024-06-01 16:52:11 +03:00
end
function scene:spawnBaleba()
2024-06-02 20:25:27 +03:00
local balebaCount = #scene.balebas
if balebaCount >= 6 then
2024-06-03 00:31:15 +03:00
scene.balebaSpawner:remove()
2024-06-02 20:25:27 +03:00
return
end
2024-06-01 16:52:11 +03:00
2024-06-03 00:31:15 +03:00
--scene.balebas[balebaCount + 1] = Baleba(math.random(410, 900), math.random(10, 210), scene.player, true)
2024-06-01 16:52:11 +03:00
end
function scene:enter()
2024-06-02 20:25:27 +03:00
scene.super.enter(self)
2024-06-01 16:52:11 +03:00
2024-06-03 00:31:15 +03:00
2024-06-02 20:25:27 +03:00
scene.player = Player(150, 100)
scene.ground = Ground(0, 225, scene.player)
2024-06-01 16:52:11 +03:00
2024-06-02 20:25:27 +03:00
scene.balebaSpawner.timerEndedCallback = function()
2024-06-03 00:31:15 +03:00
scene:spawnBaleba()
2024-06-01 16:52:11 +03:00
end
2024-06-02 20:25:27 +03:00
for i = 1, 3 do
scene:spawnBaleba()
2024-06-01 16:52:11 +03:00
end
2024-06-02 20:25:27 +03:00
scene.helloAudio:play(1)
if scene.musicEnabled then
scene.levelAudio:play(0)
2024-06-01 16:52:11 +03:00
end
2024-06-01 22:46:36 +03:00
2024-06-02 20:25:27 +03:00
playdate.timer.performAfterDelay(1000, function()
2024-06-01 22:46:36 +03:00
scene.tank = Tank(550, 190, scene.player, scene.ground)
2024-06-02 20:25:27 +03:00
scene:addSprite(scene.tank) -- Raw sprite
end)
2024-06-01 16:52:11 +03:00
end
function scene:update()
2024-06-02 20:25:27 +03:00
scene.super.update(self)
2024-06-01 16:52:11 +03:00
if scene.player == nil then
return
end
2024-06-02 20:25:27 +03:00
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
Noble.Text.draw("Bat: " .. math.floor(scene.player.getBat() / 100), 10, 215, Noble.Text.ALIGN_LEFT, false, font)
end
if scene.player.isDead() and not scene.telemLostSoundPlayed then
2024-06-01 22:46:36 +03:00
scene.telemLostSound:play(1)
scene.telemLostSoundPlayed = true
2024-06-02 20:25:27 +03:00
screenShake(1000, 5)
playdate.timer.performAfterDelay(4000, function()
scene.resultShowed = true
2024-06-03 00:31:15 +03:00
local message = "You failed!"
if scene.player.targetDone then
message = "You did it!"
end
c = notify(message, function()
2024-06-02 20:25:27 +03:00
Noble.transition(Menu)
c:remove()
end)
c:moveTo(200, 120)
c:add()
end)
2024-06-01 22:46:36 +03:00
2024-06-01 16:52:11 +03:00
return
end
end
function scene:exit()
scene.super.exit(self)
2024-06-01 22:46:36 +03:00
scene.tank:remove()
2024-06-01 16:52:11 +03:00
scene.telemLostSound:stop()
2024-06-02 20:25:27 +03:00
scene.levelAudio:stop()
scene.balebaSpawner:remove()
2024-06-01 16:52:11 +03:00
2024-06-02 20:25:27 +03:00
Noble.showFPS = false
2024-06-01 16:52:11 +03:00
end
function scene:finish()
2024-06-02 20:25:27 +03:00
scene.super.finish(self)
playdate.display.setScale(1)
end