creating cursor class pt 1
This commit is contained in:
parent
409800a54f
commit
620caa0ec7
6 changed files with 83 additions and 26 deletions
84
src/cleaver.lua
Normal file
84
src/cleaver.lua
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
-- cleaver.lua
|
||||
Cleaver = {}
|
||||
Cleaver.__index = Cleaver
|
||||
|
||||
function Cleaver:new()
|
||||
local instance = setmetatable({}, Cleaver)
|
||||
instance.state = "idle"
|
||||
instance.timer = 0
|
||||
instance.score = 0
|
||||
instance.images = {}
|
||||
Cleaver.loadImages(instance)
|
||||
return instance
|
||||
end
|
||||
|
||||
function Cleaver:loadImages()
|
||||
self.images.idle = love.graphics.newImage("assets/images/cleaver/cleaver-idle.png")
|
||||
self.images.windup = love.graphics.newImage("assets/images/cleaver/cleaver-windup.png")
|
||||
self.images.charged = love.graphics.newImage("assets/images/cleaver/cleaver-charged.png")
|
||||
self.images.chop = love.graphics.newImage("assets/images/cleaver/cleaver-chop.png")
|
||||
self.currentImage = self.images.idle
|
||||
end
|
||||
|
||||
function Cleaver:update(dt)
|
||||
if self.state == "windup" then
|
||||
self.timer = self.timer + dt
|
||||
if self.timer >= 0.2 then
|
||||
self.state = "charged"
|
||||
self.currentImage = self.images.charged
|
||||
end
|
||||
elseif self.state == "charged" then
|
||||
-- Stay in charged state until mouse is released
|
||||
elseif self.state == "chop" then
|
||||
self.timer = self.timer + dt
|
||||
if self.timer >= 0.1 then
|
||||
self.state = "idle"
|
||||
self.currentImage = self.images.idle
|
||||
self.timer = 0
|
||||
end
|
||||
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"
|
||||
self.currentImage = self.images.windup
|
||||
self.timer = 0
|
||||
end
|
||||
end
|
||||
|
||||
function Cleaver:mousereleased(button)
|
||||
if button == 1 then -- Left mouse button
|
||||
if self.state == "charged" then
|
||||
self.state = "chop"
|
||||
self.currentImage = self.images.chop
|
||||
self.timer = 0
|
||||
-- Check if the cleaver is hovering over a meat instance
|
||||
local x, y = love.mouse.getPosition()
|
||||
for i, meat in ipairs(instances.meats) do
|
||||
if x >= meat.x and x <= meat.x + meat.currentImage:getWidth() and y >= meat.y and y <= meat.y + meat.currentImage:getHeight() then
|
||||
-- Remove the meat instance from the table
|
||||
table.remove(instances.meats, i)
|
||||
self.score = self.score + 1
|
||||
break
|
||||
end
|
||||
end
|
||||
elseif self.state == "windup" then
|
||||
self.state = "idle"
|
||||
self.currentImage = self.images.idle
|
||||
self.timer = 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function Cleaver:getScore()
|
||||
return self.score
|
||||
end
|
||||
|
||||
return Cleaver
|
||||
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
|
||||
|
||||
|
||||
|
||||
|
||||
21
src/meat.lua
Normal file
21
src/meat.lua
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
-- meat.lua
|
||||
Meat = {}
|
||||
Meat.__index = Meat
|
||||
|
||||
function Meat:new()
|
||||
local instance = setmetatable({}, Meat)
|
||||
instance.meatType = ""
|
||||
instance.images = {}
|
||||
instance.scorePending = false
|
||||
instance.value = 1
|
||||
return instance
|
||||
end
|
||||
|
||||
function Meat:loadImages()
|
||||
self.images.chicken = love.graphics.newImage("assets/images/meats/chicken.png")
|
||||
self.images.pig = love.graphics.newImage("assets/images/meats/pig.png")
|
||||
self.images.cow = love.graphics.newImage("assets/images/meats/cow.png")
|
||||
self.currentImage = self.images.chicken
|
||||
end
|
||||
|
||||
return Meat
|
||||
52
src/meat_factory.lua
Normal file
52
src/meat_factory.lua
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
-- meat-factory.lua
|
||||
MeatFactory = {}
|
||||
MeatFactory.__index = MeatFactory
|
||||
local Meat = require("src/meat")
|
||||
|
||||
|
||||
function MeatFactory:new(spawnRate, meatTypes, meatTable)
|
||||
local instance = setmetatable({}, MeatFactory)
|
||||
instance.spawnRate = spawnRate
|
||||
instance.meatTypes = meatTypes
|
||||
instance.meatTable = meatTable
|
||||
instance.spawnTimer = 0
|
||||
return instance
|
||||
end
|
||||
|
||||
function MeatFactory:update(dt)
|
||||
self.spawnTimer = self.spawnTimer + dt
|
||||
if self.spawnTimer >= self.spawnRate then
|
||||
self:spawnMeat()
|
||||
self.spawnTimer = 0
|
||||
end
|
||||
end
|
||||
|
||||
function MeatFactory:spawnMeat()
|
||||
local meatType = self.meatTypes[math.random(#self.meatTypes)]
|
||||
local meat = Meat:new()
|
||||
meat:loadImages() -- Load images for the meat
|
||||
meat.meatType = meatType
|
||||
meat.currentImage = meat.images[meatType]
|
||||
meat.value = self:getMeatValue(meatType)
|
||||
-- Set the x and y coordinates to be within the bounds of the meat table
|
||||
local meatTableX = (love.graphics.getWidth() - self.meatTable.image:getWidth()) / 2
|
||||
local meatTableY = (love.graphics.getHeight() - self.meatTable.image:getHeight()) / 2
|
||||
meat.x = meatTableX + (self.meatTable.image:getWidth() - meat.currentImage:getWidth()) - 15
|
||||
meat.y = meatTableY + (self.meatTable.image:getHeight() - meat.currentImage:getHeight()) - 30
|
||||
table.insert(instances.meats, meat)
|
||||
|
||||
end
|
||||
|
||||
function MeatFactory:getMeatValue(meatType)
|
||||
-- You can implement a logic to determine the value of the meat based on its type
|
||||
-- For example:
|
||||
if meatType == "chicken" then
|
||||
return 10
|
||||
elseif meatType == "cow" then
|
||||
return 20
|
||||
elseif meatType == "pig" then
|
||||
return 15
|
||||
end
|
||||
end
|
||||
|
||||
return MeatFactory
|
||||
26
src/meat_tables.lua
Normal file
26
src/meat_tables.lua
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
MeatTable = {}
|
||||
MeatTable.__index = MeatTable
|
||||
|
||||
function MeatTable:new()
|
||||
local instance = setmetatable({}, MeatTable)
|
||||
instance.image = love.graphics.newImage("assets/images/env/newer_table.png")
|
||||
instance.x = (love.graphics.getWidth() - instance.image:getWidth()) / 2;
|
||||
instance.y = (love.graphics.getHeight() - instance.image:getHeight()) / 2;
|
||||
return instance
|
||||
end
|
||||
|
||||
function MeatTable:draw()
|
||||
local x = self.x
|
||||
local image = self.image
|
||||
local y = self.y
|
||||
love.graphics.draw(image, x, y);
|
||||
end
|
||||
|
||||
function MeatTable:isHovering(x, y)
|
||||
return x >= self.x and x <= self.x + self.image:getWidth() and y >= self.y and y <= self.y + self.image:getHeight()
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
return MeatTable
|
||||
Loading…
Add table
Add a link
Reference in a new issue