From 620caa0ec7f6ac9ce4dd863c0358b31094de9951 Mon Sep 17 00:00:00 2001 From: Juan Date: Thu, 7 Aug 2025 16:13:31 -0400 Subject: [PATCH] creating cursor class pt 1 --- main.lua | 26 ++++------ cleaver.lua => src/cleaver.lua | 16 +++--- src/cursor.lua | 65 ++++++++++++++++++++++++ meat.lua => src/meat.lua | 0 meat_factory.lua => src/meat_factory.lua | 2 +- meat_tables.lua => src/meat_tables.lua | 0 6 files changed, 83 insertions(+), 26 deletions(-) rename cleaver.lua => src/cleaver.lua (88%) create mode 100644 src/cursor.lua rename meat.lua => src/meat.lua (100%) rename meat_factory.lua => src/meat_factory.lua (97%) rename meat_tables.lua => src/meat_tables.lua (100%) diff --git a/main.lua b/main.lua index f1cf0bd..c694b49 100644 --- a/main.lua +++ b/main.lua @@ -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 @@ -83,20 +80,17 @@ 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 diff --git a/cleaver.lua b/src/cleaver.lua similarity index 88% rename from cleaver.lua rename to src/cleaver.lua index 2857be2..7ff586b 100644 --- a/cleaver.lua +++ b/src/cleaver.lua @@ -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,15 +39,11 @@ 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 - love.graphics.draw(self.currentImage, x - self.currentImage:getWidth() / 2, y - self.currentImage:getHeight() / 2) - end - end +function Cleaver:draw(x,y) + love.graphics.draw(self.currentImage, x - self.currentImage:getWidth() / 2, y - self.currentImage:getHeight() / 2) end + function Cleaver:mousepressed(button) if button == 1 then -- Left mouse button self.state = "windup" @@ -79,8 +76,9 @@ function Cleaver:mousereleased(button) end end + function Cleaver:getScore() return self.score end -return Cleaver +return Cleaver \ No newline at end of file diff --git a/src/cursor.lua b/src/cursor.lua new file mode 100644 index 0000000..1549c5e --- /dev/null +++ b/src/cursor.lua @@ -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 + + + + diff --git a/meat.lua b/src/meat.lua similarity index 100% rename from meat.lua rename to src/meat.lua diff --git a/meat_factory.lua b/src/meat_factory.lua similarity index 97% rename from meat_factory.lua rename to src/meat_factory.lua index 7e90866..d67c817 100644 --- a/meat_factory.lua +++ b/src/meat_factory.lua @@ -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) diff --git a/meat_tables.lua b/src/meat_tables.lua similarity index 100% rename from meat_tables.lua rename to src/meat_tables.lua