Hi guys,
I am working on a puzzle game but have been stuck on how to handle touch, ie how to move the parts on the board so I would like to ask for some advice or guidance. This is how its supposed to work. The board is built up of 36 (6x6) brushes. As a player you can press and hold a brush and then move it across the board to mix or extract colors from the brushes you move your selected brush over. You can only move the brush up, down, left or right. You can move the brush as far across the board as you want in one direction only to impact several brushes. So if you press a yellow brush and move it over a blue brush the blue brush becomes green. The yellow brush you selected stays yellow. When the player manages to align 3 brushes of the same color they should be removed and new colors should be added. There are some more rules but getting this sorted should help a lot.
Ive removed all my attempts to handle touch since none of them seems to make sense
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT)
-- 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, 255) --purple
}
-- create a palette to place brushes on
palette = {}
paletteWidth = WIDTH
paletteHeiht = WIDTH
brushWidth = paletteWidth/6
brushHeight = paletteWidth/6
startX = brushWidth/2
startY = brushHeight/2
counter = 1
for i=1, 6 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
end
function draw()
background(0)
for i=1, 6 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
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
-- Class Brush
Brush = class()
function Brush:init(s, p, c, index)
self.pos = p
self.size = s
self.col = c
self.ind = index
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:touched(touch)
if self:hit(vec2(touch.x, touch.y)) then
selectedBrushY = self.pos.y
end
if touch.state == MOVING then
if touch.y + brushHeight > selectedBrushY then
-- do something :)
elseif touch.y + brushHeight < selectedBrushY then
-- do something else :)
end
end
end