From ad9a0ea35e36f6eabbb5ff2d993cca0bb35e8c45 Mon Sep 17 00:00:00 2001 From: Tyler Scott Date: Sun, 6 Feb 2022 15:21:49 +0700 Subject: [PATCH] Find unattached bubbles and make them fall --- main.lua | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/main.lua b/main.lua index 829bfd0..ac113c2 100644 --- a/main.lua +++ b/main.lua @@ -132,6 +132,45 @@ local function find_matches(start_index) return matches end +local function find_unattached_bubbles() + local to_check = {} + local visited = {} + local attached = {} + local unattached = {} + + -- start with top row bubbles attached to the ceiling + for i = 1, #game.levels[game.current_level][1] do + if game.bubble_slots[i].bubble_type ~= 0 then + to_check[#to_check+1] = i + attached[i] = true + end + visited[i] = true + end + + while #to_check > 0 do + local current_index = table.remove(to_check) + local slot = game.bubble_slots[current_index] + + for i = 1, #slot.neighbours do + local neighbour_index = slot.neighbours[i] + local neighbour = game.bubble_slots[neighbour_index] + if not visited[neighbour_index] and neighbour.bubble_type ~= 0 then + attached[neighbour_index] = true + to_check[#to_check+1] = neighbour_index + end + visited[neighbour_index] = true + end + end + + for i = 1, #game.bubble_slots do + if game.bubble_slots[i].bubble_type ~= 0 and not attached[i] then + unattached[#unattached+1] = i + end + end + + return unattached +end + function love.load(arg) if arg[#arg] == "debug" then require("lldebugger").start() end @@ -229,6 +268,7 @@ function love.load(arg) } game.bursting_bubbles = {} + game.falling_bubbles = {} end function love.update(dt) @@ -298,6 +338,8 @@ function love.update(dt) local matches = find_matches(nearest_slot_index) if #matches >= 3 then + game.next_bubble = nil + -- remove matches for i = 1, #matches do local index = matches[i] @@ -310,7 +352,20 @@ function love.update(dt) } game.bubble_slots[index].bubble_type = 0 end - game.next_bubble = nil + + -- remove unattached bubbles + local unattached = find_unattached_bubbles() + for i = 1, #unattached do + local index = unattached[i] + game.falling_bubbles[#game.falling_bubbles+1] = { + x = game.bubble_slots[index].x, + y = game.bubble_slots[index].y, + velocity_x = i % 2 == 0 and -2 or 2, + velocity_y = love.math.random(-2.0, -5.0), + bubble_type = game.bubble_slots[index].bubble_type + } + game.bubble_slots[index].bubble_type = 0 + end end -- calculate remaining bubble types @@ -352,6 +407,23 @@ function love.update(dt) end end + if #game.falling_bubbles > 0 then + for i = #game.falling_bubbles, 1, -1 do + local bubble = game.falling_bubbles[i] + bubble.x = bubble.x + bubble.velocity_x + bubble.y = bubble.y + bubble.velocity_y + bubble.velocity_x = bubble.velocity_x * 0.99 + bubble.velocity_y = bubble.velocity_y + 0.3 -- gravity 0.3 + if bubble.x - game.bubble_radius <= game.level_left or + bubble.x + game.bubble_radius >= game.level_right then + bubble.velocity_x = -bubble.velocity_x + end + if bubble.y - game.bubble_radius > game.window_height then + table.remove(game.falling_bubbles, i) + end + end + end + if game.frame_by_frame then game.paused = true game.frame_by_frame = false @@ -414,6 +486,7 @@ function love.draw(alpha) game.launcher_offset_y ) + -- draw bursting bubbles if #game.bursting_bubbles > 0 then for i = 1, #game.bursting_bubbles do local bubble = game.bursting_bubbles[i] @@ -431,6 +504,24 @@ function love.draw(alpha) end end + -- draw falling bubbles + if #game.falling_bubbles > 0 then + for i = 1, #game.falling_bubbles do + local bubble = game.falling_bubbles[i] + love.graphics.setColor(1, 1, 1, bubble.alpha) + love.graphics.draw( + game.bubble_images[bubble.bubble_type], + bubble.x + bubble.velocity_x * alpha, + bubble.y + bubble.velocity_y * alpha, + 0, + bubble.scale, + bubble.scale, + game.bubble_radius, + game.bubble_radius + ) + end + end + -- draw next bubble -- if moving, extrapolate position based on alpha value from love.run if game.next_bubble then