creating cursor class pt 1
This commit is contained in:
parent
409800a54f
commit
620caa0ec7
6 changed files with 83 additions and 26 deletions
26
main.lua
26
main.lua
|
|
@ -1,11 +1,10 @@
|
||||||
-- imports
|
-- imports
|
||||||
local Cleaver = require("cleaver")
|
local MeatFactory = require("src/meat_factory")
|
||||||
local MeatFactory = require("meat_factory")
|
local MeatTable = require("src/meat_tables")
|
||||||
local MeatTable = require("meat_tables")
|
local Cursor = require("src/cursor")
|
||||||
|
|
||||||
-- main.lua
|
-- main.lua
|
||||||
local font
|
local font
|
||||||
local cleaver
|
|
||||||
local tileImage
|
local tileImage
|
||||||
local bottomBar
|
local bottomBar
|
||||||
local profitLossBar
|
local profitLossBar
|
||||||
|
|
@ -16,8 +15,6 @@ function love.load()
|
||||||
love.graphics.setFont(font)
|
love.graphics.setFont(font)
|
||||||
love.mouse.setVisible(false) -- Hide the default cursor
|
love.mouse.setVisible(false) -- Hide the default cursor
|
||||||
|
|
||||||
cleaver = Cleaver:new()
|
|
||||||
cleaver:loadImages()
|
|
||||||
|
|
||||||
-- Load tile image
|
-- Load tile image
|
||||||
tileImage = love.graphics.newImage("assets/images/tiles/tiles_bigger_new.png")
|
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")
|
profitLossBar = love.graphics.newImage("assets/images/ui/chopping-profitloss-bar.png")
|
||||||
|
|
||||||
-- Load basic cursor
|
-- Load basic cursor
|
||||||
cursor = love.graphics.newImage("assets/images/ui/cursor.png")
|
cursor = Cursor:new()
|
||||||
|
|
||||||
-- Load MeatTable
|
-- Load MeatTable
|
||||||
meatTable = MeatTable:new()
|
meatTable = MeatTable:new()
|
||||||
|
|
@ -42,7 +39,7 @@ function love.load()
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
cleaver:update(dt)
|
cursor:update(dt)
|
||||||
meatFactory:update(dt)
|
meatFactory:update(dt)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -72,7 +69,7 @@ function love.draw()
|
||||||
love.graphics.draw(bottomBar, bottomBarX, bottomBarY)
|
love.graphics.draw(bottomBar, bottomBarX, bottomBarY)
|
||||||
-- Draw score on bottom bar
|
-- Draw score on bottom bar
|
||||||
local scoreText = "Net Worth: "
|
local scoreText = "Net Worth: "
|
||||||
local scoreValue = "$" .. tostring(cleaver:getScore())
|
local scoreValue = "$" .. tostring(cursor.cleaver:getScore())
|
||||||
local scoreTextWidth = font:getWidth(scoreText)
|
local scoreTextWidth = font:getWidth(scoreText)
|
||||||
local scoreValueWidth = font:getWidth(scoreValue)
|
local scoreValueWidth = font:getWidth(scoreValue)
|
||||||
love.graphics.setColor(0, 0, 0) -- Black
|
love.graphics.setColor(0, 0, 0) -- Black
|
||||||
|
|
@ -84,19 +81,16 @@ function love.draw()
|
||||||
-- Draw profit/loss bar at top right
|
-- Draw profit/loss bar at top right
|
||||||
love.graphics.draw(profitLossBar, love.graphics.getWidth() - profitLossBar:getWidth() - 10, 10)
|
love.graphics.draw(profitLossBar, love.graphics.getWidth() - profitLossBar:getWidth() - 10, 10)
|
||||||
|
|
||||||
local mouse_x, mouse_y = love.mouse.getPosition()
|
-- Draw cursor
|
||||||
love.graphics.draw(cursor, mouse_x - cursor:getWidth() / 2, mouse_y - cursor:getHeight() / 2)
|
cursor:draw()
|
||||||
|
|
||||||
-- Draw cleaver
|
|
||||||
cleaver:draw()
|
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.mousepressed(x, y, button)
|
function love.mousepressed(x, y, button)
|
||||||
cleaver:mousepressed(button)
|
cursor:mousepressed(button)
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.mousereleased(x, y, button)
|
function love.mousereleased(x, y, button)
|
||||||
cleaver:mousereleased(button)
|
cursor:mousereleased(button)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,11 @@ Cleaver.__index = Cleaver
|
||||||
|
|
||||||
function Cleaver:new()
|
function Cleaver:new()
|
||||||
local instance = setmetatable({}, Cleaver)
|
local instance = setmetatable({}, Cleaver)
|
||||||
instance.images = {}
|
|
||||||
instance.state = "idle"
|
instance.state = "idle"
|
||||||
instance.timer = 0
|
instance.timer = 0
|
||||||
instance.score = 0
|
instance.score = 0
|
||||||
|
instance.images = {}
|
||||||
|
Cleaver.loadImages(instance)
|
||||||
return instance
|
return instance
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -38,15 +39,11 @@ function Cleaver:update(dt)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Cleaver:draw()
|
function Cleaver:draw(x,y)
|
||||||
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)
|
love.graphics.draw(self.currentImage, x - self.currentImage:getWidth() / 2, y - self.currentImage:getHeight() / 2)
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function Cleaver:mousepressed(button)
|
function Cleaver:mousepressed(button)
|
||||||
if button == 1 then -- Left mouse button
|
if button == 1 then -- Left mouse button
|
||||||
self.state = "windup"
|
self.state = "windup"
|
||||||
|
|
@ -79,6 +76,7 @@ function Cleaver:mousereleased(button)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function Cleaver:getScore()
|
function Cleaver:getScore()
|
||||||
return self.score
|
return self.score
|
||||||
end
|
end
|
||||||
65
src/cursor.lua
Normal file
65
src/cursor.lua
Normal 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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
-- meat-factory.lua
|
-- meat-factory.lua
|
||||||
MeatFactory = {}
|
MeatFactory = {}
|
||||||
MeatFactory.__index = MeatFactory
|
MeatFactory.__index = MeatFactory
|
||||||
local Meat = require("meat")
|
local Meat = require("src/meat")
|
||||||
|
|
||||||
|
|
||||||
function MeatFactory:new(spawnRate, meatTypes, meatTable)
|
function MeatFactory:new(spawnRate, meatTypes, meatTable)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue