/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-09 04:58:45 UTC
  • Revision ID: josh@9ix.org-20130309045845-wdcov53ghv2h6iwf
apply acceleration to velocity, THEN apply velocity to position (then do 
collision).  important for detecting correct velocity

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
STRICT = true
2
2
DEBUG = true
3
 
RECORD = true
4
 
PLAYBACK = false
5
3
 
6
4
require 'zoetrope'
7
 
__ = require 'underscore'
8
 
--inspect = require 'inspect'
9
 
require 'pepperprof'
10
 
 
11
 
util = {
12
 
   dim = function(dir)
13
 
      if dir == 'x' then
14
 
         return 'width'
15
 
      elseif dir == 'y' then
16
 
         return 'height'
17
 
      else
18
 
         if STRICT then error('dir '..dir) end
19
 
      end
20
 
   end
21
 
}
22
5
 
23
6
Player = Animation:extend {
24
7
   image = 'data/player.png',
27
10
   sequences = {
28
11
      stand = { frames = { 1 }, fps = 1 },
29
12
      walk = { frames = { 2, 3 }, fps = 5 },
30
 
      jump = { frames = { 4 }, fps = 1 },
31
 
      climbLeft = { frames = { 5, 6 }, fps = 5 },
32
 
      climbRight = { frames = { 7, 8 }, fps = 5 }
 
13
      jump = { frames = { 4 }, fps = 1 }
33
14
   },
34
 
   collisions = {},
35
 
   onWall = false,
36
 
   leftWallAt = 0,
37
15
   onNew = function (self)
38
16
              self.velocity.y = 0
39
17
              self.maxVelocity.y = 400
40
18
           end,
41
 
   doPhysics = function (self, dir, elapsed)
42
 
                  local vel = self.velocity
43
 
                  local acc = self.acceleration
44
 
                  local drag = self.drag
45
 
                  local minVel = self.minVelocity
46
 
                  local maxVel = self.maxVelocity
47
 
 
48
 
                  -- check existence of properties
49
 
 
50
 
                  if STRICT then
51
 
                     assert(vel, 'active sprite has no velocity property')
52
 
                     assert(acc, 'active sprite has no acceleration property')
53
 
                     assert(drag, 'active sprite has no drag property')
54
 
                     assert(minVel, 'active sprite has no minVelocity property')
55
 
                     assert(maxVel, 'active sprite has no maxVelocity property')
56
 
                     assert(__.include({'x','y','rotation'}, dir), 'direction should be x, y, or rotation')
57
 
                  end
58
 
 
59
 
                  vel.x = vel.x or 0
60
 
                  vel.y = vel.y or 0
61
 
                  vel.rotation = vel.rotation or 0
62
 
 
63
 
                  -- physics
64
 
 
65
 
                  if acc[dir] and acc[dir] ~= 0 then
66
 
                     vel[dir] = vel[dir] + acc[dir] * elapsed
67
 
                  else
68
 
                     if drag[dir] then
69
 
                        if vel[dir] > 0 then
70
 
                           vel[dir] = vel[dir] - drag[dir] * elapsed
71
 
                           if vel[dir] < 0 then vel[dir] = 0 end
72
 
                        elseif vel[dir] < 0 then
73
 
                           vel[dir] = vel[dir] + drag[dir] * elapsed
74
 
                           if vel[dir] > 0 then vel[dir] = 0 end
75
 
                        end
76
 
                     end
77
 
                  end
78
 
 
79
 
                  if minVel[dir] and vel[dir] < minVel[dir] then vel[dir] = minVel[dir] end
80
 
                  if maxVel[dir] and vel[dir] > maxVel[dir] then vel[dir] = maxVel[dir] end
81
 
 
82
 
                  -- ugly hack for falling through floor on really slow frames
83
 
                  if math.abs(vel[dir] * elapsed) > 32 then
84
 
                     print('skip')
85
 
                     return
86
 
                  end
87
 
 
88
 
                  if vel[dir] ~= 0 then self[dir] = self[dir] + vel[dir] * elapsed end
89
 
 
90
 
                  if self[dir] < 0 then self[dir] = 0 end
91
 
                  local edge = the.view.map[util.dim(dir)] -
92
 
                               the.player[util.dim(dir)]
93
 
                  -- TODO: take map position into account
94
 
                  if self[dir] > edge then self[dir] = edge end
95
 
               end,
96
19
   onStartFrame = function (self)
97
20
                     -- this is all in startframe so it happens before
98
21
                     -- physics calc at beginning of update
99
22
 
100
 
                     -- jumping/falling updates could go in EndFrame...
101
 
                     self.falling = self.velocity.y > 0
102
 
                     if self.falling then self.jumping = false end
103
 
                     --print(self.jumping, self.falling)
104
 
 
105
 
                     if (not self.onGround) and (not self.onWall) then
106
 
                        self:play('jump')
107
 
                     end
108
 
 
 
23
                     self.velocity.x = 0
109
24
                     self.acceleration.y = 800
110
25
 
111
 
                     if self.onWall then
112
 
                        self.acceleration.y = 0
113
 
 
114
 
                        if self.onWall == 'right' then
115
 
                           self:play('climbRight')
116
 
                        elseif self.onWall == 'left' then
117
 
                           self:play('climbLeft')
118
 
                        end
119
 
 
120
 
                        if the.keys:pressed('up') then
121
 
                           self.velocity.y = -200
122
 
                        elseif the.keys:pressed('down') then
123
 
                           self.velocity.y = 200
124
 
                        else
125
 
                           self.velocity.y = 0
126
 
                           self:freeze(self.sequences[self.currentName].frames[1])
127
 
                        end
128
 
                     end
129
 
 
130
26
                     if the.keys:pressed('left') then
131
27
                        self.velocity.x = -200
132
 
                        if self.onGround then self:play('walk') end
133
 
                        if self.onWall == 'right' then
134
 
                           self.onWall = false
135
 
                           self.leftWallAt = love.timer.getTime()
136
 
                        end
 
28
                        self:play('walk')
137
29
                     elseif the.keys:pressed('right') then
138
30
                        self.velocity.x = 200
139
 
                        if self.onGround then self:play('walk') end
140
 
                        if self.onWall == 'left' then
141
 
                           self.onWall = false
142
 
                           self.leftWallAt = love.timer.getTime()
143
 
                        end
 
31
                        self:play('walk')
144
32
                     else
145
 
                        if not self.onWall then
146
 
                           if self.onGround then self:play('stand') end
147
 
                           self.velocity.x = 0
148
 
                        end
 
33
                        self:play('stand')
149
34
                     end
150
35
 
151
 
                     if the.keys:justPressed('up') and
152
 
                      (self.onGround or the.console.visible or
153
 
                       (love.timer.getTime() - self.leftWallAt < .1) ) then
 
36
                     if the.keys:justPressed('up') then
154
37
                        self.velocity.y = -400
155
 
                        self.jumping = true
156
38
                     end
157
39
                  end,
158
 
   update = function (self, elapsed)
159
 
               -- NOTE: this is an override, not a callback
160
 
 
161
 
               self:doPhysics('x', elapsed)
162
 
               self:collide(the.view.map)
163
 
 
164
 
               -- handle X collisions
165
 
               self.onWall = false
166
 
               for _, col in ipairs(self.collisions) do
167
 
                  col.other:displaceDir(self, 'x')
168
 
                  if self.velocity.x > 0 then
169
 
                     self.onWall = 'right'
170
 
                  elseif self.velocity.x < 0 then
171
 
                     self.onWall = 'left'
172
 
                  else
173
 
                     print 'x ??'
174
 
                  end
175
 
               end
176
 
 
177
 
               self.onGround = false -- right before Y collision callbacks
178
 
               self:doPhysics('y', elapsed)
179
 
               self:collide(the.view.map)
180
 
 
181
 
               -- handle Y collisions
182
 
               for _, col in ipairs(self.collisions) do
183
 
                  if self.velocity.y > 0 then
184
 
                     self.onGround = true
185
 
                  end
186
 
 
187
 
                  col.other:displaceDir(self, 'y')
188
 
                  self.velocity.y = 0
189
 
                  self.jumping = false
190
 
               end
191
 
 
192
 
               Animation.update(self, elapsed)
193
 
            end,
194
 
   collide = function (self, ...)
195
 
                self.collisions = {}
196
 
                Animation.collide(self, ...)
197
 
                -- I could return a true/false value here if I wanted to...
198
 
             end,
199
40
   onCollide = function (self, other, xOverlap, yOverlap)
 
41
                  -- seriously, why does this even fire?
200
42
                  if other == the.view.map then return end
201
43
 
202
 
                  table.insert(self.collisions, {other = other,
203
 
                                                 xOverlap = xOverlap,
204
 
                                                 yOverlap = yOverlap })
 
44
                  --print(string.format('col s{x=%i y=%i w=%i h=%i} %s', self.x, self.y, self.width, self.height, tostring(other)))
 
45
                  --print('vel.x:'..self.velocity.x.." vel.y:"..self.velocity.y)
 
46
 
 
47
                  -- assumption: any other collision is with a solid map tile
 
48
                  if yOverlap > xOverlap then
 
49
                     other:displace(self)
 
50
                  elseif xOverlap > yOverlap then
 
51
                     -- check if we've moved since collisions were generated
 
52
                     local xov, yov = self:overlap(other.x, other.y,
 
53
                                                   other.width, other.height)
 
54
                     if xov ~= 0 and yov ~= 0 then
 
55
                        self.velocity.y = 0
 
56
                        other:displace(self)
 
57
                     end
 
58
                  else
 
59
                     print('??')
 
60
                  end
 
61
 
205
62
               end
206
63
}
207
64
 
