Compare commits

..

No commits in common. "ca8dadc90a727531f725679dbf72f05b3442d30e" and "16b4cd92d83f33db58990b3611bb0a4fce03e462" have entirely different histories.

6 changed files with 60 additions and 100 deletions

5
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,5 @@
{
"Lua.diagnostics.disable": [
"lowercase-global"
]
}

View file

@ -1,24 +0,0 @@
---@diagnostic disable: undefined-field
---@meta
---@module 'classic'
---@class Object
local Object = {}
---Create a new instance of the class.
---@return self
function Object:new(...) end
---Call the super-class constructor.
---@param self Object
function Object.super.new(self, ...) end
---Create a subclass.
---@return Object # the new subclass (its metatable is set up)
function Object:extend() end
---Mix-in a table of methods into the class.
---@param mixin table
function Object:implement(mixin) end
return Object

View file

@ -10,15 +10,8 @@
"love.filesystem.load": "loadfile"
},
"Lua.diagnostics.globals": ["love"],
"Lua.workspace.library": [
"${3rd}/love2d/library",
"${3rd}/classic",
"${workspaceFolder}/def"
],
"Lua.workspace.checkThirdParty": true,
"Lua.diagnostics.disable": [
"lowercase-global"
]
"Lua.workspace.library": ["${3rd}/love2d/library"],
"Lua.workspace.checkThirdParty": true
},
"launch": {
"version": "0.2.0",

View file

@ -1,26 +1,17 @@
-- 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
Room = require 'obj/Room'
-- objects --
Healthbar = require 'obj/Healthbar'
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'},
@ -33,46 +24,16 @@ function love.load()
move = {'left', 'right', 'up', 'down'}
}
}
timer = Timer()
health = Healthbar()
end
function love.update(dt)
input:update(dt)
if current_room then current_room:update(dt) end
timer:update(dt)
health:update(dt)
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
---@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](...)
health:draw()
end

41
obj/Healthbar.lua Normal file
View file

@ -0,0 +1,41 @@
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

View file

@ -1,16 +0,0 @@
---@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