/traderous

To get this branch, use:
bzr branch /bzr/traderous
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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='..mirrorX..' Y='..mirrorY)

                     self.xMirror = Mirror:new{ of = self, image = self.image }
                     the.mirrors: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
                  if self.type == 'bullet' then
                     --print('pruning bullet')
                  end
                  the.mirrors:remove(self.xMirror)
                  self.xMirror.of = nil -- break circular reference
                  self.xMirror = nil
                  -- die?
               end

               if self.y ~= mirrorY then
                  if not self.yMirror then
                     --print('creating mirror: X='..mirrorX..' Y='..mirrorY)

                     self.yMirror = Mirror:new{ of = self, image = self.image }
                     the.mirrors: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
                  if self.type == 'bullet' then
                     --print('pruning bullet')
                  end
                  the.mirrors:remove(self.yMirror)
                  self.yMirror.of = nil -- break circular reference
                  self.yMirror = nil
                  -- die?
               end

               if self.x ~= mirrorX and self.y ~= mirrorY then
                  if not self.xyMirror then
                     --print('creating mirror: X='..mirrorX..' Y='..mirrorY)

                     self.xyMirror = Mirror:new{ of = self, image = self.image }
                     the.mirrors: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
                  if self.type == 'bullet' then
                     --print('pruning bullet')
                  end
                  the.mirrors:remove(self.xyMirror)
                  self.xyMirror.of = nil -- break circular reference
                  self.xyMirror = nil
                  -- die?
               end
            end,
   onRemove = function(self)
                 if self.xMirror then
                    if self.type == 'bullet' then
                       --print('removing bullet, so removing mirror')
                    end
                    the.mirrors:remove(self.xMirror)
                    self.xMirror.of = nil -- break circular reference
                    self.xMirror = nil
                 end

                 if self.yMirror then
                    if self.type == 'bullet' then
                       --print('removing bullet, so removing mirror')
                    end
                    the.mirrors:remove(self.yMirror)
                    self.yMirror.of = nil -- break circular reference
                    self.yMirror = nil
                 end

                 if self.xyMirror then
                    if self.type == 'bullet' then
                       --print('removing bullet, so removing mirror')
                    end
                    the.mirrors:remove(self.xyMirror)
                    self.xyMirror.of = nil -- break circular reference
                    self.xyMirror = nil
                 end
              end,
   update = function (self, elapsed)
               Tile.update(self, elapsed)

               self:wrap()
               if self.active then
                  self:reflect()
               end
            end
}