Remove and animate matched bubbles

main
Tyler Scott 2022-02-06 09:42:31 +07:00
parent 0d4c32864c
commit 00a355221b
1 changed files with 86 additions and 14 deletions

View File

@ -227,6 +227,8 @@ function love.load(arg)
velocity_x = 0, velocity_x = 0,
velocity_y = 0 velocity_y = 0
} }
game.bursting_bubbles = {}
end end
function love.update(dt) function love.update(dt)
@ -234,7 +236,9 @@ function love.update(dt)
return return
end end
if game.next_bubble.velocity_x ~= 0 or game.next_bubble.velocity_y ~= 0 then if game.next_bubble and
(game.next_bubble.velocity_x ~= 0 or game.next_bubble.velocity_y ~= 0) then
local next_x = game.next_bubble.x + game.next_bubble.velocity_x local next_x = game.next_bubble.x + game.next_bubble.velocity_x
local next_y = game.next_bubble.y + game.next_bubble.velocity_y local next_y = game.next_bubble.y + game.next_bubble.velocity_y
local bubble_should_stop = false local bubble_should_stop = false
@ -286,19 +290,68 @@ function love.update(dt)
if bubble_should_stop then if bubble_should_stop then
local nearest_slot_index = find_nearest_slot(next_x, next_y) local nearest_slot_index = find_nearest_slot(next_x, next_y)
game.bubble_slots[nearest_slot_index].bubble_type = game.next_bubble.bubble_type
game.next_bubble.x = game.bubble_slots[nearest_slot_index].x game.next_bubble.x = game.bubble_slots[nearest_slot_index].x
game.next_bubble.y = game.bubble_slots[nearest_slot_index].y game.next_bubble.y = game.bubble_slots[nearest_slot_index].y
game.next_bubble.velocity_x = 0 game.next_bubble.velocity_x = 0
game.next_bubble.velocity_y = 0 game.next_bubble.velocity_y = 0
game.bubble_slots[nearest_slot_index].bubble_type = game.next_bubble.bubble_type
local matches = find_matches(nearest_slot_index) local matches = find_matches(nearest_slot_index)
if #matches >= 3 then
-- remove matches
for i = 1, #matches do
local index = matches[i]
game.bursting_bubbles[#game.bursting_bubbles+1] = {
x = game.bubble_slots[index].x,
y = game.bubble_slots[index].y,
bubble_type = game.bubble_slots[index].bubble_type,
scale = 1.0,
alpha = 1.0
}
game.bubble_slots[index].bubble_type = 0
end
game.next_bubble = nil
end
-- calculate remaining bubble types
local total_bubble_count, counts_by_type = get_bubble_counts()
if total_bubble_count == 0 then
print('level cleared!')
else
local bubble_types = {}
for bubble_type, count in pairs(counts_by_type) do
if bubble_type >= 1 and bubble_type <= 8 then
bubble_types[#bubble_types+1] = bubble_type
end
end
-- get next bubble
game.next_bubble_index = game.next_bubble_index + 1
game.next_bubble = {
x = game.launcher_x,
y = game.launcher_y,
bubble_type = get_next_bubble_type(game.next_bubble_index, bubble_types),
velocity_x = 0,
velocity_y = 0
}
end
else else
game.next_bubble.x = next_x game.next_bubble.x = next_x
game.next_bubble.y = next_y game.next_bubble.y = next_y
end end
end end
if #game.bursting_bubbles > 0 then
for i = #game.bursting_bubbles, 1, -1 do
local bubble = game.bursting_bubbles[i]
bubble.scale = bubble.scale * 1.05
bubble.alpha = bubble.alpha - 0.1
if bubble.scale >= 1.5 then
table.remove(game.bursting_bubbles, i)
end
end
end
if game.frame_by_frame then if game.frame_by_frame then
game.paused = true game.paused = true
game.frame_by_frame = false game.frame_by_frame = false
@ -361,8 +414,26 @@ function love.draw(alpha)
game.launcher_offset_y game.launcher_offset_y
) )
if #game.bursting_bubbles > 0 then
for i = 1, #game.bursting_bubbles do
local bubble = game.bursting_bubbles[i]
love.graphics.setColor(1, 1, 1, bubble.alpha)
love.graphics.draw(
game.bubble_images[bubble.bubble_type],
bubble.x,
bubble.y,
0,
bubble.scale,
bubble.scale,
game.bubble_radius,
game.bubble_radius
)
end
end
-- draw next bubble -- draw next bubble
-- if moving, extrapolate position based on alpha value from love.run -- if moving, extrapolate position based on alpha value from love.run
if game.next_bubble then
love.graphics.setColor(1, 1, 1, 1) love.graphics.setColor(1, 1, 1, 1)
love.graphics.draw( love.graphics.draw(
game.bubble_images[game.next_bubble.bubble_type], game.bubble_images[game.next_bubble.bubble_type],
@ -374,10 +445,11 @@ function love.draw(alpha)
game.bubble_radius, game.bubble_radius,
game.bubble_radius game.bubble_radius
) )
end
end end
function love.mousepressed(x, y, button, is_touch, presses) function love.mousepressed(x, y, button, is_touch, presses)
if button == 1 then if button == 1 and game.next_bubble then
local diff_x = x - game.launcher_x local diff_x = x - game.launcher_x
local diff_y = y - game.launcher_y local diff_y = y - game.launcher_y
local dist = math.sqrt(diff_x * diff_x + diff_y * diff_y) local dist = math.sqrt(diff_x * diff_x + diff_y * diff_y)