creating cursor class pt 1

This commit is contained in:
Juan 2025-08-07 16:13:31 -04:00
parent 409800a54f
commit 620caa0ec7
6 changed files with 83 additions and 26 deletions

View file

@ -1,11 +1,10 @@
-- imports
local Cleaver = require("cleaver")
local MeatFactory = require("meat_factory")
local MeatTable = require("meat_tables")
local MeatFactory = require("src/meat_factory")
local MeatTable = require("src/meat_tables")
local Cursor = require("src/cursor")
-- main.lua
local font
local cleaver
local tileImage
local bottomBar
local profitLossBar
@ -16,8 +15,6 @@ function love.load()
love.graphics.setFont(font)
love.mouse.setVisible(false) -- Hide the default cursor
cleaver = Cleaver:new()
cleaver:loadImages()
-- Load tile image
tileImage = love.graphics.newImage("assets/images/tiles/tiles_bigger_new.png")
@ -26,7 +23,7 @@ function love.load()
profitLossBar = love.graphics.newImage("assets/images/ui/chopping-profitloss-bar.png")
-- Load basic cursor
cursor = love.graphics.newImage("assets/images/ui/cursor.png")
cursor = Cursor:new()
-- Load MeatTable
meatTable = MeatTable:new()
@ -42,7 +39,7 @@ function love.load()
end
function love.update(dt)
cleaver:update(dt)
cursor:update(dt)
meatFactory:update(dt)
end
@ -72,7 +69,7 @@ function love.draw()
love.graphics.draw(bottomBar, bottomBarX, bottomBarY)
-- Draw score on bottom bar
local scoreText = "Net Worth: "
local scoreValue = "$" .. tostring(cleaver:getScore())
local scoreValue = "$" .. tostring(cursor.cleaver:getScore())
local scoreTextWidth = font:getWidth(scoreText)
local scoreValueWidth = font:getWidth(scoreValue)
love.graphics.setColor(0, 0, 0) -- Black
@ -84,19 +81,16 @@ function love.draw()
-- Draw profit/loss bar at top right
love.graphics.draw(profitLossBar, love.graphics.getWidth() - profitLossBar:getWidth() - 10, 10)
local mouse_x, mouse_y = love.mouse.getPosition()
love.graphics.draw(cursor, mouse_x - cursor:getWidth() / 2, mouse_y - cursor:getHeight() / 2)
-- Draw cleaver
cleaver:draw()
-- Draw cursor
cursor:draw()
end
function love.mousepressed(x, y, button)
cleaver:mousepressed(button)
cursor:mousepressed(button)
end
function love.mousereleased(x, y, button)
cleaver:mousereleased(button)
cursor:mousereleased(button)
end

View file

@ -4,10 +4,11 @@ Cleaver.__index = Cleaver
function Cleaver:new()
local instance = setmetatable({}, Cleaver)
instance.images = {}
instance.state = "idle"
instance.timer = 0
instance.score = 0
instance.images = {}
Cleaver.loadImages(instance)
return instance
end
@ -38,14 +39,10 @@ function Cleaver:update(dt)
end
end
function Cleaver:draw()
local x, y = love.mouse.getPosition()
for _, meatTable in ipairs(instances.meat_tables) do
if meatTable:isHovering(x,y) then
function Cleaver:draw(x,y)
love.graphics.draw(self.currentImage, x - self.currentImage:getWidth() / 2, y - self.currentImage:getHeight() / 2)
end
end
end
function Cleaver:mousepressed(button)
if button == 1 then -- Left mouse button
@ -79,6 +76,7 @@ function Cleaver:mousereleased(button)
end
end
function Cleaver:getScore()
return self.score
end

65
src/cursor.lua Normal file
View file

@ -0,0 +1,65 @@
--cursor.lua
Cursor = {}
Cursor.__index = Cursor
local Cleaver = require("src/cleaver")
function Cursor:new()
local instance = setmetatable({}, Cursor)
instance.state = "idle"
instance.states = {"idle", "cleaver"}
instance.cleaver = Cleaver:new()
instance.idleCursor = love.graphics.newImage("assets/images/ui/cursor.png")
return instance
end
function Cursor:update(dt)
self.x, self.y = love.mouse.getPosition()
local isAboveMeatTable = false
-- Check if cursor is above any meat table
for _, meatTable in ipairs(instances.meat_tables) do
if meatTable:isHovering(self.x, self.y) then
isAboveMeatTable = true
break
end
end
-- Update the cleaver state or cursor state accordingly
if isAboveMeatTable then
self.state = "cleaver"
else
self.state = "idle"
end
-- You can also update the cleaver's state or position here if needed
self.cleaver:update(dt) -- Assuming Cleaver has an update function
end
function Cursor:draw()
if self.state == "cleaver" then
self.cleaver:draw(self.x, self.y)
elseif self.state == "idle" then
love.graphics.draw(self.idleCursor, self.x - self.idleCursor:getWidth() / 2, self.y - self.idleCursor:getHeight() / 2)
end
end
function Cursor:mousepressed(button)
if self.state == "cleaver" then
self.cleaver:mousepressed(button)
end
end
function Cursor:mousereleased(button)
if self.state == "cleaver" then
self.cleaver:mousereleased(button)
end
end
return Cursor

View file

@ -1,7 +1,7 @@
-- meat-factory.lua
MeatFactory = {}
MeatFactory.__index = MeatFactory
local Meat = require("meat")
local Meat = require("src/meat")
function MeatFactory:new(spawnRate, meatTypes, meatTable)