room boilerplate code
This commit is contained in:
parent
df994fb05e
commit
ca8dadc90a
3 changed files with 67 additions and 53 deletions
63
main.lua
63
main.lua
|
|
@ -1,17 +1,26 @@
|
|||
-- debug --
|
||||
-- if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then
|
||||
-- require("lldebugger").start()
|
||||
-- end
|
||||
-- debug
|
||||
if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then
|
||||
require("lldebugger").start()
|
||||
end
|
||||
|
||||
-- libraries --
|
||||
-- libraries
|
||||
Object = require 'lib/classic/classic'
|
||||
Baton = require 'lib/baton/baton'
|
||||
Timer = require 'lib/hump/timer'
|
||||
|
||||
-- objects --
|
||||
Healthbar = require 'obj/Healthbar'
|
||||
-- objects
|
||||
Room = require 'obj/Room'
|
||||
|
||||
function love.load()
|
||||
---@type Room|nil
|
||||
current_room = nil
|
||||
|
||||
-- load rooms
|
||||
local room_files = {}
|
||||
recursiveEnumerate('rooms', room_files)
|
||||
requireFiles(room_files)
|
||||
|
||||
-- load input
|
||||
input = Baton.new {
|
||||
controls = {
|
||||
left = {'key:left', 'key:a'},
|
||||
|
|
@ -24,16 +33,46 @@ function love.load()
|
|||
move = {'left', 'right', 'up', 'down'}
|
||||
}
|
||||
}
|
||||
timer = Timer()
|
||||
health = Healthbar()
|
||||
end
|
||||
|
||||
function love.update(dt)
|
||||
input:update(dt)
|
||||
timer:update(dt)
|
||||
health:update(dt)
|
||||
if current_room then current_room:update(dt) end
|
||||
end
|
||||
|
||||
function love.draw()
|
||||
health: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
|
||||
---@diagnostic disable-next-line: undefined-field
|
||||
if love.filesystem.isFile(file) then
|
||||
table.insert(file_list, file)
|
||||
---@diagnostic disable-next-line: undefined-field
|
||||
elseif love.filesystem.isDirectory(file) then
|
||||
recursiveEnumerate(file, file_list)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---Load files table as lua modules.
|
||||
---@param files table table containing files to be loaded
|
||||
function requireFiles(files)
|
||||
for _, file in ipairs(files) do
|
||||
local file = file:sub(1, -5)
|
||||
require(file)
|
||||
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, ...)
|
||||
current_room = _G[room_type](...)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,41 +0,0 @@
|
|||
local Healthbar = Object:extend()
|
||||
|
||||
function Healthbar:new(x, y, hp)
|
||||
self.x = x or 100
|
||||
self.y = y or 100
|
||||
self.hp = hp or 100
|
||||
self.max_width = 200
|
||||
self.qwidth = (self.hp / 100) * self.max_width
|
||||
self.lwidth = self.qwidth
|
||||
end
|
||||
|
||||
function Healthbar:decrease(amount)
|
||||
if self.hp ~= 0 then
|
||||
if fg then timer:cancel(fg) end
|
||||
if bg then timer:cancel(bg) end
|
||||
if self.hp - amount <= 0 then
|
||||
self.hp = 0
|
||||
else
|
||||
self.hp = self.hp - amount
|
||||
end
|
||||
local target_width = self.max_width * (self.hp / 100)
|
||||
fg = timer:tween(0.3, self, {qwidth = target_width}, 'in-out-cubic')
|
||||
bg = timer:tween(0.6, self, {lwidth = target_width}, 'in-out-cubic')
|
||||
end
|
||||
end
|
||||
|
||||
function Healthbar:update(dt)
|
||||
if input:pressed('right') then self:decrease(20) end
|
||||
end
|
||||
|
||||
function Healthbar:draw()
|
||||
-- trailing hp
|
||||
love.graphics.setColor(1, 0.5, 0.5)
|
||||
love.graphics.rectangle("fill", self.x, self.y, self.lwidth, 20)
|
||||
|
||||
-- healthbar
|
||||
love.graphics.setColor(1, 0.2, 0.2)
|
||||
love.graphics.rectangle("fill", self.x, self.y, self.qwidth, 20)
|
||||
end
|
||||
|
||||
return Healthbar
|
||||
16
obj/Room.lua
Normal file
16
obj/Room.lua
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
---@module 'obj/Room'
|
||||
|
||||
---@class Room:Object
|
||||
local Room = Object:extend()
|
||||
|
||||
---Instantiates room.
|
||||
function Room:new() end
|
||||
|
||||
---Updates room (see [love.update()](lua://love.update))
|
||||
---@param dt number delta time
|
||||
function Room:update(dt) end
|
||||
|
||||
---Draws graphics (see [love.draw()](lua://love.draw))
|
||||
function Room:draw() end
|
||||
|
||||
return Room
|
||||
Loading…
Add table
Reference in a new issue