Compare commits
2 commits
16b4cd92d8
...
ca8dadc90a
| Author | SHA1 | Date | |
|---|---|---|---|
| ca8dadc90a | |||
| df994fb05e |
6 changed files with 100 additions and 60 deletions
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
|
|
@ -1,5 +0,0 @@
|
||||||
{
|
|
||||||
"Lua.diagnostics.disable": [
|
|
||||||
"lowercase-global"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
24
def/Object.d.lua
Normal file
24
def/Object.d.lua
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
---@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
|
||||||
|
|
@ -10,8 +10,15 @@
|
||||||
"love.filesystem.load": "loadfile"
|
"love.filesystem.load": "loadfile"
|
||||||
},
|
},
|
||||||
"Lua.diagnostics.globals": ["love"],
|
"Lua.diagnostics.globals": ["love"],
|
||||||
"Lua.workspace.library": ["${3rd}/love2d/library"],
|
"Lua.workspace.library": [
|
||||||
"Lua.workspace.checkThirdParty": true
|
"${3rd}/love2d/library",
|
||||||
|
"${3rd}/classic",
|
||||||
|
"${workspaceFolder}/def"
|
||||||
|
],
|
||||||
|
"Lua.workspace.checkThirdParty": true,
|
||||||
|
"Lua.diagnostics.disable": [
|
||||||
|
"lowercase-global"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"launch": {
|
"launch": {
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
|
|
|
||||||
63
main.lua
63
main.lua
|
|
@ -1,17 +1,26 @@
|
||||||
-- debug --
|
-- debug
|
||||||
-- if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then
|
if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then
|
||||||
-- require("lldebugger").start()
|
require("lldebugger").start()
|
||||||
-- end
|
end
|
||||||
|
|
||||||
-- libraries --
|
-- libraries
|
||||||
Object = require 'lib/classic/classic'
|
Object = require 'lib/classic/classic'
|
||||||
Baton = require 'lib/baton/baton'
|
Baton = require 'lib/baton/baton'
|
||||||
Timer = require 'lib/hump/timer'
|
Timer = require 'lib/hump/timer'
|
||||||
|
|
||||||
-- objects --
|
-- objects
|
||||||
Healthbar = require 'obj/Healthbar'
|
Room = require 'obj/Room'
|
||||||
|
|
||||||
function love.load()
|
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 {
|
input = Baton.new {
|
||||||
controls = {
|
controls = {
|
||||||
left = {'key:left', 'key:a'},
|
left = {'key:left', 'key:a'},
|
||||||
|
|
@ -24,16 +33,46 @@ function love.load()
|
||||||
move = {'left', 'right', 'up', 'down'}
|
move = {'left', 'right', 'up', 'down'}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
timer = Timer()
|
|
||||||
health = Healthbar()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
input:update(dt)
|
input:update(dt)
|
||||||
timer:update(dt)
|
if current_room then current_room:update(dt) end
|
||||||
health:update(dt)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.draw()
|
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
|
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