area and game object boilerplate

This commit is contained in:
yuki 2025-11-10 01:37:32 -03:00
parent 3e3d332967
commit abcd5371d8
9 changed files with 78 additions and 150 deletions

View file

@ -14,9 +14,7 @@ Timer = require 'lib/hump/timer'
-- objects --
Room = require 'obj/Room'
Circle = require 'obj/Circle'
Rectangle = require 'obj/Rectangle'
Polygon = require 'obj/Polygon'
Area = require 'obj/Area'
function love.load()
---@type Room|nil
@ -48,9 +46,6 @@ end
function love.update(dt)
input:update(dt)
if current_room then current_room:update(dt) end
if input:pressed('f1') then gotoRoom('CircleRoom') end
if input:pressed('f2') then gotoRoom('RectangleRoom') end
if input:pressed('f3') then gotoRoom('PolygonRoom') end
end
function love.draw()
@ -103,3 +98,14 @@ function gotoRoom(room_type, ...)
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

35
obj/Area.lua Normal file
View file

@ -0,0 +1,35 @@
---@module 'obj/Area'
---@class Area:Object
---@field room Room
local Area = Object:extend()
---Instantiates area
function Area:new(room)
self.room = room
self.game_objects = {}
end
---Updates area
---@param dt number
function Area:update(dt)
for _, game_object in ipairs(self.game_objects) do game_object:update(dt) end
end
---Draws area
function Area:draw()
for _, game_object in ipairs(self.game_objects) do game_object:draw() end
end
---Adds game object to area
---@param game_object_type string name of game object's class
---@param x number|nil horizontal position
---@param y number|nil vertical position
---@param opts table|nil additional arguments
function Area:addGameObject(game_object_type, x, y, opts)
local game_object = _G[game_object_type](self, x or 0, y or 0, opts or {})
table.insert(self.game_objects, game_object)
return game_object
end
return Area

View file

@ -1,21 +0,0 @@
---@class Circle:Object circle object
---@field x number horizontal position of circle
---@field y number vertical position of circle
---@field radius number radius of circle
---@field mode string drawing mode of circle
local Circle = Object:extend()
function Circle:new(config)
self.x = config.x or 400
self.y = config.y or 300
self.radius = config.radius or 50
self.mode = config.mode or "fill"
end
function Circle:update(dt) end
function Circle:draw()
love.graphics.circle(self.mode, self.x, self.y, self.radius)
end
return Circle

31
obj/GameObject.lua Normal file
View file

@ -0,0 +1,31 @@
---@module 'obj/GameObject'
---@class GameObject:Object
---@field area number
---@field x number
---@field y number
---@field opts table|nil
local GameObject = Object:extend()
---Instantiates game object
function GameObject:new(area, x, y, opts)
if opts then for k,v in pairs(opts) do self[k] = v end end
self.area = area
self.x = x
self.y = y
self.id = UUID()
self.dead = false
self.timer = Timer()
end
---Updates game object
---@param dt number
function GameObject:update(dt)
if self.timer then self.timer:update(dt) end
end
---Draw game object
function GameObject:draw() end
return GameObject

View file

@ -1,17 +0,0 @@
---@class Polygon:Object Polygon object
---@field mode string drawing mode of Polygon
---@field points any points of Polygon
local Polygon = Object:extend()
function Polygon:new(mode, ...)
self.mode = mode or "fill"
self.points = {...} or {100,100, 200,100, 150,200}
end
function Polygon:update(dt) end
function Polygon:draw()
love.graphics.polygon(self.mode, self.points)
end
return Polygon

View file

@ -1,23 +0,0 @@
---@class Rectangle:Object Rectangle object
---@field x number horizontal position of Rectangle
---@field y number vertical position of Rectangle
---@field mode string drawing mode of Rectangle
---@field width number width of Rectangle
---@field height number height of Rectangle
local Rectangle = Object:extend()
function Rectangle:new(config)
self.x = config.x or 400
self.y = config.y or 300
self.mode = config.mode or "fill"
self.width = config.width or 100
self.height = config.height or 60
end
function Rectangle:update(dt) end
function Rectangle:draw()
love.graphics.rectangle(self.mode, self.x, self.y, self.width, self.height)
end
return Rectangle

View file

@ -1,29 +0,0 @@
---@class CircleRoom:Room
---@field circle Circle
---@field super Room
---@field radtween number
---@field timer Timer
local CircleRoom = Room:extend()
function CircleRoom:new()
CircleRoom.super.new(self)
self.radtween = 50
self.circle = Circle{radius = self.radtween}
self.timer = Timer()
end
function CircleRoom:update(dt)
CircleRoom.super.update(self, dt)
if input:pressed('action') then
self.timer:tween(1, self.circle, {radius = 100}, 'in-out-cubic')
end
self.circle:update(dt)
self.timer:update(dt)
end
function CircleRoom:draw()
CircleRoom.super.draw(self)
self.circle:draw()
end
return CircleRoom

View file

@ -1,27 +0,0 @@
---@class PolygonRoom:Room
---@field polygon Polygon
---@field super Room
---@field timer Timer
local PolygonRoom = Room:extend()
function PolygonRoom:new()
PolygonRoom.super.new(self)
self.polygon = Polygon('fill', 100,100, 200,100, 150,200)
self.timer = Timer()
end
function PolygonRoom:update(dt)
PolygonRoom.super.update(self, dt)
if input:pressed('action') then
self.polygon.mode = 'line'
end
self.polygon:update(dt)
self.timer:update(dt)
end
function PolygonRoom:draw()
PolygonRoom.super.draw(self)
self.polygon:draw()
end
return PolygonRoom

View file

@ -1,27 +0,0 @@
---@class RectangleRoom:Room
---@field rectangle Rectangle
---@field super Room
---@field timer Timer
local RectangleRoom = Room:extend()
function RectangleRoom:new()
RectangleRoom.super.new(self)
self.rectangle = Rectangle{}
self.timer = Timer()
end
function RectangleRoom:update(dt)
RectangleRoom.super.update(self, dt)
if input:pressed('action') then
self.timer:tween(1, self.rectangle, {width = 50}, 'in-out-cubic')
end
self.rectangle:update(dt)
self.timer:update(dt)
end
function RectangleRoom:draw()
RectangleRoom.super.draw(self)
self.rectangle:draw()
end
return RectangleRoom