/zoeplat

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

« back to all changes in this revision

Viewing changes to main.lua

  • Committer: Josh C
  • Date: 2013-03-12 15:08:32 UTC
  • Revision ID: josh@9ix.org-20130312150832-ue1q4xuq4weypqc2
fairly major overhaul of collision handling to track whether we're on a 
wall.  also monkey patch Sprite to displace on only one axis

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
DEBUG = true
3
3
 
4
4
require 'zoetrope'
 
5
__ = require 'underscore'
5
6
--inspect = require 'inspect'
6
 
--_ = require 'underscore'
7
7
 
8
8
Player = Animation:extend {
9
9
   image = 'data/player.png',
14
14
      walk = { frames = { 2, 3 }, fps = 5 },
15
15
      jump = { frames = { 4 }, fps = 1 }
16
16
   },
 
17
   collisions = {},
 
18
   onWall = false,
17
19
   onNew = function (self)
18
20
              self.velocity.y = 0
19
21
              self.maxVelocity.y = 400
20
22
           end,
 
23
   doPhysics = function (self, dir, elapsed)
 
24
                  local vel = self.velocity
 
25
                  local acc = self.acceleration
 
26
                  local drag = self.drag
 
27
                  local minVel = self.minVelocity
 
28
                  local maxVel = self.maxVelocity
 
29
 
 
30
                  -- check existence of properties
 
31
 
 
32
                  if STRICT then
 
33
                     assert(vel, 'active sprite has no velocity property')
 
34
                     assert(acc, 'active sprite has no acceleration property')
 
35
                     assert(drag, 'active sprite has no drag property')
 
36
                     assert(minVel, 'active sprite has no minVelocity property')
 
37
                     assert(maxVel, 'active sprite has no maxVelocity property')
 
38
                     assert(__.include({'x','y','rotation'}, dir), 'direction should be x, y, or rotation')
 
39
                  end
 
40
 
 
41
                  vel.x = vel.x or 0
 
42
                  vel.y = vel.y or 0
 
43
                  vel.rotation = vel.rotation or 0
 
44
 
 
45
                  -- physics
 
46
 
 
47
                  if acc[dir] and acc[dir] ~= 0 then
 
48
                     vel[dir] = vel[dir] + acc[dir] * elapsed
 
49
                  else
 
50
                     if drag[dir] then
 
51
                        if vel[dir] > 0 then
 
52
                           vel[dir] = vel[dir] - drag[dir] * elapsed
 
53
                           if vel[dir] < 0 then vel[dir] = 0 end
 
54
                        elseif vel[dir] < 0 then
 
55
                           vel[dir] = vel[dir] + drag[dir] * elapsed
 
56
                           if vel[dir] > 0 then vel[dir] = 0 end
 
57
                        end
 
58
                     end
 
59
                  end
 
60
 
 
61
                  if minVel[dir] and vel[dir] < minVel[dir] then vel[dir] = minVel[dir] end
 
62
                  if maxVel[dir] and vel[dir] > maxVel[dir] then vel[dir] = maxVel[dir] end
 
63
 
 
64
                  if vel[dir] ~= 0 then self[dir] = self[dir] + vel[dir] * elapsed end
 
65
               end,
21
66
   onStartFrame = function (self)
22
67
                     -- this is all in startframe so it happens before
23
68
                     -- physics calc at beginning of update
27
72
                     if self.falling then self.jumping = false end
28
73
                     --print(self.jumping, self.falling)
29
74
 
30
 
                     if not self:onGround() then
 
75
                     if (not self.onGround) and (not self.onWall) then
31
76
                        self:play('jump')
32
77
                     end
33
78
 
34
79
                     self.velocity.x = 0
35
80
                     self.acceleration.y = 800
36
81
 
 
82
                     if self.onWall then
 
83
                        self.acceleration.y = 0
 
84
 
 
85
                        if the.keys:pressed('up') then
 
86
                           self.velocity.y = -200
 
87
                           self:play('stand')
 
88
                        elseif the.keys:pressed('down') then
 
89
                           self.velocity.y = 200
 
90
                           self:play('stand')
 
91
                        else
 
92
                           self.velocity.y = 0
 
93
                           self:play('stand')
 
94
                        end
 
95
                     end
 
96
 
37
97
                     if the.keys:pressed('left') then
38
98
                        self.velocity.x = -200
39
 
                        if self:onGround() then self:play('walk') end
 
99
                        if self.onGround then self:play('walk') end
 
100
                        if self.onWall == 'right' then self.onWall = false end
 
101
                        if self.onWall == 'right' then self.onWall = false end
40
102
                     elseif the.keys:pressed('right') then
41
103
                        self.velocity.x = 200
42
 
                        if self:onGround() then self:play('walk') end
 
104
                        if self.onGround then self:play('walk') end
 
105
                        if self.onWall == 'left' then self.onWall = false end
43
106
                     else
44
 
                        if self:onGround() then self:play('stand') end
 
107
                        if self.onGround then self:play('stand') end
45
108
                     end
46
109
 
47
 
                     if the.keys:justPressed('up') and self:onGround() then
 
110
                     if the.keys:justPressed('up') and self.onGround then
48
111
                        self.velocity.y = -400
49
112
                        self.jumping = true
50
113
                     end
51
114
                  end,
52
 
   onEndFrame = function (self)
53
 
                   --print(self.velocity.y)
54
 
                end,
 
115
   update = function (self, elapsed)
 
116
               -- NOTE: this is an override, not a callback
 
117
 
 
118
               self:doPhysics('x', elapsed)
 
119
               self:collide(the.view.map)
 
120
 
 
121
               -- handle X collisions
 
122
               for _, col in ipairs(self.collisions) do
 
123
                  col.other:displaceDir(self, 'x')
 
124
                  if self.velocity.x > 0 then
 
125
                     self.onWall = 'right'
 
126
                  elseif self.velocity.x < 0 then
 
127
                     self.onWall = 'left'
 
128
                  else
 
129
                     print 'x ??'
 
130
                  end
 
131
               end
 
132
 
 
133
               self.onGround = false -- right before Y collision callbacks
 
134
               self:doPhysics('y', elapsed)
 
135
               self:collide(the.view.map)
 
136
 
 
137
               -- handle Y collisions
 
138
               for _, col in ipairs(self.collisions) do
 
139
                  if self.velocity.y > 0 then
 
140
                     self.onGround = true
 
141
                  end
 
142
 
 
143
                  col.other:displaceDir(self, 'y')
 
144
                  self.velocity.y = 0
 
145
                  self.jumping = false
 
146
               end
 
147
 
 
148
               Animation.update(self, elapsed)
 
149
            end,
 
150
   collide = function (self, ...)
 
151
                self.collisions = {}
 
152
                Animation.collide(self, ...)
 
153
                -- I could return a true/false value here if I wanted to...
 
154
             end,
55
155
   onCollide = function (self, other, xOverlap, yOverlap)
56
 
                  -- seriously, why does this even fire?
57
156
                  if other == the.view.map then return end
58
157
 
59
 
                  --print(string.format('col s{x=%i y=%i w=%i h=%i} %s', self.x, self.y, self.width, self.height, tostring(other)))
60
 
                  --print('vel.x:'..self.velocity.x.." vel.y:"..self.velocity.y)
61
 
 
62
 
                  -- assumption: any other collision is with a solid map tile
63
 
                  if yOverlap > xOverlap then
64
 
                     other:displace(self)
65
 
                  elseif xOverlap > yOverlap then
66
 
                     -- check if we've moved since collisions were generated
67
 
                     local xov, yov = self:overlap(other.x, other.y,
68
 
                                                   other.width, other.height)
69
 
                     if xov ~= 0 and yov ~= 0 then
70
 
                        self.velocity.y = 0
71
 
                        other:displace(self)
72
 
                        self.jumping = false
73
 
                     end
74
 
                  else
75
 
                     print('??')
76
 
                  end
77
 
 
78
 
               end,
79
 
   onGround = function (self)
80
 
                 return (not self.jumping) and (not self.falling)
81
 
              end
 
158
                  table.insert(self.collisions, {other = other,
 
159
                                                 xOverlap = xOverlap,
 
160
                                                 yOverlap = yOverlap })
 
161
               end
82
162
}
83
163
 
 
164
-- displace on a specific axis (monkey patch Sprite)
 
165
function Sprite:displaceDir(other, dir)
 
166
   if not self.solid or self == other or not other.solid then return end
 
167
   if STRICT then assert(other:instanceOf(Sprite), 'asked to displace a non-sprite') end
 
168
 
 
169
   if other.sprites then
 
170
      -- handle groups
 
171
 
 
172
      for _, spr in pairs(other.sprites) do
 
173
         self:displace(spr, dir)
 
174
      end
 
175
   else
 
176
      -- handle sprites
 
177
      local dim
 
178
      if dir == 'x' then
 
179
         dim = 'width'
 
180
      elseif dir == 'y' then
 
181
         dim = 'height'
 
182
      else
 
183
         print 'dir ??'
 
184
      end
 
185
 
 
186
      local negMove = (other[dir] - self[dir]) + other[dim]
 
187
      local posMove = (self[dir] + self[dim]) - other[dir]
 
188
 
 
189
      -- TODO: re-add hinting?
 
190
      if negMove < posMove then
 
191
         chg = - negMove
 
192
      else
 
193
         chg = posMove
 
194
      end
 
195
   end
 
196
 
 
197
   other[dir] = other[dir] + chg
 
198
end
 
199
 
84
200
GameView = View:extend {
85
201
   onNew = function (self)
86
202
              self:loadLayers('data/map.lua')
89
205
           end,
90
206
   onUpdate = function (self)
91
207
                 --print('tick')
92
 
                 the.player:collide(self.map)
 
208
                 --the.player:collide(self.map)
93
209
                 --self.map:collide(the.player)
94
210
              end
95
211
}
97
213
the.app = App:new {
98
214
   onRun = function (self)
99
215
              self.view = GameView:new()
100
 
              --print(inspect(_(the.app):keys()))
 
216
              self.console:watch('onGround', 'the.player.onGround')
 
217
              self.console:watch('onWall', 'the.player.onWall')
101
218
           end,
102
219
   onUpdate = function (self, dt)
103
220
                 if the.keys:justPressed('escape') and 
104
221
                   not self.console.visible then
105
 
                    love.event.quit()
 
222
                    self.quit()
106
223
                 end
107
224
              end
108
225
}
 
 
b'\\ No newline at end of file'