colored circles program

This commit is contained in:
yuki 2025-11-10 02:52:20 -03:00
parent abcd5371d8
commit 40fe3c84cf
6 changed files with 110 additions and 6 deletions

View file

@ -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()

View file

@ -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

View file

@ -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

View file

@ -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

36
obj/game/Circle.lua Normal file
View file

@ -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

27
rooms/Stage.lua Normal file
View file

@ -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