116 lines
3.4 KiB
Lua
116 lines
3.4 KiB
Lua
-- debug --
|
|
if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then
|
|
DEBUG = true
|
|
require("lldebugger").start()
|
|
else DEBUG = false end
|
|
|
|
-- libraries --
|
|
---@type Object
|
|
Object = require 'lib/classic/classic'
|
|
---@type Baton
|
|
Baton = require 'lib/baton/baton'
|
|
---@type Timer
|
|
Timer = require 'lib/hump/timer'
|
|
|
|
-- generic objects --
|
|
Room = require 'obj/Room'
|
|
Area = require 'obj/Area'
|
|
GameObject = require 'obj/GameObject'
|
|
|
|
-- game objects --
|
|
Circle = require 'obj/game/Circle'
|
|
|
|
function love.load()
|
|
---@type Room|nil
|
|
current_room = nil
|
|
|
|
-- load rooms
|
|
local room_files = {}
|
|
print('loading rooms:')
|
|
recursiveEnumerate('rooms', room_files)
|
|
for _,v in ipairs(room_files) do print('',v) end
|
|
requireFiles(room_files, true)
|
|
|
|
-- load input
|
|
input = Baton.new {
|
|
controls = {
|
|
left = {'key:left', 'key:a'},
|
|
right = {'key:right', 'key:d'},
|
|
up = {'key:up', 'key:w'},
|
|
down = {'key:down', 'key:s'},
|
|
action = {'key:z', 'key:space'},
|
|
f1 = {'key:f1'}, f2 = {'key:f2'}, f3 = {'key:f3'}
|
|
},
|
|
pairs = {
|
|
move = {'left', 'right', 'up', 'down'}
|
|
}
|
|
}
|
|
end
|
|
|
|
function love.update(dt)
|
|
input:update(dt)
|
|
if current_room then current_room:update(dt) end
|
|
if input:pressed('f1') then gotoRoom('Stage') end
|
|
end
|
|
|
|
function love.draw()
|
|
if current_room then current_room:draw() end
|
|
end
|
|
|
|
---Enumerates files in a folder and inserts them to a provided table.
|
|
---@param folder string path to folder to scan
|
|
---@param file_list table table to be inserted to
|
|
function recursiveEnumerate(folder, file_list)
|
|
local items = love.filesystem.getDirectoryItems(folder)
|
|
for _, item in ipairs(items) do
|
|
local file = folder .. '/' .. item
|
|
local info = love.filesystem.getInfo(file)
|
|
|
|
if info then -- exists (covers both files and directories)
|
|
if info.type == "file" then
|
|
table.insert(file_list, file)
|
|
elseif info.type == "directory" then
|
|
recursiveEnumerate(file, file_list)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
---Load files table as lua modules.
|
|
---@param files table table containing files to be loaded
|
|
---@param should_return boolean|nil whether required files return a table/object
|
|
function requireFiles(files, should_return)
|
|
local returns = should_return or false
|
|
for _, file in ipairs(files) do
|
|
local module = file:sub(1, -5)
|
|
if not returns then require(module) else
|
|
local filename = module:match("([^/]+)$")
|
|
_G[filename] = require(module)
|
|
end
|
|
end
|
|
end
|
|
|
|
---Go to a specific room.
|
|
---@param room_type string the name of the room as registered
|
|
---@param ... any arguments passed to the room constructor
|
|
function gotoRoom(room_type, ...)
|
|
local Class = _G[room_type]
|
|
if not Class then
|
|
error("room '"..room_type.."' not found in _G")
|
|
end
|
|
if type(Class) ~= "table" and type(Class) ~= "function" then
|
|
error("room '"..room_type.."' is not callable (got "..type(Class)..", does room return itself?)")
|
|
end
|
|
current_room = _G[room_type](...)
|
|
end
|
|
|
|
---Generates and returns random UUID string
|
|
---@return string
|
|
function UUID()
|
|
local fn = function(x)
|
|
local r = math.random(16) - 1
|
|
r = (x == "x") and (r + 1) or (r % 4) + 9
|
|
return ("0123456789abcdef"):sub(r, r)
|
|
end
|
|
return (("xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"):gsub("[xy]", fn))
|
|
end
|