-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlot.lua
More file actions
86 lines (70 loc) · 1.81 KB
/
Slot.lua
File metadata and controls
86 lines (70 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
-- Slot.lua
local globalData = require 'globalData'
local Util = require 'Util'
local Tile = require 'Tile'
local Slot = {
grid = nil, -- back link to grid
x = nil, -- column index
y = nil, -- row index
center = nil, -- point table, screen coords
tile = nil, -- tile at this slot, can be nil
}
Slot.__index = Slot
function Slot.new(grid, x, y)
local o = {}
setmetatable(o, Slot)
o.grid = grid
o.x = x
o.y = y
o:position()
return o
end
function Slot:position()
local dim = globalData.dim
-- calculate where the screen coords center point will be
self.center = {x=(self.x * dim.Q) - dim.Q + dim.halfQ, y=(self.y * dim.Q) - dim.Q + dim.halfQ}
self.center.x = self.center.x + dim.firstTileX
self.center.y = dim.firstTileY + self.center.y
end
function Slot:createTile(letter)
if letter == nil then
letter = table.remove(self.grid.letterPool)
end
if letter then
self.tile = Tile.new(self, letter)
end
return letter ~= nil
end
function Slot:deselectAll()
-- plural function goes up the chain to parent (Grid)
self.grid:deselectAllSlots()
end
function Slot:deselect()
-- single function goes down the chain to child (Tile)
if self.tile then
self.tile:deselect()
end
end
function Slot:select(x, y)
if self.tile then
-- only select this slot if event x/y is within radius of center
-- otherwise diagonal drags select adjacent tiles
if Util.pointInCircle(x, y, self.center.x, self.center.y, globalData.dim.Q / 3.33) then
self.tile:select()
self.grid:selectSlot(self)
-- else
-- trace('not in circle')
end
end
end
--[[
function Slot:tapped()
-- bubbled up from Tile:tap event handler
self.grid:tapped(self) -- bubble up to grid
end
]]
function Slot:testSelection()
-- pass this up the chain
self.grid:testSelection()
end
return Slot