diff --git a/main.lua b/main.lua index 13a5ec2..7a1d0d7 100644 --- a/main.lua +++ b/main.lua @@ -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 diff --git a/obj/Area.lua b/obj/Area.lua new file mode 100644 index 0000000..c9919b8 --- /dev/null +++ b/obj/Area.lua @@ -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 diff --git a/obj/Circle.lua b/obj/Circle.lua deleted file mode 100644 index dc8c91a..0000000 --- a/obj/Circle.lua +++ /dev/null @@ -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 diff --git a/obj/GameObject.lua b/obj/GameObject.lua new file mode 100644 index 0000000..4d314f3 --- /dev/null +++ b/obj/GameObject.lua @@ -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 diff --git a/obj/Polygon.lua b/obj/Polygon.lua deleted file mode 100644 index cd748ce..0000000 --- a/obj/Polygon.lua +++ /dev/null @@ -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 diff --git a/obj/Rectangle.lua b/obj/Rectangle.lua deleted file mode 100644 index 85b81b6..0000000 --- a/obj/Rectangle.lua +++ /dev/null @@ -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 diff --git a/rooms/CircleRoom.lua b/rooms/CircleRoom.lua deleted file mode 100644 index 352f5c6..0000000 --- a/rooms/CircleRoom.lua +++ /dev/null @@ -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 diff --git a/rooms/PolygonRoom.lua b/rooms/PolygonRoom.lua deleted file mode 100644 index 1b7f6d1..0000000 --- a/rooms/PolygonRoom.lua +++ /dev/null @@ -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 diff --git a/rooms/RectangleRoom.lua b/rooms/RectangleRoom.lua deleted file mode 100644 index 215288c..0000000 --- a/rooms/RectangleRoom.lua +++ /dev/null @@ -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