/ld27

To get this branch, use:
bzr branch http://9ix.org/bzr/ld27
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
Player = Tile:extend{
   image = 'data/player.png',
   onNew = function(self)
              self.collider = PlayerCollider:new{x=self.x, y=self.y}
              the.view:add(self.collider)
           end,
   -- onUpdate = function(self, dt)
   --               self.collider.x = self.x + self.width / 2 * (1 - self.scale)
   --               self.collider.y = self.y + self.height / 2 * (1 - self.scale)
   --            end,
}

PlayerCollider = Fill:extend{
   fill = {0,0,255},
   collisions = {},
   onStartFrame = function(self)
     local mouseup, mousedn, mousert, mouselt
     if the.mouse:pressed() then
       local x, y = love.mouse.getPosition()
       local w, h = love.window.getMode()

       local theta = math.atan2(y - h/2, x - w/2)
       --local theta = math.atan2(y - self.y * the.app.scale,
       --                         x - self.x * the.app.scale)

       -- 1/8 = .125, 7/8 = .875
       if theta > math.pi * .125 and theta < math.pi * .875 then
         mousedn = true
       elseif theta < -math.pi * .125 and theta > -math.pi * .875 then
         mouseup = true
       end

       -- 3/8 = .375, 5/8 = .625
       if math.abs(theta) < math.pi * .375 then
         mousert = true
       elseif math.abs(theta) > math.pi * .625 then
         mouselt = true
       end

       --print('mouse pressed: ' .. theta)
     end

     if the.keys:pressed('left') or mouselt then
       self.velocity.x = -200 * the.activeMaze.moveMod
     elseif the.keys:pressed('right') or mousert then
       self.velocity.x = 200 * the.activeMaze.moveMod
     else
       self.velocity.x = 0
     end

     if the.keys:pressed('up') or mouseup then
       self.velocity.y = -200 * the.activeMaze.moveMod
     elseif the.keys:pressed('down') or mousedn then
       self.velocity.y = 200 * the.activeMaze.moveMod
     else
       self.velocity.y = 0
     end
   end,
   update = function(self, elapsed)
               self.visible = DEBUG and the.console.visible

               self:doPhysics('x', elapsed)
               if not (DEBUG and the.console.visible) then
                  self:collide(the.activeMaze)
               end

               -- handle X collisions
               for _, col in ipairs(self.collisions) do
                  col.other:displaceDir(self, 'x')
               end

               self:doPhysics('y', elapsed)
               if not (DEBUG and the.console.visible) then
                  self:collide(the.activeMaze)
               end

               -- handle Y collisions
               for _, col in ipairs(self.collisions) do
                  col.other:displaceDir(self, 'y')
               end

               local p = the.player
               p.x = self.x - p.width / 2 * (1 - p.scale)
               p.y = self.y - p.height / 2 * (1 - p.scale)
            end,
   collide = function (self, ...)
                self.collisions = {}
                Fill.collide(self, ...)
             end,
   onCollide = function (self, other, xOverlap, yOverlap)
                  if other == the.activeMaze then return end

                  --print('collision')
                  --print(inspect(other))

                  table.insert(self.collisions, {other = other,
                                                 xOverlap = xOverlap,
                                                 yOverlap = yOverlap })
               end

}