/ld27

To get this branch, use:
bzr branch http://9ix.org/bzr/ld27

« back to all changes in this revision

Viewing changes to player.lua

  • Committer: Josh C
  • Date: 2013-08-25 02:04:52 UTC
  • Revision ID: josh@9ix.org-20130825020452-py8fijli8m5te5gv
improve performance with (sketchy, experimental) sprite batches

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
local velLimit = 400
1
2
Player = Tile:extend{
2
3
   image = 'data/player.png',
3
 
   onNew = function(self)
4
 
              self.collider = PlayerCollider:new{x=self.x, y=self.y}
5
 
              the.view:add(self.collider)
6
 
           end,
7
 
   -- onUpdate = function(self, dt)
8
 
   --               self.collider.x = self.x + self.width / 2 * (1 - self.scale)
9
 
   --               self.collider.y = self.y + self.height / 2 * (1 - self.scale)
10
 
   --            end,
11
 
}
12
 
 
13
 
PlayerCollider = Fill:extend{
14
 
   fill = {0,0,255},
15
 
   collisions = {},
 
4
   maxVelocity = {x=velLimit,y=velLimit},
 
5
   minVelocity = {x=-velLimit, y=-velLimit},
16
6
   onStartFrame = function(self)
17
 
     local mouseup, mousedn, mousert, mouselt
18
 
     if the.mouse:pressed() then
19
 
       local x, y = love.mouse.getPosition()
20
 
       local w, h = love.window.getMode()
21
 
 
22
 
       local theta = math.atan2(y - h/2, x - w/2)
23
 
       --local theta = math.atan2(y - self.y * the.app.scale,
24
 
       --                         x - self.x * the.app.scale)
25
 
 
26
 
       -- 1/8 = .125, 7/8 = .875
27
 
       if theta > math.pi * .125 and theta < math.pi * .875 then
28
 
         mousedn = true
29
 
       elseif theta < -math.pi * .125 and theta > -math.pi * .875 then
30
 
         mouseup = true
31
 
       end
32
 
 
33
 
       -- 3/8 = .375, 5/8 = .625
34
 
       if math.abs(theta) < math.pi * .375 then
35
 
         mousert = true
36
 
       elseif math.abs(theta) > math.pi * .625 then
37
 
         mouselt = true
38
 
       end
39
 
 
40
 
       --print('mouse pressed: ' .. theta)
41
 
     end
42
 
 
43
 
     if the.keys:pressed('left') or mouselt then
44
 
       self.velocity.x = -200 * the.activeMaze.moveMod
45
 
     elseif the.keys:pressed('right') or mousert then
46
 
       self.velocity.x = 200 * the.activeMaze.moveMod
47
 
     else
48
 
       self.velocity.x = 0
49
 
     end
50
 
 
51
 
     if the.keys:pressed('up') or mouseup then
52
 
       self.velocity.y = -200 * the.activeMaze.moveMod
53
 
     elseif the.keys:pressed('down') or mousedn then
54
 
       self.velocity.y = 200 * the.activeMaze.moveMod
55
 
     else
56
 
       self.velocity.y = 0
57
 
     end
58
 
   end,
59
 
   update = function(self, elapsed)
60
 
               self.visible = DEBUG and the.console.visible
61
 
 
62
 
               self:doPhysics('x', elapsed)
63
 
               if not (DEBUG and the.console.visible) then
64
 
                  self:collide(the.activeMaze)
65
 
               end
66
 
 
67
 
               -- handle X collisions
68
 
               for _, col in ipairs(self.collisions) do
69
 
                  col.other:displaceDir(self, 'x')
70
 
               end
71
 
 
72
 
               self:doPhysics('y', elapsed)
73
 
               if not (DEBUG and the.console.visible) then
74
 
                  self:collide(the.activeMaze)
75
 
               end
76
 
 
77
 
               -- handle Y collisions
78
 
               for _, col in ipairs(self.collisions) do
79
 
                  col.other:displaceDir(self, 'y')
80
 
               end
81
 
 
82
 
               local p = the.player
83
 
               p.x = self.x - p.width / 2 * (1 - p.scale)
84
 
               p.y = self.y - p.height / 2 * (1 - p.scale)
85
 
            end,
86
 
   collide = function (self, ...)
87
 
                self.collisions = {}
88
 
                Fill.collide(self, ...)
89
 
             end,
90
 
   onCollide = function (self, other, xOverlap, yOverlap)
91
 
                  if other == the.activeMaze then return end
92
 
 
93
 
                  --print('collision')
94
 
                  --print(inspect(other))
95
 
 
96
 
                  table.insert(self.collisions, {other = other,
97
 
                                                 xOverlap = xOverlap,
98
 
                                                 yOverlap = yOverlap })
99
 
               end
100
 
 
101
 
}
 
7
                     if the.keys:pressed('left') then
 
8
                        self.velocity.x = -200 * the.activeMaze.moveMod
 
9
                     elseif the.keys:pressed('right') then
 
10
                        self.velocity.x = 200 * the.activeMaze.moveMod
 
11
                     else
 
12
                        self.velocity.x = 0
 
13
                     end
 
14
 
 
15
                     if the.keys:pressed('up') then
 
16
                        self.velocity.y = -200 * the.activeMaze.moveMod
 
17
                     elseif the.keys:pressed('down') then
 
18
                        self.velocity.y = 200 * the.activeMaze.moveMod
 
19
                     else
 
20
                        self.velocity.y = 0
 
21
                     end
 
22
                  end,
 
23
   onCollide = function(self, other)
 
24
                  other:displace(self)
 
25
               end
 
26
}
 
 
b'\\ No newline at end of file'