Refactor getting bubble counts

main
Tyler Scott 2022-02-06 09:37:37 +07:00
parent 4410d32776
commit 046c9f9f4e
1 changed files with 25 additions and 14 deletions

View File

@ -72,6 +72,22 @@ local function get_next_bubble_type(index, remaining_bubble_types)
return remaining_bubble_types[love.math.random(#remaining_bubble_types)]
end
local function get_bubble_counts()
local total = 0
local count_by_type = {}
for i = 1, #game.bubble_slots do
local bubble_type = game.bubble_slots[i].bubble_type
if bubble_type ~= 0 then
total = total + 1
end
if count_by_type[bubble_type] == nil then
count_by_type[bubble_type] = 0
end
count_by_type[bubble_type] = count_by_type[bubble_type] + 1
end
return total, count_by_type
end
local function find_nearest_slot(x, y)
local nearest_slot_index = 0
local min_dist = math.huge
@ -182,6 +198,11 @@ function love.load(arg)
game.level_right = game.level_left + game.level_width
game.level_bottom = game.level_top + game.level_height
for i = 1, #game.bubble_slots do
game.bubble_slots[i].x = game.bubble_slots[i].x + game.level_left
game.bubble_slots[i].y = game.bubble_slots[i].y + game.level_top
end
game.launcher_image = love.graphics.newImage('images/launcher.png')
game.launcher_height = game.launcher_image:getHeight()
game.launcher_width = game.launcher_image:getWidth()
@ -191,22 +212,12 @@ function love.load(arg)
game.launcher_offset_y = game.launcher_height / 2
game.launcher_rotation = -tau / 4 -- up
game.bubble_type_counts = {}
local total_bubble_count, counts_by_type = get_bubble_counts()
local bubble_types = {}
for i = 1, #game.bubble_slots do
game.bubble_slots[i].x = game.bubble_slots[i].x + game.level_left
game.bubble_slots[i].y = game.bubble_slots[i].y + game.level_top
local bubble_type = game.bubble_slots[i].bubble_type
if game.bubble_type_counts[bubble_type] == nil then
game.bubble_type_counts[bubble_type] = 0
if bubble_type >= 1 and bubble_type <= 8 then
bubble_types[#bubble_types+1] = bubble_type
end
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
game.bubble_type_counts[bubble_type] = game.bubble_type_counts[bubble_type] + 1
end
if #bubble_types == 0 then
bubble_types = {1,2,3,4,5,6,7,8}
end
game.next_bubble_index = 1
game.next_bubble = {