diff --git a/main.lua b/main.lua index 7a1d0d7..942aef8 100644 --- a/main.lua +++ b/main.lua @@ -12,9 +12,13 @@ Baton = require 'lib/baton/baton' ---@type Timer Timer = require 'lib/hump/timer' --- objects -- +-- generic objects -- Room = require 'obj/Room' Area = require 'obj/Area' +GameObject = require 'obj/GameObject' + +-- game objects -- +Circle = require 'obj/game/Circle' function love.load() ---@type Room|nil @@ -46,6 +50,7 @@ end function love.update(dt) input:update(dt) if current_room then current_room:update(dt) end + if input:pressed('f1') then gotoRoom('Stage') end end function love.draw() diff --git a/obj/Area.lua b/obj/Area.lua index c9919b8..8aadd3d 100644 --- a/obj/Area.lua +++ b/obj/Area.lua @@ -2,6 +2,7 @@ ---@class Area:Object ---@field room Room +---@field game_objects GameObject[] local Area = Object:extend() ---Instantiates area @@ -13,7 +14,10 @@ end ---Updates area ---@param dt number function Area:update(dt) - for _, game_object in ipairs(self.game_objects) do game_object:update(dt) end + for i, game_object in ipairs(self.game_objects) do + if game_object:isDead() then table.remove(self.game_objects, i) end + game_object:update(dt) + end end ---Draws area diff --git a/obj/GameObject.lua b/obj/GameObject.lua index 4d314f3..dc5622c 100644 --- a/obj/GameObject.lua +++ b/obj/GameObject.lua @@ -1,10 +1,11 @@ ---@module 'obj/GameObject' ---@class GameObject:Object ----@field area number +---@field area Area ---@field x number ---@field y number ---@field opts table|nil +---@field super GameObject local GameObject = Object:extend() ---Instantiates game object @@ -28,4 +29,25 @@ end ---Draw game object function GameObject:draw() end +---Mark object for destruction +function GameObject:destroy() + if self.timer then + self.timer:clear() -- cancel all tweens/after/every + self.timer = nil + end + self.dead = true +end + +---Returns whether object is marked for destruction +---@return boolean +function GameObject:isDead() + return self.dead == true +end + +---Kill object (meant to be overridden) +---**default behavior:** self:destroy() +function GameObject:kill() + self:destroy() +end + return GameObject diff --git a/obj/Room.lua b/obj/Room.lua index 9afa923..71ea83f 100644 --- a/obj/Room.lua +++ b/obj/Room.lua @@ -1,16 +1,26 @@ ---@module 'obj/Room' ---@class Room:Object +---@field area Area room area +---@field id string room id +---@field super Room local Room = Object:extend() ---Instantiates room. -function Room:new() end +function Room:new() + self.area = Area(self) + self.id = UUID() +end ---Updates room (see [love.update()](lua://love.update)) ---@param dt number delta time -function Room:update(dt) end +function Room:update(dt) + self.area:update(dt) +end ---Draws graphics (see [love.draw()](lua://love.draw)) -function Room:draw() end +function Room:draw() + self.area:draw() +end return Room diff --git a/obj/game/Circle.lua b/obj/game/Circle.lua new file mode 100644 index 0000000..5b998bd --- /dev/null +++ b/obj/game/Circle.lua @@ -0,0 +1,36 @@ +---@class Circle:GameObject circle object +---@field area Area area of circle +---@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 = GameObject:extend() + +function Circle:new(area, x, y, config) + Circle.super.new(self, area, x or 400, y or 300) + self.tweened = false + self.current_radius = 0 + self.radius = config.radius or 50 + self.mode = config.mode or "fill" + self._r, self._g, self._b = love.math.random(0.1, 1.0), love.math.random(0.1, 1.0), love.math.random(0.1, 1.0) +end + +function Circle:update(dt) + Circle.super.update(self, dt) + if not self.tweened then + self.tweened = true + self.timer:tween(0.4, self, {current_radius = self.radius}, 'in-out-cubic') + end +end + +function Circle:draw() + Circle.super.draw(self) + love.graphics.setColor(self._r, self._g, self._b) + love.graphics.circle(self.mode, self.x, self.y, self.current_radius) +end + +function Circle:kill() + self.timer:tween(0.4, self, {current_radius = 0}, 'in-out-cubic', function() self:destroy() end) +end + +return Circle diff --git a/rooms/Stage.lua b/rooms/Stage.lua new file mode 100644 index 0000000..1b07d92 --- /dev/null +++ b/rooms/Stage.lua @@ -0,0 +1,27 @@ +---@class Stage:Room +local Stage = Room:extend() + +function Stage:new() + Stage.super.new(self) + self.timer = Timer() +end + +function Stage:update(dt) + Stage.super.update(self, dt) + + if input:pressed('action') then + local latest_object = self.area:addGameObject('Circle', love.math.random(1, 800), love.math.random(1, 600)) + end + + if input:pressed('right') then + for _, game_object in ipairs(self.area.game_objects) do + game_object:kill() + end + end +end + +function Stage:draw() + Stage.super.draw(self) +end + +return Stage