From 737ae2efe040dde1da59812d3ac3f422af3852dd Mon Sep 17 00:00:00 2001 From: jill Date: Sat, 23 Jan 2021 17:22:14 +0300 Subject: [PATCH] more button functionality --- constructors.lua | 2 +- main.lua | 115 ++++++++++++++++++++++++++++------------------- 2 files changed, 70 insertions(+), 47 deletions(-) diff --git a/constructors.lua b/constructors.lua index 223cd78..25dd5ef 100644 --- a/constructors.lua +++ b/constructors.lua @@ -52,7 +52,7 @@ function self.food(x, y, type) speed = 0.2, time = math.random(), deathtimer = 0, - type = 1 + type = type } return food diff --git a/main.lua b/main.lua index e93e414..1506a95 100644 --- a/main.lua +++ b/main.lua @@ -17,6 +17,9 @@ debug = false food = {} feesh = {} +foodtier = 1 +foodcount = 1 + footerbuttons = { { cost = 100, @@ -30,21 +33,33 @@ footerbuttons = { { cost = 200, sprite = 'food', - tier = 1, + tier = foodtier, openanim = 1, open = true, - func = function() - --table.insert(feesh, constr.fish(math.random(), 0.3)) + func = function(self) + self.openanim = 0 + foodtier = foodtier + 1 + if foodtier >= 3 then + self.open = false + else + self.tier = foodtier + end end }, { cost = 300, sprite = 'foodcount', - tier = 3, + tier = foodcount, openanim = 1, open = true, - func = function() - --table.insert(feesh, constr.fish(math.random(), 0.3)) + func = function(self) + self.openanim = 0 + foodcount = foodcount + 1 + if foodcount >= 9 then + self.open = false + else + self.tier = foodcount + end end } } @@ -105,6 +120,12 @@ function love.update(dt) bench.update() bench.startBenchmark('update') + bench.startBenchmark('update_buttons') + for _,btn in ipairs(footerbuttons) do + btn.openanim = btn.openanim + dt * 6 + end + bench.stopBenchmark('update_buttons') + bench.startBenchmark('update_food') for i,f in ipairs(food) do f.y = f.y + dt * f.speed @@ -504,7 +525,7 @@ function love.draw() bench.startBenchmark('render_food') for _,f in ipairs(food) do - local sheet = sheets.food1 + local sheet = sheets['food' .. (f.type)] local x = f.x * sw local y = f.y * sh local frame = math.floor((f.time%1) * #sheet.quads) + 1 @@ -539,50 +560,52 @@ function love.draw() love.graphics.draw(sprites['footer/buttonbg'], x * size, y * size, 0, size, size) end - if btn and btn.open then - -- sprite inside - if btn.sprite == 'guppy' then - local sheet = fishsprite('medium', false, 'swim') - local frame = math.floor((love.timer.getTime() * 2) % 1 * #sheet.quads) + 1 - local scale = (sprites['footer/buttonbg']:getWidth() / sheet.width) * 0.9 - local offset = (sprites['footer/buttonbg']:getWidth() * size) / 2 - love.graphics.draw(sheet.spriteSheet, sheet.quads[frame], x * size + offset, y * size + offset*0.75, 0, size * scale, size * scale, sheet.width/2, sheet.width/2) - elseif btn.sprite == 'food' then - local sheets = {sheets.food2, sheets.food3} - local sheet = sheets[btn.tier] + if btn then + if btn.open then + -- sprite inside + if btn.sprite == 'guppy' then + local sheet = fishsprite('medium', false, 'swim') + local frame = math.floor((love.timer.getTime() * 2) % 1 * #sheet.quads) + 1 + local scale = (sprites['footer/buttonbg']:getWidth() / sheet.width) * 0.9 + local offset = (sprites['footer/buttonbg']:getWidth() * size) / 2 + love.graphics.draw(sheet.spriteSheet, sheet.quads[frame], x * size + offset, y * size + offset*0.75, 0, size * scale, size * scale, sheet.width/2, sheet.width/2) + elseif btn.sprite == 'food' then + local sheets = {sheets.food2, sheets.food3} + local sheet = sheets[btn.tier] - local scale = (sprites['footer/buttonbg']:getWidth() / sheet.width) * 0.65 - local offset = (sprites['footer/buttonbg']:getWidth() * size) / 2 - love.graphics.draw(sheet.spriteSheet, sheet.quads[1], x * size + offset, y * size + offset*0.75, 0, size * scale, size * scale, sheet.width/2, sheet.width/2) - elseif btn.sprite == 'foodcount' then - love.graphics.setFont(fonts.continuum) - local offset = (sprites['footer/buttonbg']:getWidth() * size) / 2 + local scale = (sprites['footer/buttonbg']:getWidth() / sheet.width) * 0.65 + local offset = (sprites['footer/buttonbg']:getWidth() * size) / 2 + love.graphics.draw(sheet.spriteSheet, sheet.quads[1], x * size + offset, y * size + offset*0.75, 0, size * scale, size * scale, sheet.width/2, sheet.width/2) + elseif btn.sprite == 'foodcount' then + love.graphics.setFont(fonts.continuum) + local offset = (sprites['footer/buttonbg']:getWidth() * size) / 2 - local bordersize = 1 - for _,p in ipairs({{0, 1}, {1, 0}, {1, 1}, {-1, 0}, {0, -1}, {-1, -1}, {1, -1}, {-1, 1}}) do - love.graphics.setColor(0, 0, 0, 0.5) - love.graphics.printf(btn.tier + 1, round(x * size) + p[1] * bordersize, round(y * size + offset*0.75 - fonts.continuum:getHeight()/2) + p[2] * bordersize, round(sprites['footer/buttonbg']:getWidth() * size), 'center') + local bordersize = 1 + for _,p in ipairs({{0, 1}, {1, 0}, {1, 1}, {-1, 0}, {0, -1}, {-1, -1}, {1, -1}, {-1, 1}}) do + love.graphics.setColor(0, 0, 0, 0.5) + love.graphics.printf(btn.tier + 1, round(x * size) + p[1] * bordersize, round(y * size + offset*0.75 - fonts.continuum:getHeight()/2) + p[2] * bordersize, round(sprites['footer/buttonbg']:getWidth() * size), 'center') + end + + love.graphics.setColor(0, 1, 0) + love.graphics.printf(btn.tier + 1, round(x * size), round(y * size + offset*0.75 - fonts.continuum:getHeight()/2), round(sprites['footer/buttonbg']:getWidth() * size), 'center') end + -- price + love.graphics.setFont(fonts.pix) + local font = love.graphics.getFont() love.graphics.setColor(0, 1, 0) - love.graphics.printf(btn.tier + 1, round(x * size), round(y * size + offset*0.75 - fonts.continuum:getHeight()/2), round(sprites['footer/buttonbg']:getWidth() * size), 'center') + love.graphics.printf('$' .. btn.cost, round(x * size), round(y * size + 51 * size - font:getHeight()/2), round(sprites['footer/buttonbg']:getWidth() * size), 'center') + love.graphics.setColor(1, 1, 1) + love.graphics.setFont(fonts.default) + + -- reflection + love.graphics.setBlendMode('add') + love.graphics.draw(sprites['footer/reflection'], x * size, y * size, 0, size, size) + love.graphics.setBlendMode('alpha') end - -- price - love.graphics.setFont(fonts.pix) - local font = love.graphics.getFont() - love.graphics.setColor(0, 1, 0) - love.graphics.printf('$' .. btn.cost, round(x * size), round(y * size + 51 * size - font:getHeight()/2), round(sprites['footer/buttonbg']:getWidth() * size), 'center') - love.graphics.setColor(1, 1, 1) - love.graphics.setFont(fonts.default) - - -- reflection - love.graphics.setBlendMode('add') - love.graphics.draw(sprites['footer/reflection'], x * size, y * size, 0, size, size) - love.graphics.setBlendMode('alpha') - -- open/close anim - if (btn and not btn.openanim == 1) then + if (btn and btn.openanim < 1) then local sheet = sheets.buttonopen local anim = 0 if btn then anim = btn.openanim end @@ -606,8 +629,8 @@ function love.draw() end function love.mousepressed(x, y, b) - if b == 2 then - table.insert(food, constr.food(x/love.graphics.getWidth(), y/love.graphics.getHeight(), 1)) + if b == 1 and y > FOOTER_HEIGHT and #food < foodcount then + table.insert(food, constr.food(x/love.graphics.getWidth(), y/love.graphics.getHeight(), foodtier)) end end @@ -622,7 +645,7 @@ function love.mousereleased(x, y, b) if hovered then if footerbuttons[i] and footerbuttons[i].open then - footerbuttons[i].func() + footerbuttons[i].func(footerbuttons[i]) end end