208
 
function Sprite:displaceDir(other, dir)
209
 
   if not self.solid or self == other or not other.solid then return end
210
 
   if STRICT then assert(other:instanceOf(Sprite), 'asked to displace a non-sprite') end
211
 
 
212
 
   if other.sprites then
213
 
      -- handle groups
214
 
 
215
 
      for _, spr in pairs(other.sprites) do
216
 
         self:displace(spr, dir)
217
 
      end
218
 
   else
219
 
      -- handle sprites
220
 
      local dim = util.dim(dir)
221
 
 
222
 
      local negMove = (other[dir] - self[dir]) + other[dim]
223
 
      local posMove = (self[dir] + self[dim]) - other[dir]
224
 
 
225
 
      -- TODO: re-add hinting?
226
 
      if negMove < posMove then
227
 
         chg = - negMove
228
 
      else
229
 
         chg = posMove
230
 
      end
231
 
   end
232
 
 
233
 
   other[dir] = other[dir] + chg
234
 
end
235
 
 
236
 
function Sprite:update (elapsed)
237
 
   if self.onUpdate then self:onUpdate(elapsed) end
238
 
end
239
 
 
240
65
GameView = View:extend {
241
66
   onNew = function (self)
242
67
              self:loadLayers('data/map.lua')
243
68
              self.focus = the.player
244
69
              self:clampTo(self.map)
245
 
 
246
 
              the.recorder = Recorder:new{mousePosInterval = 9999}
247
 
              the.app.meta:add(the.recorder)
248
 
              if RECORD then
249
 
                 the.recorder:startRecording()
250
 
              elseif PLAYBACK then
251
 
                 local storage = Storage:new{filename = 'record.lua'}
252
 
                 storage:load()
253
 
                 --print(inspect(storage.data))
254
 
                 the.recorder.record = storage.data
255
 
                 the.recorder:startPlaying()
256
 
              end
257
70
           end,
258
71
   onUpdate = function (self)
259
 
                 --print('drawTook: ', the.drawTook)
260
72
                 --print('tick')
261
 
                 --the.player:collide(self.map)
 
73
                 the.player:collide(self.map)
262
74
                 --self.map:collide(the.player)
263
 
              end,
264
 
   -- draw = function (self, x, y)
265
 
   --           View.draw(self, x, y)
266
 
 
267
 
   --           love.graphics.print('FPS:' .. love.timer.getFPS(), 20, 20)
268
 
   --        end
 
75
              end
269
76
}
270
77
 
