-
-
Save fikabord/fd4898215453fef57104 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--# Brush | |
Brush = class() | |
function Brush:init(s, p, c, index) | |
self.pos = p | |
self.size = s | |
self.col = c | |
self.ind = index | |
self.score = false | |
end | |
function Brush:draw() | |
pushStyle() | |
fill(self.col) | |
rectMode(CENTER) | |
rect(self.pos.x, self.pos.y, self.size, self.size) | |
popStyle() | |
end | |
function Brush:hit(point) | |
if point.x > (self.pos.x - self.size/2) and | |
point.x < (self.pos.x + self.size/2) and | |
point.y > (self.pos.y - self.size/2) and | |
point.y < (self.pos.y + self.size/2) then | |
return true | |
end | |
return false | |
end | |
function Brush:setMixColor(startColor, mixColor) | |
local mix = getColorIndex(mixColor) | |
for i=1, 6 do | |
if mixChart[i][1] == startColor then | |
end | |
for j=1, 6 do | |
if mixChart[j][i] == startColor then | |
return mixChart[j][mix+1] | |
end | |
end | |
end | |
end | |
local touchedBrushes = {} | |
local brushAlreadyMixed = false | |
function Brush:touched(touch) | |
if not self:hit(vec2(touch.x, touch.y)) then return end | |
if touch.state == BEGAN then | |
refBrush = self | |
if refBrush.col == colors[7] then -- cannot use grey to mix | |
message = "cannot use grey to mix" | |
else | |
massage = nil | |
end | |
elseif touch.state == MOVING | |
and self~=refBrush | |
and not touchedBrushes[self] then | |
touchedBrushes[self] = true -- remember | |
if brushAlreadyMixed == false then | |
if touch.y > refBrush.pos.y + brushHeight/2 then | |
self.col = Brush:setMixColor(refBrush.col, self.col) | |
brushAlreadyMixed = true | |
elseif touch.y < refBrush.pos.y - brushHeight/2 then | |
self.col = Brush:setMixColor(refBrush.col, self.col) | |
brushAlreadyMixed = true | |
elseif touch.x < refBrush.pos.y - brushWidth/2 then | |
self.col = Brush:setMixColor(refBrush.col, self.col) | |
brushAlreadyMixed = true | |
elseif touch.x > refBrush.pos.y + brushWidth/2 then | |
self.col = Brush:setMixColor(refBrush.col, self.col) | |
brushAlreadyMixed = true | |
end | |
end | |
elseif touch.state == ENDED then | |
touchedBrushes = {} -- reset | |
brushAlreadyMixed = false | |
checkMixedColors() | |
end | |
end | |
--# Main | |
-- Main | |
function setup() | |
-- colors | |
colors = { | |
color(255, 0, 0), -- red | |
color(255, 163, 0), --orange | |
color(255, 255, 0), --yellow | |
color(0, 255, 0), -- green | |
color(0, 0, 255), --blue | |
color(192, 0, 255), --purple | |
color(180, 180, 180) --gray | |
} | |
mixChart = { | |
{colors[1],colors[1],colors[3],colors[2],colors[7],colors[6],colors[7]}, | |
{colors[2],colors[2],colors[1],colors[2],colors[7],colors[7],colors[7]}, | |
{colors[3],colors[2],colors[1],colors[3],colors[5],colors[4],colors[7]}, | |
{colors[4],colors[7],colors[7],colors[5],colors[4],colors[2],colors[7]}, | |
{colors[5],colors[6],colors[7],colors[4],colors[3],colors[5],colors[1]}, | |
{colors[6],colors[5],colors[7],colors[7],colors[7],colors[1],colors[6]} | |
} | |
-- create a palette to place brushes on | |
palette = {} | |
paletteWidth = math.min(WIDTH,HEIGHT) -- <<<< | |
paletteHeiht = paletteWidth -- <<<< | |
brushWidth = paletteWidth/6 | |
brushHeight = paletteWidth/6 | |
startX = brushWidth/2 | |
startY = brushHeight/2 | |
counter = 1 | |
for i=1, 12 do | |
palette[i] = {} | |
for j=1, 6 do | |
local brushColor = randomColor() | |
palette[i][j] = Brush(paletteWidth/6, vec2(startX,startY), brushColor, counter) | |
startX = startX + brushWidth | |
counter = counter + 1 | |
end | |
startX = brushWidth/2 | |
startY = startY + brushHeight | |
end | |
playerScore = 0 | |
message = nil | |
end | |
function draw() | |
background(0) | |
for i=1, 12 do | |
for j=1, 6 do | |
palette[i][j]:draw() | |
fill(255,255,255) | |
local x = palette[i][j].pos.x | |
local y = palette[i][j].pos.y | |
text(palette[i][j].ind, x, y) | |
end | |
end | |
text("score = "..playerScore, WIDTH/2, HEIGHT*0.9) | |
-- message to player | |
if message ~= nil then | |
text(message, WIDTH/2, HEIGHT/2) | |
end | |
end | |
function touched(touch) | |
for i=1, 6 do | |
for j=1, 6 do | |
palette[i][j]:touched(touch) | |
end | |
end | |
end | |
function randomColor() | |
local r = math.random(6) | |
return colors[r] | |
end | |
-- check for matched color combos | |
function checkMixedColors() | |
local scoreFound = false | |
for i=1, 6 do | |
local rowCount = 0 | |
local rowMaxCount = 1 | |
local rowCombo = {} | |
local rowC = nil | |
local columCount = 0 | |
local columMaxCount = 1 | |
local columCombo = {} | |
local columColor = nil | |
for j=1, 6 do | |
-- check for +3 in a row in rows first | |
if palette[i][j].col == rowColor then | |
rowCount = rowCount + 1 | |
if rowCount > rowMaxCount then | |
rowMaxCount = rowMaxCount + 1 | |
rowCombo[#rowCombo+1] = palette[i][j].ind | |
end | |
else | |
rowColor = palette[i][j].col | |
rowCount = 1 | |
if rowMaxCount == 1 then | |
rowCombo[1] = palette[i][j].ind | |
end | |
end | |
-- then check for +3 in a row in colums | |
if palette[j][i].col == columColor then | |
columCount = columCount + 1 | |
if columCount > columMaxCount then | |
columMaxCount = columMaxCount + 1 | |
columCombo[#columCombo+1] = palette[j][i].ind | |
end | |
else | |
columColor = palette[j][i].col | |
columCount = 1 | |
if columMaxCount == 1 then | |
columCombo[1] = palette[j][i].ind | |
end | |
end | |
end | |
-- if we find +3 in a row then mark witch brushes to score | |
if rowMaxCount >= 3 then | |
scoreFound = true | |
for i=1, #rowCombo do | |
resolveCombo(rowCombo) | |
end | |
end | |
if columMaxCount >= 3 then | |
scoreFound = true | |
for i=1, #columCombo do | |
resolveCombo(columCombo) | |
end | |
end | |
end | |
if scoreFound == true then | |
score() | |
end | |
end | |
-- set which brushes generate a score | |
function resolveCombo(combo) | |
for counter=1, #combo do | |
for i=1, 6 do | |
for j=1, 6 do | |
if palette[i][j].ind == combo[counter] then | |
palette[i][j].score = true | |
end | |
end | |
end | |
end | |
end | |
function score() | |
-- increase player score | |
for i=1, 6 do | |
for j=1, 6 do | |
if palette[i][j].score == true then | |
playerScore = playerScore + 1 | |
end | |
end | |
end | |
-- move brushes down where there was a score/hole created | |
local lengthOfHole = 0 | |
local firstHole = nil | |
for i=1, 6 do | |
for j=1, 6 do | |
if palette[j][i].score == true then | |
lengthOfHole = lengthOfHole + 1 | |
if firstHole == nil then | |
firstHole = j | |
end | |
end | |
end | |
if firstHole ~= nil then | |
for counter = 1, 6 do | |
endY = nil | |
endX = nil | |
endPos = palette[firstHole+counter-1][i].pos | |
startPos = palette[firstHole+lengthOfHole+counter-1][i].pos | |
palette[firstHole+counter-1][i] = palette[firstHole+lengthOfHole+counter-1][i] | |
tween(1, startPos, {x=endPos.x, y=endPos.y},tween.easing.quintIn) | |
end | |
firstHole = nil | |
lengthOfHole = 0 | |
end | |
end | |
--set all score flags to false | |
for i=1, 6 do | |
for j=1, 6 do | |
palette[i][j].score = false | |
end | |
end | |
-- generate new colors for brushes above | |
--for i=7, 12 do | |
--for j=1, 6 do | |
--palette[i][j].col = randomColor() | |
--end | |
--end | |
-- check if new combos have been created by falling brushes | |
checkMixedColors() | |
end | |
function getColorIndex(colorIndex) | |
for i=1, 6 do | |
if colors[i] == colorIndex then | |
return i | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment