notsanequarium/main.lua

173 lines
4.6 KiB
Lua
Raw Permalink Normal View History

2021-01-22 22:33:08 +00:00
require 'const'
require 'util'
constr = require 'constructors'
2021-01-23 16:37:37 +00:00
require 'lib.audio'
2021-01-22 22:33:08 +00:00
assets = require 'lib.assets'
ease = require 'lib.ease'
bench = require 'lib.benchmark'
2021-01-23 16:37:37 +00:00
tick = require 'lib.tick'
2021-02-09 18:06:08 +00:00
lurker = require 'lib.lurker'
2021-01-22 22:33:08 +00:00
2021-01-23 23:30:01 +00:00
scenes = {
gameplay = require('scenes/gameplay/main')
}
local defaultscene = scenes.gameplay
scene = defaultscene
2021-01-22 22:33:08 +00:00
sprites = {}
sound_path = {}
music_path = {}
2021-01-23 13:46:02 +00:00
fonts = {}
2021-01-25 15:17:32 +00:00
cursors = {}
2021-01-22 22:33:08 +00:00
debug = false
2021-01-25 19:58:16 +00:00
is_mobile = love.system.getOS() == "Android" or love.system.getOS() == "iOS"
2021-01-22 22:33:08 +00:00
2021-02-09 18:06:08 +00:00
options = { -- temporary
pause_on_unfocus = true,
render_shadow = true,
}
2021-01-23 23:30:01 +00:00
function newAnimation(image, width, height)
2021-01-22 22:33:08 +00:00
local animation = {}
animation.spriteSheet = image;
animation.quads = {};
animation.width = width
animation.height = height
for y = 0, image:getHeight() - height, height do
for x = 0, image:getWidth() - width, width do
table.insert(animation.quads, love.graphics.newQuad(x, y, width, height, image:getDimensions()))
end
end
return animation
end
function love.load()
assets.clear()
assets.load('assets')
2021-01-23 13:46:02 +00:00
fonts.pix = love.graphics.newFont('assets/fonts/pix.ttf', 6)
fonts.continuum = love.graphics.newFont('assets/fonts/cont.ttf', 14)
fonts.default = love.graphics.newFont(12)
2021-01-23 23:30:01 +00:00
if scene.load then scene.load() end
2021-01-25 15:17:32 +00:00
cursors.default = love.mouse.newCursor('assets/sprites/cursor/pointer.png', sprites['cursor/pointer']:getWidth()/2, sprites['cursor/pointer']:getHeight()/2)
cursors.hover = love.mouse.newCursor('assets/sprites/cursor/hand.png', sprites['cursor/hand']:getWidth()/2, sprites['cursor/hand']:getHeight()/2)
cursors.drag = love.mouse.newCursor('assets/sprites/cursor/dragging.png', sprites['cursor/dragging']:getWidth()/2, sprites['cursor/dragging']:getHeight()/2)
2021-01-25 19:58:16 +00:00
if not is_mobile then
love.mouse.setCursor(cursors.default)
end
if is_mobile then
2021-01-25 20:31:30 +00:00
local winwidth, winheight = love.graphics.getDimensions()
2021-01-25 19:58:16 +00:00
love.window.setMode(winwidth, winheight, {borderless=true, resizable=false, minwidth=705, minheight=510, fullscreen=true})
end
2021-01-22 22:33:08 +00:00
end
2021-01-25 16:42:27 +00:00
local cursor
2021-03-27 16:48:01 +00:00
local lastcursor = nil
2021-01-25 16:42:27 +00:00
function setCursor(mouse)
cursor = mouse
end
2021-01-23 23:30:01 +00:00
frame = 0
2021-01-22 22:33:08 +00:00
function love.update(dt)
2021-01-25 16:42:27 +00:00
cursor = nil
2021-01-22 22:33:08 +00:00
frame = frame + 1
bench.update()
2021-01-23 16:37:37 +00:00
tick.update(dt)
2021-02-09 18:06:08 +00:00
lurker.update()
2021-01-22 22:33:08 +00:00
bench.startBenchmark('update')
2021-01-23 23:30:01 +00:00
if scene.update then scene.update(dt) end
2021-01-22 22:33:08 +00:00
bench.stopBenchmark('update')
2021-01-25 16:42:27 +00:00
2021-03-27 16:48:01 +00:00
if not cursor then cursor = 'default' end
if (not is_mobile) and (cursor ~= lastcursor) then
love.mouse.setCursor(cursors[cursor])
2021-01-25 16:42:27 +00:00
end
2021-03-27 16:48:01 +00:00
lastcursor = cursor
2021-01-22 22:33:08 +00:00
end
function love.draw()
2021-01-23 13:46:02 +00:00
love.graphics.setFont(fonts.default)
2021-01-22 22:33:08 +00:00
love.graphics.setColor(1, 1, 1)
local sw, sh = love.graphics.getDimensions()
2021-01-23 23:30:01 +00:00
bench.startBenchmark('render')
2021-01-23 19:42:56 +00:00
2021-01-23 23:30:01 +00:00
if scene.draw then scene.draw() end
2021-01-23 19:42:56 +00:00
2021-01-22 22:33:08 +00:00
love.graphics.setColor(1, 1, 1, 1)
love.graphics.print('FPS: ' .. 1 / love.timer.getDelta(), 0, sh - 16)
2021-01-25 18:34:38 +00:00
if paused then
love.graphics.setBlendMode('subtract')
love.graphics.setColor(1, 1, 1, 0.1)
love.graphics.rectangle('fill', 0, 0, sw, sh)
love.graphics.setBlendMode('alpha')
love.graphics.setColor(0, 0, 0, 0.6)
love.graphics.rectangle('fill', 0, 0, sw, sh)
love.graphics.setColor(1, 1, 1)
love.graphics.print('paused, space to unpause', 20, 20)
2021-02-09 18:06:08 +00:00
y = 30
for k,v in pairs(options) do
y = y + 16
local text = love.graphics.newText(love.graphics.getFont(), k .. ' = ' .. (v and 'true' or 'false')) -- todo: cache!!!!!!!! please
local invertcol = mouseOverBox(20, y + 1, text:getWidth(), text:getHeight())
if invertcol then love.graphics.setColor(1, 1, 1) else love.graphics.setColor(0, 0, 0) end
love.graphics.rectangle('fill', 20, y + 1, text:getWidth(), text:getHeight())
if invertcol then love.graphics.setColor(0, 0, 0) else love.graphics.setColor(1, 1, 1) end
love.graphics.draw(text, 20, y)
end
2021-01-25 18:34:38 +00:00
end
2021-01-22 22:33:08 +00:00
if debug then bench.renderBenchmark() end
bench.stopBenchmark('render')
end
function love.mousepressed(x, y, b)
2021-01-23 23:30:01 +00:00
if scene.mousepressed then scene.mousepressed(x, y, b) end
2021-02-09 18:06:08 +00:00
if is_mobile then
if paused then paused = not paused end
end
y = 30
for k,v in pairs(options) do
y = y + 16
local text = love.graphics.newText(love.graphics.getFont(), k .. ' = ' .. (v and 'true' or 'false')) -- todo: cache!!!!!!!! please
if mouseOverBox(20, y + 1, text:getWidth(), text:getHeight()) then
options[k] = not v
end
end
2021-01-23 13:46:02 +00:00
end
2021-01-23 16:37:37 +00:00
function love.mousereleased(x, y, b)
2021-01-23 23:30:01 +00:00
if scene.mousereleased then scene.mousereleased(x, y, b) end
2021-01-23 16:37:37 +00:00
end
2021-01-22 22:33:08 +00:00
function love.keypressed(key)
if key == 'f3' then
debug = not debug
2021-01-25 18:34:38 +00:00
elseif key == 'space' then
paused = false
2021-01-22 22:33:08 +00:00
end
2021-02-09 18:06:08 +00:00
end
function love.focus(f)
if not f and options.pause_on_unfocus then
paused = true
end
2021-03-27 16:48:01 +00:00
end