fpv/source/scenes/Game.lua

161 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")
scene.telemLostSound = playdate.sound.fileplayer.new( "assets/audio/telemko" )
scene.telemLostSoundPlayed = false
scene.balebas = {}
scene.t = playdate.timer.new(10000)
scene.t.repeats = true
2024-06-01 22:46:36 +03:00
scene.tankTimer = playdate.timer.new(1000)
2024-06-01 16:52:11 +03:00
scene.fp = playdate.sound.fileplayer.new( "assets/audio/war" )
scene.fp:setVolume(0.7)
scene.hello = playdate.sound.fileplayer.new( "assets/audio/hello" )
end
function scene:init()
scene.super.init(self)
scene:setValues()
end
function scene:start()
scene.super.start(self)
self.optionsMenu:addMenuItem("Main Menu", function() Noble.transition(Menu) end)
Noble.showFPS = true
end
function scene:spawnBaleba()
local baleba = Baleba(math.random(410, 900), math.random(10, 210), scene.player, true)
baleba:add()
return baleba
end
function scene:enter()
scene.super.enter(self)
scene.player = Player(30, 30)
scene.player:add()
scene.ground = Ground(400, 225, scene.player)
scene.ground:add()
scene.t.timerEndedCallback = function()
if #scene.balebas >= 6 then
return
end
local k = #scene.balebas+1
2024-06-01 22:46:36 +03:00
--scene.balebas[k] = scene:spawnBaleba()
2024-06-01 16:52:11 +03:00
end
for i=1, 3 do
scene.balebas[i] = scene:spawnBaleba()
end
local musicEnabled = Noble.Settings.get("music")
scene.hello:play(1)
if musicEnabled then
scene.fp:play(0)
end
2024-06-01 22:46:36 +03:00
scene.tankTimer.timerEndedCallback = function()
scene.tank = Tank(550, 190, scene.player, scene.ground)
scene.tank:add()
end
2024-06-01 16:52:11 +03:00
end
function scene:update()
scene.super.update(self)
if scene.player == nil then
Noble.Text.draw("Telemetry Lost", 200, 110, Noble.Text.ALIGN_CENTER, false, font)
return
end
2024-06-01 22:46:36 +03:00
if scene.player.isDead() and not scene.telemLostSoundPlayed then
scene.telemLostSound:play(1)
scene.telemLostSoundPlayed = true
screenShake(500, 5)
local et = playdate.timer.new(6000)
2024-06-01 16:52:11 +03:00
et.timerEndedCallback = function()
Noble.transition(Menu)
end
return
end
local bat = scene.player.getBat()
bat = math.floor(bat/100)
Noble.Text.draw("Bat: " .. bat, 10, 215, Noble.Text.ALIGN_LEFT, false, font)
end
function scene:destroyPlayer()
if scene.player ~= nil then
scene.player:remove()
scene.player = nil
end
end
function scene:exit()
scene.super.exit(self)
scene:destroyPlayer()
scene.ground:remove()
scene.ground = nil
2024-06-01 22:46:36 +03:00
scene.tank:remove()
scene.ground = nil
2024-06-01 16:52:11 +03:00
for i=1, #scene.balebas do
scene.balebas[i]:destroy()
scene.balebas[i] = nil
end
scene.telemLostSound:stop()
scene.telemLostSoundPlayed = false
scene.fp:stop()
scene.t:remove()
scene.t = nil
2024-06-01 22:46:36 +03:00
scene.tankTimer:remove()
scene.tankTimer = nil
2024-06-01 16:52:11 +03:00
Noble.showFPS = false
end
function scene:finish()
scene.super.finish(self)
playdate.display.setScale(1)
end