Context: I like to organize my assets into their own file. Such as HUD assets go into the HUD file. The HUD file contains things like fuel gauge, health gauge and stuff. They to our their own file and are imported into HUD. This way on my main.lua file I can just do import HUD and it brings everything with it.
The Problem: I want to pass information back and forth between files but they're not working.
As in the HUD class, I have fuel which uses draw something like fuel:draw() it only draws once. What I want is for my car class which has like car.fueltank to pass it's fuel to the HUD so it can show the remaining fuel on screen.
I like the simplicity of this setup
import "coreLibs/object"
import "coreLibs/graphics"
import "coreLibs/sprites"
local pd <const> = playdate
local gfx <const> = pd.graphics
class('Fuel').extends(playdate.graphics.sprite)
local fuel = Fuel()
fuel:setZIndex(1000)
fuel:setSize(100, 50)
fuel:setCollideRect(0,0,100,20)
fuel:moveTo(100, 100)
fuel:addSprite()
fuel.percentage = 100
local Test = 10000
function fuel:draw(p)
Test-=1
print("Fuel draw ",tst)
local cx, cy, width, height = self:getCollideBounds()
gfx.setColor(playdate.graphics.kColorBlack)
gfx.fillRect(cx, cy, Test, height)
gfx.drawText("Fuel",cx,cy)
end
I really like this way because it's easy and works very well. But I don't know how to update the width of the fuel gauge, seems to draw once then it's done.
If I do something like this in my main.lua file
local function initalize()
car = Car(277,0,1,1)
car ()
local backgroundImage = gfx.image.new("images/background")
gfx.sprite.setBackgroundDrawingCallback(
function(x,y,width,height)
gfx.setClipRect(x,y,width,height)
backgroundImage:draw(0,0)
gfx.clearClipRect()
gfx.fillRect(5, 210, car:getRemainingFuel(), 25)
gfx.setImageDrawMode(playdate.graphics.kColorXOR)
gfx.drawText("Fuel",50,215)
end
)
end
It' works well, but it removes my background (because in my car class I'm doing gfx.sprite.redrawBackground() and I guess the setBackgroundDrawingCallback doesn't like that). It also seems way to complicated. I'm going to have a few more assets on screen and hate to have to continue this messy code.
I've tried putting the gfx.fillRect directly inside the playdate:update, but it doesn't seem to work.
My Inital thought on how it should work is.
- I have my Car class
-- car class holds properties like : gassTank,efficiency,remaining gass - On button press or something fuel is taken out of the tank
- On Main which imports Hud I could pass car.remainingFuel to Hud HUD then passes that to fuel. Then when fuel updates via draw or playdate.Update the UI updates and I see it on screen. Yet I can't seem to get that right without over complicating it like above.
I COULD just write everything inside of main, but that gets real messy real quick. I'd prefer to just keep everything isolated.
How do some of the rest of you manage your files? Do you typically use draw and pickup updated properties a different way? or use a sprite class and use playdate.update?