Something went wrong. Try again.
A Love2D remaster of my bug-catching-cult-classic Bait and Switch
Something went wrong. Try again.
6.8 kB · 298 lines
Lua
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299local config = require("src.config")local canvas
local player = require("src.player")local assets = require("src.assets")local net = require("src.net")local enemy = require("src.enemy")local projectile = require("src.projectile")local particle = require("src.particle")local speedyBug = require("src.speedyBug")local tutorial = require("src.tutorial")local utils = require("src.utils")
local gameState = require("src.gameState")local mainMenu = require("src.mainMenu")local upgradeMenu = require("src.upgradeMenu")local deathScreen = require("src.deathScreen")local pauseMenu = require("src.pauseMenu")local settingsMenu = require("src.settingsMenu")
local ui = require("src.ui")local input = require("src.input")local save = require("src.save")
function love.load() love.window.setMode(config.window.width, config.window.height, { resizable = false }) love.window.setTitle(config.game.title) love.graphics.setDefaultFilter("nearest", "nearest")
love.mouse.setVisible(false)
canvas = love.graphics.newCanvas(config.screen.width, config.screen.height)
assets.load() input.load() player.load() enemy.load() net.load() projectile.load() speedyBug.load() particle.load() ui.load() upgradeMenu.load() pauseMenu.load() settingsMenu.load() tutorial.load() mainMenu.load()
local data = save.read() if data then gameState.deathScreen.bestWave = data.bestWave or 1 endend
local stateUpdate = {}
function stateUpdate.MAIN_MENU(dt) mainMenu.update(dt)end
function stateUpdate.MAIN_MENU_SETTINGS(dt) settingsMenu.update(dt) if input.isActionPressed("pause") then settingsMenu.hide() endend
function stateUpdate.MENU_TO_PLAY(dt) mainMenu.update(dt)end
function stateUpdate.PLAYING(dt) if mainMenu.fadingOut then mainMenu.fadeOutAlpha = mainMenu.fadeOutAlpha - dt * mainMenu.config.fadeOutSpeed if mainMenu.fadeOutAlpha <= 0 then mainMenu.fadeOutAlpha = 0 mainMenu.fadingOut = false end end
particle.update(dt) ui.update(dt) projectile.update(dt) player.update(dt) net.update(dt) tutorial.update(dt)
speedyBug.update(dt) if not gameState.tutorialMode then enemy.update(dt) else local n = #enemy.active local i = 1 while i <= n do local e = enemy.active[i] if e.dead then enemy.active[i] = enemy.active[n] enemy.active[n] = nil n = n - 1 else i = i + 1 end end end
ui.setProgress(gameState.stats.waveKills, gameState.killsPerWave)
if input.isActionPressed("pause") then pauseMenu.toggle() endend
function stateUpdate.PAUSED_MENU(dt) pauseMenu.update(dt) if input.isActionPressed("pause") then pauseMenu.toggle() endend
function stateUpdate.PAUSED_SETTINGS(dt) settingsMenu.update(dt) if input.isActionPressed("pause") then settingsMenu.hide() endend
function stateUpdate.PAUSED_UPGRADE(dt) upgradeMenu.update(dt)end
function stateUpdate.DEATH_ANIMATING(dt) particle.update(dt) projectile.update(dt) speedyBug.update(dt) deathScreen.update(dt)end
function stateUpdate.DEATH_SCREEN(dt) deathScreen.update(dt)end
function love.update(dt) input.update() stateUpdate[gameState.current](dt) input.clear()end
local stateMousepressed = {}
function stateMousepressed.MAIN_MENU(x, y, button) mainMenu.mousepressed(x, y, button)end
function stateMousepressed.MAIN_MENU_SETTINGS(x, y, button) settingsMenu.mousepressed(x, y, button)end
function stateMousepressed.MENU_TO_PLAY() end
function stateMousepressed.PLAYING(x, y, button) net.mousepressed(x, y, button)end
function stateMousepressed.PAUSED_MENU(x, y, button) pauseMenu.mousepressed(x, y, button)end
function stateMousepressed.PAUSED_SETTINGS(x, y, button) settingsMenu.mousepressed(x, y, button)end
function stateMousepressed.PAUSED_UPGRADE(x, y, button) upgradeMenu.mousepressed(x, y, button)end
function stateMousepressed.DEATH_ANIMATING() endfunction stateMousepressed.DEATH_SCREEN() end
function love.mousepressed(x, y, button) input.mousepressed(x, y, button) stateMousepressed[gameState.current](x, y, button)end
function love.mousereleased(x, y, button) input.mousereleased(x, y, button)end
function love.keypressed(key) input.keypressed(key)end
function love.keyreleased(key) input.keyreleased(key)end
local function drawGameWorld() if not player.dead then player.draw() end enemy.draw() speedyBug.draw() net.draw() projectile.draw() particle.draw() tutorial.draw() ui.draw()end
local function drawCanvasToScreen() love.graphics.setCanvas() love.graphics.clear(0, 0, 0) love.graphics.setColor(1, 1, 1) local display = gameState.display if display then love.graphics.draw(canvas, display.offsetX, display.offsetY, 0, display.scale, display.scale) else local scaleX = love.graphics.getWidth() / config.screen.width local scaleY = love.graphics.getHeight() / config.screen.height love.graphics.draw(canvas, 0, 0, 0, scaleX, scaleY) endend
local stateDraw = {}
function stateDraw.MAIN_MENU() mainMenu.draw() drawCursor()end
function stateDraw.MAIN_MENU_SETTINGS() mainMenu.draw() settingsMenu.draw() drawCursor()end
function stateDraw.MENU_TO_PLAY() mainMenu.draw() drawCursor()end
function stateDraw.PLAYING() drawGameWorld()
if mainMenu.fadingOut then love.graphics.setColor(1, 1, 1, mainMenu.fadeOutAlpha) love.graphics.draw(assets.bg, 0, 0) end
drawCursor()end
function stateDraw.PAUSED_MENU() drawGameWorld() pauseMenu.draw() drawCursor()end
function stateDraw.PAUSED_SETTINGS() drawGameWorld() settingsMenu.draw() drawCursor()end
function stateDraw.PAUSED_UPGRADE() drawGameWorld() upgradeMenu.draw() drawCursor()end
function stateDraw.DEATH_ANIMATING() drawGameWorld()end
function stateDraw.DEATH_SCREEN() drawGameWorld() deathScreen.draw()end
function love.draw() love.graphics.setCanvas(canvas) love.graphics.clear() love.graphics.setColor(1, 1, 1) love.graphics.draw(assets.bg, 0, 0)
stateDraw[gameState.current]()
drawCanvasToScreen()end
function drawCursor() local mx, my = input.getMousePosition() local ox = assets.cursor:getWidth() / 2 local oy = assets.cursor:getHeight() / 2 local rot = love.timer.getTime() * 2
utils.drawWithShadow(assets.cursor, mx, my, rot, 1, 1, ox, oy, 1, config.visual.shadowColor)end