271
78
the.app = App:new {
272
79
   onRun = function (self)
273
80
              self.view = GameView:new()
274
 
              self.console:watch('onGround', 'the.player.onGround')
275
 
              self.console:watch('onWall', 'the.player.onWall')
276
 
              self.console:watch('updateTook', 'the.updateTook')
277
 
              self.console:watch('drawTook', 'the.drawTook')
278
 
              self.console:watch('recorder state', 'the.recorder.state')
279
 
 
280
 
              --the.profiler = newProfiler('time', 2000)
281
 
              --the.profiler = newProfiler()
282
 
              --the.profiler:start()
283
81
           end,
284
82
   onUpdate = function (self, dt)
285
 
                 if the.keys:justPressed('escape') then
286
 
                    if the.profiler then
287
 
                       the.profiler:stop()
288
 
                       local outfile = io.open( "profile.txt", "w+" )
289
 
                       the.profiler:report( outfile )
290
 
                       outfile:close()
291
 
                    end
292
 
 
293
 
                    if RECORD then
294
 
                       if not love.filesystem.remove('record.lua') then
295
 
                          error('could not remove record.lua')
296
 
                       end
297
 
                       local storage = Storage:new{
298
 
                          data = the.recorder.record,
299
 
                          filename = 'record.lua'
300
 
                       }
301
 
                       storage:save(false)
302
 
                       --print(inspect(the.recorder.record))
303
 
                    end
304
 
 
305
 
                    self.quit()
306
 
                 end
307
 
              end,
308
 
   update = function (self, dt)
309
 
               the.updateStart = love.timer.getMicroTime()
310
 
               App.update(self, dt)
311
 
               if the.updateStart then
312
 
                  the.updateTook = love.timer.getMicroTime() - the.updateStart
313
 
               end
314
 
            end
315
 
}
 
83
                 -- apparently I can do this w/ C-A-q if DEBUG is on
 
84
                 -- if the.keys:justPressed('escape') then
 
85
                 --    love.event.quit()
 
86
                 -- end
 
87
              end
 
88
}
 
 
b'\\ No newline at end of file'