fpv/source/scenes/Game.lua

175 lines
4.6 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
2024-06-03 20:08:46 +03:00
function scene:drawBackground()
local speed = 0.1
if Ground.player ~= nil and Ground.player.isMovingRight() == true then
speed = 0.2
end
2024-06-01 16:52:11 +03:00
2024-06-03 20:08:46 +03:00
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/bgTest")
scene.bgX = 0
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-03 20:08:46 +03:00
scene.tank = nil
scene.distance = 900
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-03 20:08:46 +03:00
--scene.helloAudio:play(1)
2024-06-02 20:25:27 +03:00
if scene.musicEnabled then
2024-06-03 20:08:46 +03:00
--scene.levelAudio:play(0)
2024-06-01 16:52:11 +03:00
end
2024-06-03 20:08:46 +03:00
end
2024-06-01 22:46:36 +03:00
2024-06-03 20:08:46 +03:00
function round(number)
local formatted = string.format("%.2f", number)
return formatted
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-03 20:08:46 +03:00
if scene.distance > 0 then
scene.distance = scene.distance - 1
end
if scene.distance < 850 and scene.tank == nil then
scene.tank = Tank(500, 190, scene.player, scene.ground)
scene:addSprite(scene.tank) -- Raw sprite
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
2024-06-03 20:08:46 +03:00
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(scene.distance .. "m", 200, 40, Noble.Text.ALIGN_CENTER, false, font)
2024-06-02 20:25:27 +03:00
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