-- Domino card class - update #1
-- by: @beezing
Domino = class()
-- default values
local cardWidth = 100
local cardHeight = cardWidth*2
local dotSize = cardWidth/4
local separatorWidth = cardWidth/20+1
local defDotColor = color(176, 0, 0, 255) -- red
local defBackColor = color(255, 255, 176, 255) -- yellow
local defStripeCol1 = color( 0, 32, 223, 255) -- blue
local defStripeCol2 = color(223, 32, 0, 255) -- red
-- create a domino card with the given numbers
function Domino:init(top, bottom)
-- limit numbers
self.top = math.min(math.max(top, 0), 6)
self.bottom = math.min(math.max(bottom, 0), 6)
-- geometry
self.position = vec2(0, 0)
self.scale = 1.0
self.angle = 0
-- status
self.closed = false
self.hold = false
-- colors
self.dotColor = defDotColor
self.backColor = defBackColor
end
-- draw domino dots of number
function Domino:drawDots(number)
-- offset from center
ofs = dotSize * self.scale
-- adjust dot size
local size = 0
if number == 1 then
size = dotSize * self.scale * 2.0 -- dot size is twice bigger for 1
elseif number == 6 then
size = dotSize * self.scale * 0.9 -- dot size is 10% smaller for 6
else
size = dotSize * self.scale
end
-- draw dots
if number == 1 then
ellipse(0, 0, size)
elseif number == 2 then
ellipse(-ofs,-ofs, size)
ellipse( ofs, ofs, size)
elseif number == 3 then
ellipse(-ofs,-ofs, size)
ellipse( 0, 0, size)
ellipse( ofs, ofs, size)
elseif number == 4 then
ellipse(-ofs,-ofs, size)
ellipse(-ofs, ofs, size)
ellipse( ofs,-ofs, size)
ellipse( ofs, ofs, size)
elseif number == 5 then
ellipse( 0, 0, size)
ellipse(-ofs,-ofs, size)
ellipse(-ofs, ofs, size)
ellipse( ofs,-ofs, size)
ellipse( ofs, ofs, size)
elseif number == 6 then
ellipse(-ofs, 0, size)
ellipse( ofs, 0, size)
ellipse(-ofs,-ofs, size)
ellipse(-ofs, ofs, size)
ellipse( ofs,-ofs, size)
ellipse( ofs, ofs, size)
end
end
-- draw stripes for the backside
function Domino:drawStripes()
-- realign coordinate origin
translate(-cardWidth/2 * self.scale, -cardHeight/2 * self.scale)
-- set drawing properties
rectMode(CORNER)
lineCapMode(SQUARE)
-- compute stripe properties
local div = 10 -- number of stripes
local bar = cardWidth * self.scale / div -- width of split bar
local mid = bar/2 -- center of split bar
local wid = bar-1 -- width of stripe
-- draw stripes
strokeWidth(wid)
for i = 1, 2*div do
-- draw short stripes
stroke(defStripeCol1)
if i % 2 == 0 then
line((i/2-1)*bar+mid, 0, (i/2-1)*bar+mid, cardHeight * self.scale)
end
-- draw long stripes
stroke(defStripeCol2)
line(0, (i-1)*bar+mid, cardWidth * self.scale, (i-1)*bar+mid)
end
end
-- draw opened card
function Domino:drawOpened()
-- save matrix
pushStyle()
pushMatrix()
-- transform geometry
translate(self.position.x, self.position.y)
rotate(self.angle)
-- draw background
rectMode(CENTER)
fill(self.backColor)
rect(0, 0, cardWidth * self.scale, cardHeight * self.scale)
-- draw separator line
local l = 0.8 -- line length percentage
lineCapMode(PROJECT)
strokeWidth(separatorWidth * self.scale)
stroke(self.dotColor)
line(-l/2 * cardWidth * self.scale, 0, l/2 * cardWidth * self.scale, 0)
-- draw dots
noStroke()
fill(self.dotColor)
local c = cardWidth/2 -- dot center
-- draw top number
pushMatrix()
translate(0, c * self.scale)
self:drawDots(self.top)
popMatrix()
-- draw bottom number
pushMatrix()
translate(0, -c * self.scale)
self:drawDots(self.bottom)
popMatrix()
-- restore matrix
popMatrix()
popStyle()
end
-- draw closed card
function Domino:drawClosed()
-- save matrix
pushStyle()
pushMatrix()
-- transform geometry
translate(self.position.x, self.position.y)
rotate(self.angle)
-- draw background
fill(255, 255, 255, 255)
rectMode(CENTER)
rect(0, 0, cardWidth * self.scale, cardHeight * self.scale)
-- draw backside ornament (only stripes for now)
self:drawStripes()
-- restore geometry
popMatrix()
popStyle()
end
-- handle card touch
function Domino:touched()
local w = cardWidth * self.scale / 2
local h = cardHeight * self.scale / 2
-- rotate touch position
local t = vec2(CurrentTouch.x, CurrentTouch.y)
--t = t:rotate(math.rad(self.angle))
-- detect touch
if CurrentTouch.state == BEGAN then
if t.x >= self.position.x-w and
t.x <= self.position.x-w + w*2 and
t.y >= self.position.y-h and
t.y <= self.position.y-h + h*2 and
self.hold == false then
self.hold = true
end
end
-- detect drag
if CurrentTouch.state == MOVING then
if t.x >= self.position.x-w and
t.x <= self.position.x-w + w*2 and
t.y >= self.position.y-h and
t.y <= self.position.y-h + h*2 and
self.hold == true then
self.position = vec2(t.x, t.y)
end
end
-- detect end of touch
if CurrentTouch.state == ENDED then
if t.x >= self.position.x-w and
t.x <= self.position.x-w + w*2 and
t.y >= self.position.y-h and
t.y <= self.position.y-h + h*2 and
self.hold == true then
self.hold = false
end
end
end
-- draw card
function Domino:draw()
if self.closed == true then
self:drawClosed()
else
self:drawOpened()
end
end