Skip to content

Commit

Permalink
Add ColorManager
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubg1 committed Jan 13, 2021
1 parent 1ccc028 commit 3f30ed3
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 65 deletions.
89 changes: 89 additions & 0 deletions src/ColorManager.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
local class = require "com/class"
local ColorManager = class:derive("ColorManager")

-- Counts spheres on the board
function ColorManager:new()
self.sphereColorCounts = {}
self.dangerSphereColorCounts = {}
self.lastSphereColor = 1

self:reset()
end



--- Returns a random sphere color that is on the board.
-- If no spheres are on the board, this function returns the last sphere color that appeared on the board.
-- @tparam boolean omitDangerCheck When it is set to true, gets just any random sphere color that is on board provided it is not special (e.g. wild).
-- Setting this to false searches only in colors that are in the danger colors list, and if nothing was found there,
-- it gets a random color from the entire board.
-- @treturn number A sphere color.
function ColorManager:pickColor(omitDangerCheck)
local availableColors = {}
if not omitDangerCheck then
-- check the vises in danger first
for i, count in ipairs(self.dangerSphereColorCounts) do
if count > 0 then table.insert(availableColors, i) end
end
if #availableColors > 0 then return availableColors[math.random(1, #availableColors)] end -- if there are, pick one from them
end
for i, count in ipairs(self.sphereColorCounts) do
if count > 0 then table.insert(availableColors, i) end
end
if #availableColors == 0 then return self.lastSphereColor end -- if no spheres present
return availableColors[math.random(1, #availableColors)]
end



--- Resets the onboard color counters back to 0.
function ColorManager:reset()
for i, sphere in pairs(game.spheres) do
if sphere.generatable then
self.sphereColorCounts[i] = 0
self.dangerSphereColorCounts[i] = 0
end
end
end

--- Increments the given color counter by one.
function ColorManager:increment(color, danger)
if danger then
self.dangerSphereColorCounts[color] = self.dangerSphereColorCounts[color] + 1
else
self.sphereColorCounts[color] = self.sphereColorCounts[color] + 1
end
end

--- Decrements the given color counter by one.
function ColorManager:decrement(color, danger)
if danger then
self.dangerSphereColorCounts[color] = self.dangerSphereColorCounts[color] - 1
else
self.sphereColorCounts[color] = self.sphereColorCounts[color] - 1
self.lastSphereColor = color
end
end



function ColorManager:isColorExistent(color)
if not self.sphereColorCounts[color] then return true end
return self.sphereColorCounts[color] > 0
end



function ColorManager:getDebugText()
local s = ""

for i, v in pairs(self.sphereColorCounts) do
s = s .. string.format("%s: N %s D %s\n", i, self.sphereColorCounts[i], self.dangerSphereColorCounts[i])
end

return s
end



return ColorManager
15 changes: 2 additions & 13 deletions src/Debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,6 @@ function Debug:getDebugParticle()
return s
end

function Debug:getDebugSession()
local s = ""

s = s .. "SphereColors:\n"
for i = 1, 9 do
s = s .. tostring(i) .. " -> " .. game.session.sphereColorCounts[i] .. ", " .. game.session.dangerSphereColorCounts[i] .. "\n"
end

return s
end

function Debug:getDebugLevel()
local s = ""

Expand Down Expand Up @@ -201,9 +190,9 @@ function Debug:getDebugInfo()
if game.particleManager then
s = s .. self:getDebugParticle()
end
s = s .. "\n===== SESSION =====\n"
s = s .. "\n===== COLOR MANAGER =====\n"
if game:sessionExists() then
s = s .. self:getDebugSession()
s = s .. game.session.colorManager:getDebugText()
end
s = s .. "\n===== LEVEL =====\n"
if game:levelExists() then
Expand Down
6 changes: 3 additions & 3 deletions src/Level.lua
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function Level:newPowerupData()
local powerupName = names[i]
local powerupData = game.powerups[powerupName]
local data = {type = "powerup", name = powerupName}
if powerupData.colored then data.color = game.session:newSphereColor(true) end
if powerupData.colored then data.color = game.session.colorManager:pickColor(true) end
return data
end

Expand Down Expand Up @@ -339,7 +339,7 @@ function Level:reset()
self.bonusDelay = nil

self.shooter.speedShotTime = 0
game.session:resetSphereColorCounts()
game.session.colorManager:reset()
end

function Level:lose()
Expand Down Expand Up @@ -436,7 +436,7 @@ end
-- Restores all data that was saved in the serialization method.
function Level:deserialize(t)
-- Prepare the counters
game.session:resetSphereColorCounts()
game.session.colorManager:reset()

-- Level stats
self.score = t.stats.score
Expand Down
41 changes: 2 additions & 39 deletions src/Session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ local Vec2 = require("src/Essentials/Vector2")

-- Include class constructors
local Level = require("src/Level")
local ColorManager = require("src/ColorManager")



Expand All @@ -24,11 +25,7 @@ function Session:new()
self.scoreDisplay = 0

self.level = nil

self.sphereColorCounts = {}
self.dangerSphereColorCounts = {}
self:resetSphereColorCounts()
self.lastSphereColor = 1
self.colorManager = ColorManager()
end


Expand Down Expand Up @@ -84,40 +81,6 @@ end



--- Returns a random sphere color that is on the board.
-- If no spheres are on the board, this function returns the last sphere color that appeared on the board.
-- @tparam boolean omitDangerCheck When it is set to true, gets just any random sphere color that is on board provided it is not special (e.g. wild).
-- Setting this to false searches only in colors that are in the danger colors list, and if nothing was found there,
-- it gets a random color from the entire board.
-- @treturn number A sphere color.
function Session:newSphereColor(omitDangerCheck)
local availableColors = {}
if not omitDangerCheck then
-- check the vises in danger first
for i, count in ipairs(self.dangerSphereColorCounts) do
if count > 0 then table.insert(availableColors, i) end
end
if #availableColors > 0 then return availableColors[math.random(1, #availableColors)] end -- if there are, pick one from them
end
for i, count in ipairs(self.sphereColorCounts) do
if count > 0 then table.insert(availableColors, i) end
end
if #availableColors == 0 then return self.lastSphereColor end -- if no spheres present
return availableColors[math.random(1, #availableColors)]
end



--- Resets the onboard color counters back to 0.
function Session:resetSphereColorCounts()
for i = 1, 9 do
self.sphereColorCounts[i] = 0
self.dangerSphereColorCounts[i] = 0
end
end



--- Returns whether both provided colors can attract or make valid scoring combinations with each other.
-- @tparam number color1 The first color to check.
-- @tparam number color2 The second color to check.
Expand Down
9 changes: 5 additions & 4 deletions src/Shooter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ function Shooter:update(dt)

-- filling
if self.active then
if game.session.sphereColorCounts[self.color] == 0 then self:setColor(0) end
if game.session.sphereColorCounts[self.nextColor] == 0 then self:setNextColor(0) end
-- remove inexistant colors
if not game.session.colorManager:isColorExistent(self.color) then self:setColor(0) end
if not game.session.colorManager:isColorExistent(self.nextColor) then self:setNextColor(0) end
self:fill()
end

Expand Down Expand Up @@ -125,11 +126,11 @@ end

function Shooter:fill()
if self.nextColor == 0 then
self:setNextColor(game.session:newSphereColor())
self:setNextColor(game.session.colorManager:pickColor())
end
if self.color == 0 and self.nextColor ~= 0 then
self:setColor(self.nextColor)
self:setNextColor(game.session:newSphereColor())
self:setNextColor(game.session.colorManager:pickColor())
end
end

Expand Down
11 changes: 5 additions & 6 deletions src/Sphere.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function Sphere:new(sphereGroup, deserializationTable, color, shootOrigin)
end

if not self.map.isDummy and self.color > 0 then
game.session.sphereColorCounts[self.color] = game.session.sphereColorCounts[self.color] + 1
game.session.colorManager:increment(self.color)
end

self.danger = false
Expand Down Expand Up @@ -71,9 +71,9 @@ function Sphere:update(dt)
if self.danger ~= danger then
self.danger = danger
if danger then
game.session.dangerSphereColorCounts[self.color] = game.session.dangerSphereColorCounts[self.color] + 1
game.session.colorManager:increment(self.color, true)
else
game.session.dangerSphereColorCounts[self.color] = game.session.dangerSphereColorCounts[self.color] - 1
game.session.colorManager:decrement(self.color, true)
end
end
end
Expand All @@ -93,10 +93,9 @@ function Sphere:delete()
if self.nextSphere then self.nextSphere.prevSphere = self.prevSphere end
-- update color count
if not self.map.isDummy and self.color > 0 then
game.session.sphereColorCounts[self.color] = game.session.sphereColorCounts[self.color] - 1
game.session.lastSphereColor = self.color
game.session.colorManager:decrement(self.color)
if self.danger then
game.session.dangerSphereColorCounts[self.color] = game.session.dangerSphereColorCounts[self.color] - 1
game.session.colorManager:decrement(self.color, true)
end
end
-- particles
Expand Down

0 comments on commit 3f30ed3

Please sign in to comment.