I'm trying to figure out what the most basic approach would be to effectively (or at least more effectively than what I have) create and manage objects. I know I could store them in a table when I create them which is what I currently do, but I'm not sure of a way to do it that would not be clunky to work with.
I don't have that much code to share, but here's a simple item class I made I'm trying to make use of composition although I still ended up using inheritance for the BaseItem.
function TestItem.new(x,y,w,h)
local self = setmetatable(BaseItem.new(),TestItem)
self.render = Render.new(x,y,w,h,Enum.GraphicType.Rectangle,{1,1,1})
self.playerIsNear = false
grid:insert({x = x, y = y, width = w, height = h}, self)
return self
end
And here's the main code that creates the objects.
function love.load()
local screenWidth, screenHeight = love.graphics.getDimensions()
grid = SpatialGrid.new(screenWidth, screenHeight, 70)
player = Player.new(10,10,40,40)
allObjects[1] = TestItem.new(300,50,20,20)
allObjects[2] = TestItem.new(800,250,20,20)
allObjects[3] = TestItem.new(600,300,20,20)
allObjects[4] = TestItem.new(900,600,20,20)
end
function love.update(dt)
player:update(dt)
for _,object in ipairs(allObjects) do
object:update(dt)
end
end
function love.draw()
player:draw()
for _,object in ipairs(allObjects) do
object:draw(player.input) -- the item objects need the players input component so they can display the interaction key
end
end
{{300,50,20,20}, {800,250,20,20},...}\$\endgroup\$