/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-17 21:23:13 UTC
  • Revision ID: josh@9ix.org-20130317212313-gsmwb0yt9hxdjj68
hack to not fall through floor on long first tick, monkey patch to turn 
off zoetrope physics (reliably, without depending on patched zoetrope)

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
require 'zoetrope'
5
5
__ = require 'underscore'
6
6
--inspect = require 'inspect'
 
7
require 'pepperprof'
 
8
 
 
9
util = {
 
10
   dim = function(dir)
 
11
      if dir == 'x' then
 
12
         return 'width'
 
13
      elseif dir == 'y' then
 
14
         return 'height'
 
15
      else
 
16
         print 'dir ??'
 
17
      end
 
18
   end
 
19
}
7
20
 
8
21
Player = Animation:extend {
9
22
   image = 'data/player.png',
12
25
   sequences = {
13
26
      stand = { frames = { 1 }, fps = 1 },
14
27
      walk = { frames = { 2, 3 }, fps = 5 },
15
 
      jump = { frames = { 4 }, fps = 1 }
 
28
      jump = { frames = { 4 }, fps = 1 },
 
29
      climbLeft = { frames = { 5, 6 }, fps = 5 },
 
30
      climbRight = { frames = { 7, 8 }, fps = 5 }
16
31
   },
17
32
   collisions = {},
18
33
   onWall = false,
 
34
   leftWallAt = 0,
19
35
   onNew = function (self)
20
36
              self.velocity.y = 0
21
37
              self.maxVelocity.y = 400
61
77
                  if minVel[dir] and vel[dir] < minVel[dir] then vel[dir] = minVel[dir] end
62
78
                  if maxVel[dir] and vel[dir] > maxVel[dir] then vel[dir] = maxVel[dir] end
63
79
 
 
80
                  -- ugly hack for falling through floor on really slow frames
 
81
                  if math.abs(vel[dir] * elapsed) > 32 then
 
82
                     print('skip')
 
83
                     return
 
84
                  end
 
85
 
64
86
                  if vel[dir] ~= 0 then self[dir] = self[dir] + vel[dir] * elapsed end
 
87
 
 
88
                  if self[dir] < 0 then self[dir] = 0 end
 
89
                  local edge = the.view.map[util.dim(dir)] -
 
90
                               the.player[util.dim(dir)]
 
91
                  -- TODO: take map position into account
 
92
                  if self[dir] > edge then self[dir] = edge end
65
93
               end,
66
94
   onStartFrame = function (self)
67
95
                     -- this is all in startframe so it happens before
81
109
                     if self.onWall then
82
110
                        self.acceleration.y = 0
83
111
 
 
112
                        if self.onWall == 'right' then
 
113
                           self:play('climbRight')
 
114
                        elseif self.onWall == 'left' then
 
115
                           self:play('climbLeft')
 
116
                        end
 
117
 
84
118
                        if the.keys:pressed('up') then
85
119
                           self.velocity.y = -200
86
 
                           self:play('stand')
87
120
                        elseif the.keys:pressed('down') then
88
121
                           self.velocity.y = 200
89
 
                           self:play('stand')
90
122
                        else
91
123
                           self.velocity.y = 0
92
 
                           self:play('stand')
 
124
                           self:freeze(self.sequences[self.currentName].frames[1])
93
125
                        end
94
126
                     end
95
127
 
96
128
                     if the.keys:pressed('left') then
97
129
                        self.velocity.x = -200
98
130
                        if self.onGround then self:play('walk') end
99
 
                        if self.onWall == 'right' then self.onWall = false end
 
131
                        if self.onWall == 'right' then
 
132
                           self.onWall = false
 
133
                           self.leftWallAt = love.timer.getTime()
 
134
                        end
100
135
                     elseif the.keys:pressed('right') then
101
136
                        self.velocity.x = 200
102
137
                        if self.onGround then self:play('walk') end
103
 
                        if self.onWall == 'left' then self.onWall = false end
 
138
                        if self.onWall == 'left' then
 
139
                           self.onWall = false
 
140
                           self.leftWallAt = love.timer.getTime()
 
141
                        end
104
142
                     else
105
 
                        if self.onGround then self:play('stand') end
106
143
                        if not self.onWall then
 
144
                           if self.onGround then self:play('stand') end
107
145
                           self.velocity.x = 0
108
146
                        end
109
147
                     end
110
148
 
111
 
                     if the.keys:justPressed('up') and self.onGround then
 
149
                     if the.keys:justPressed('up') and
 
150
                      (self.onGround or the.console.visible or
 
151
                       (love.timer.getTime() - self.leftWallAt < .1) ) then
112
152
                        self.velocity.y = -400
113
153
                        self.jumping = true
114
154
                     end
176
216
      end
177
217
   else
178
218
      -- handle sprites
179
 
      local dim
180
 
      if dir == 'x' then
181
 
         dim = 'width'
182
 
      elseif dir == 'y' then
183
 
         dim = 'height'
184
 
      else
185
 
         print 'dir ??'
186
 
      end
 
219
      local dim = util.dim(dir)
187
220
 
188
221
      local negMove = (other[dir] - self[dir]) + other[dim]
189
222
      local posMove = (self[dir] + self[dim]) - other[dir]
199
232
   other[dir] = other[dir] + chg
200
233
end
201
234
 
 
235
-- don't use zoetrope physics
 
236
function Sprite:update (elapsed)
 
237
   if self.onUpdate then self:onUpdate(elapsed) end
 
238
end
 
239
 
202
240
GameView = View:extend {
203
241
   onNew = function (self)
204
242
              self:loadLayers('data/map.lua')
206
244
              self:clampTo(self.map)
207
245
           end,
208
246
   onUpdate = function (self)
 
247
                 --print('drawTook: ', the.drawTook)
209
248
                 --print('tick')
210
249
                 --the.player:collide(self.map)
211
250
                 --self.map:collide(the.player)
217
256
              self.view = GameView:new()
218
257
              self.console:watch('onGround', 'the.player.onGround')
219
258
              self.console:watch('onWall', 'the.player.onWall')
 
259
              self.console:watch('updateTook', 'the.updateTook')
 
260
              self.console:watch('drawTook', 'the.drawTook')
 
261
 
 
262
              --the.profiler = newProfiler('time', 2000)
 
263
              --the.profiler = newProfiler()
 
264
              --the.profiler:start()
220
265
           end,
221
266
   onUpdate = function (self, dt)
222
 
                 if the.keys:justPressed('escape') and 
223
 
                   not self.console.visible then
 
267
                 if the.keys:justPressed('escape') then
 
268
                    if the.profiler then
 
269
                       the.profiler:stop()
 
270
                       local outfile = io.open( "profile.txt", "w+" )
 
271
                       the.profiler:report( outfile )
 
272
                       outfile:close()
 
273
                    end
 
274
 
224
275
                    self.quit()
225
276
                 end
226
 
              end
227
 
}
 
 
b'\\ No newline at end of file'
 
277
              end,
 
278
   update = function (self, dt)
 
279
               the.updateStart = love.timer.getMicroTime()
 
280
               App.update(self, dt)
 
281
               if the.updateStart then
 
282
                  the.updateTook = love.timer.getMicroTime() - the.updateStart
 
283
               end
 
284
            end
 
285
}