Cleaver lua refactor
This commit is contained in:
parent
f55b1823f0
commit
e52e10c0c8
5 changed files with 100 additions and 67 deletions
BIN
assets/images/meats/chicken.png
Normal file
BIN
assets/images/meats/chicken.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 475 B |
BIN
assets/images/meats/cow.png
Normal file
BIN
assets/images/meats/cow.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 677 B |
BIN
assets/images/meats/pig.png
Normal file
BIN
assets/images/meats/pig.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 489 B |
79
cleaver.lua
Normal file
79
cleaver.lua
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
-- cleaver.lua
|
||||||
|
Cleaver = {}
|
||||||
|
Cleaver.__index = Cleaver
|
||||||
|
|
||||||
|
function Cleaver:new()
|
||||||
|
local instance = setmetatable({}, Cleaver)
|
||||||
|
instance.images = {}
|
||||||
|
instance.state = "idle"
|
||||||
|
instance.timer = 0
|
||||||
|
instance.scorePending = false
|
||||||
|
instance.score = 0
|
||||||
|
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
|
||||||
|
if self.scorePending then
|
||||||
|
self.score = self.score + 1
|
||||||
|
self.scorePending = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Cleaver:draw()
|
||||||
|
local x, y = love.mouse.getPosition()
|
||||||
|
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
|
||||||
|
self.scorePending = false
|
||||||
|
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
|
||||||
|
self.scorePending = true
|
||||||
|
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
|
||||||
76
main.lua
76
main.lua
|
|
@ -1,10 +1,9 @@
|
||||||
|
-- imports
|
||||||
|
local Cleaver = require("cleaver")
|
||||||
|
|
||||||
-- main.lua
|
-- main.lua
|
||||||
local score = 0
|
|
||||||
local font
|
local font
|
||||||
local cleaverImages = {}
|
local cleaver
|
||||||
local cleaverState = "idle"
|
|
||||||
local cleaverTimer = 0
|
|
||||||
local scorePending = false
|
|
||||||
local tileImage
|
local tileImage
|
||||||
local bottomBar
|
local bottomBar
|
||||||
local profitLossBar
|
local profitLossBar
|
||||||
|
|
@ -12,54 +11,27 @@ local profitLossBar
|
||||||
function love.load()
|
function love.load()
|
||||||
font = love.graphics.newFont("assets/fonts/Born2bSportyFS.otf", 32)
|
font = love.graphics.newFont("assets/fonts/Born2bSportyFS.otf", 32)
|
||||||
love.graphics.setFont(font)
|
love.graphics.setFont(font)
|
||||||
love.mouse.setVisible(false) -- Hide the default cursor
|
love.mouse.setVisible(false) -- Hide the default cursor
|
||||||
|
|
||||||
-- Load cleaver images
|
cleaver = Cleaver:new()
|
||||||
cleaverImages.idle = love.graphics.newImage("assets/images/cleaver/cleaver-idle.png")
|
cleaver:loadImages()
|
||||||
cleaverImages.windup = love.graphics.newImage("assets/images/cleaver/cleaver-windup.png")
|
|
||||||
cleaverImages.charged = love.graphics.newImage("assets/images/cleaver/cleaver-charged.png")
|
|
||||||
cleaverImages.chop = love.graphics.newImage("assets/images/cleaver/cleaver-chop.png")
|
|
||||||
|
|
||||||
-- Set initial cleaver image
|
|
||||||
cleaverCurrentImage = cleaverImages.idle
|
|
||||||
|
|
||||||
-- Load tile image
|
-- Load tile image
|
||||||
tileImage = love.graphics.newImage("assets/images/tiles/chop-floor-tile.png")
|
tileImage = love.graphics.newImage("assets/images/tiles/chop-floor-tile.png")
|
||||||
|
|
||||||
-- Load UI images for bottom bar and profit/loss bar
|
-- Load UI images for bottom bar and profit/loss bar
|
||||||
bottomBar = love.graphics.newImage("assets/images/ui/chopping-bottom-bar.png")
|
bottomBar = love.graphics.newImage("assets/images/ui/chopping-bottom-bar.png")
|
||||||
profitLossBar = love.graphics.newImage("assets/images/ui/chopping-profitloss-bar.png")
|
profitLossBar = love.graphics.newImage("assets/images/ui/chopping-profitloss-bar.png")
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
-- Update cleaver state
|
cleaver:update(dt)
|
||||||
if cleaverState == "windup" then
|
|
||||||
cleaverTimer = cleaverTimer + dt
|
|
||||||
if cleaverTimer >= 0.2 then
|
|
||||||
cleaverState = "charged"
|
|
||||||
cleaverCurrentImage = cleaverImages.charged
|
|
||||||
end
|
|
||||||
elseif cleaverState == "charged" then
|
|
||||||
-- Stay in charged state until mouse is released
|
|
||||||
elseif cleaverState == "chop" then
|
|
||||||
cleaverTimer = cleaverTimer + dt
|
|
||||||
if cleaverTimer >= 0.1 then
|
|
||||||
cleaverState = "idle"
|
|
||||||
cleaverCurrentImage = cleaverImages.idle
|
|
||||||
cleaverTimer = 0
|
|
||||||
if scorePending then
|
|
||||||
score = score + 1
|
|
||||||
scorePending = false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
-- Draw tile background
|
-- Draw tile background
|
||||||
local tileWidth = tileImage:getWidth()
|
local tileWidth = tileImage:getWidth()
|
||||||
local tileHeight = tileImage:getHeight()
|
local tileHeight = tileImage:getHeight()
|
||||||
local scale = 4 -- Adjust this value to change the tile size
|
local scale = 4 -- Adjust this value to change the tile size
|
||||||
for x = 0, love.graphics.getWidth(), tileWidth * scale do
|
for x = 0, love.graphics.getWidth(), tileWidth * scale do
|
||||||
for y = 0, love.graphics.getHeight(), tileHeight * scale do
|
for y = 0, love.graphics.getHeight(), tileHeight * scale do
|
||||||
love.graphics.draw(tileImage, x, y, 0, scale, scale)
|
love.graphics.draw(tileImage, x, y, 0, scale, scale)
|
||||||
|
|
@ -70,46 +42,28 @@ function love.draw()
|
||||||
local bottomBarX = (love.graphics.getWidth() - bottomBar:getWidth()) / 2
|
local bottomBarX = (love.graphics.getWidth() - bottomBar:getWidth()) / 2
|
||||||
local bottomBarY = love.graphics.getHeight() - bottomBar:getHeight()
|
local bottomBarY = love.graphics.getHeight() - bottomBar:getHeight()
|
||||||
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(score)
|
local scoreValue = "$" .. tostring(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
|
||||||
love.graphics.print(scoreText, bottomBarX + 20, bottomBarY + (bottomBar:getHeight() - font:getHeight()) / 2 + 15)
|
love.graphics.print(scoreText, bottomBarX + 20, bottomBarY + (bottomBar:getHeight() - font:getHeight()) / 2 + 15)
|
||||||
love.graphics.setColor(0, 0.5, 0)
|
love.graphics.setColor(0, 0.5, 0)
|
||||||
love.graphics.print(scoreValue, bottomBarX + 20 + scoreTextWidth, bottomBarY + (bottomBar:getHeight() - font:getHeight()) / 2 + 15)
|
love.graphics.print(scoreValue, bottomBarX + 20 + scoreTextWidth, bottomBarY + (bottomBar:getHeight() - font:getHeight()) / 2 + 15)
|
||||||
love.graphics.setColor(1, 1, 1) -- Reset to white
|
love.graphics.setColor(1, 1, 1) -- Reset to white
|
||||||
|
|
||||||
-- 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)
|
||||||
|
|
||||||
-- Draw cleaver
|
-- Draw cleaver
|
||||||
local x, y = love.mouse.getPosition()
|
cleaver:draw()
|
||||||
love.graphics.draw(cleaverCurrentImage, x - cleaverCurrentImage:getWidth() / 2, y - cleaverCurrentImage:getHeight() / 2)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.mousepressed(x, y, button)
|
function love.mousepressed(x, y, button)
|
||||||
if button == 1 then -- Left mouse button
|
cleaver:mousepressed(button)
|
||||||
cleaverState = "windup"
|
|
||||||
cleaverCurrentImage = cleaverImages.windup
|
|
||||||
cleaverTimer = 0
|
|
||||||
scorePending = false
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.mousereleased(x, y, button)
|
function love.mousereleased(x, y, button)
|
||||||
if button == 1 then -- Left mouse button
|
cleaver:mousereleased(button)
|
||||||
if cleaverState == "charged" then
|
|
||||||
cleaverState = "chop"
|
|
||||||
cleaverCurrentImage = cleaverImages.chop
|
|
||||||
cleaverTimer = 0
|
|
||||||
scorePending = true
|
|
||||||
elseif cleaverState == "windup" then
|
|
||||||
cleaverState = "idle"
|
|
||||||
cleaverCurrentImage = cleaverImages.idle
|
|
||||||
cleaverTimer = 0
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
Loading…
Add table
Add a link
Reference in a new issue