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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
WrapTile = Tile:extend {
wrap = function(self)
if self.x < the.app.width / 2 then
self.x = the.bg.width - the.app.width / 2
elseif self.x > the.bg.width - the.app.width / 2 then
self.x = the.app.width / 2
end
if self.y < the.app.height / 2 then
self.y = the.bg.height - the.app.height / 2
elseif self.y > the.bg.height - the.app.height / 2 then
self.y = the.app.height / 2
end
end,
reflect = function(self)
local mirrorX, mirrorY = self.x, self.y
if self.x > the.bg.width - the.app.width then
mirrorX = self.x - the.bg.width + the.app.width
elseif self.x < the.app.width then
mirrorX = self.x + the.bg.width - the.app.width
end
if self.y > the.bg.height - the.app.height then
mirrorY = self.y - the.bg.height + the.app.height
elseif self.y < the.app.height then
mirrorY = self.y + the.bg.height - the.app.height
end
if self.x ~= mirrorX then
if not self.xMirror then
--print('creating mirror: X='..xMirrorX..' Y='..xMirrorY)
self.xMirror = Mirror:new{ of = self, image = self.image }
the.app.view:add(self.xMirror)
end
self.xMirror.x = mirrorX
self.xMirror.y = self.y
self.xMirror.rotation = self.rotation
self.xMirror.scale = self.scale
elseif self.xMirror then
the.app.view:remove(self.xMirror)
self.xMirror = nil
-- die?
end
if self.y ~= mirrorY then
if not self.yMirror then
--print('creating mirror: X='..yMirrorX..' Y='..yMirrorY)
self.yMirror = Mirror:new{ of = self, image = self.image }
the.app.view:add(self.yMirror)
end
self.yMirror.x = self.x
self.yMirror.y = mirrorY
self.yMirror.rotation = self.rotation
self.yMirror.scale = self.scale
elseif self.yMirror then
the.app.view:remove(self.yMirror)
self.yMirror = nil
-- die?
end
if self.x ~= mirrorX and self.y ~= mirrorY then
if not self.xyMirror then
--print('creating mirror: X='..xyMirrorX..' Y='..xyMirrorY)
self.xyMirror = Mirror:new{ of = self, image = self.image }
the.app.view:add(self.xyMirror)
end
self.xyMirror.x = mirrorX
self.xyMirror.y = mirrorY
self.xyMirror.rotation = self.rotation
self.xyMirror.scale = self.scale
elseif self.xyMirror then
the.app.view:remove(self.xyMirror)
self.xyMirror = nil
-- die?
end
end,
onRemove = function(self)
if self.xMirror then
the.app.view:remove(self.xMirror)
end
if self.yMirror then
the.app.view:remove(self.yMirror)
end
if self.xyMirror then
the.app.view:remove(self.xyMirror)
end
end,
update = function (self, elapsed)
Tile.update(self, elapsed)
self:wrap()
self:reflect()
-- TODO: make sure mirrors are cleaned up when we are
-- killed / removed from view
end
}
|