From 4410d327765f12bc04a7e71b5bcdc792f6cca6a4 Mon Sep 17 00:00:00 2001 From: Tyler Scott Date: Sun, 6 Feb 2022 08:07:07 +0700 Subject: [PATCH] Find matches after launched bubble collision --- main.lua | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/main.lua b/main.lua index 3b88d88..5210522 100644 --- a/main.lua +++ b/main.lua @@ -91,6 +91,31 @@ local function find_nearest_slot(x, y) return nearest_slot_index end +local function find_matches(start_index) + local bubble_type = game.bubble_slots[start_index].bubble_type + local to_check = {start_index} + local matches = {start_index} + local visited = {} + visited[start_index] = true + + 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 == bubble_type then + matches[#matches+1] = neighbour_index + to_check[#to_check+1] = neighbour_index + end + visited[neighbour_index] = true + end + end + + return matches +end + function love.load(arg) if arg[#arg] == "debug" then require("lldebugger").start() end @@ -255,6 +280,8 @@ function love.update(dt) game.next_bubble.y = game.bubble_slots[nearest_slot_index].y game.next_bubble.velocity_x = 0 game.next_bubble.velocity_y = 0 + + local matches = find_matches(nearest_slot_index) else game.next_bubble.x = next_x game.next_bubble.y = next_y