/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-20 01:30:46 UTC
  • Revision ID: josh@9ix.org-20130320013046-da2g35k1jzxdihlz
fps indicator, maybe a new tile

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',
18
31
   },
19
32
   collisions = {},
20
33
   onWall = false,
 
34
   leftWallAt = 0,
21
35
   onNew = function (self)
22
36
              self.velocity.y = 0
23
37
              self.maxVelocity.y = 400
63
77
                  if minVel[dir] and vel[dir] < minVel[dir] then vel[dir] = minVel[dir] end
64
78
                  if maxVel[dir] and vel[dir] > maxVel[dir] then vel[dir] = maxVel[dir] end
65
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
 
66
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
67
93
               end,
68
94
   onStartFrame = function (self)
69
95
                     -- this is all in startframe so it happens before
102
128
                     if the.keys:pressed('left') then
103
129
                        self.velocity.x = -200
104
130
                        if self.onGround then self:play('walk') end
105
 
                        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
106
135
                     elseif the.keys:pressed('right') then
107
136
                        self.velocity.x = 200
108
137
                        if self.onGround then self:play('walk') end
109
 
                        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
110
142
                     else
111
143
                        if not self.onWall then
112
144
                           if self.onGround then self:play('stand') end
114
146
                        end
115
147
                     end
116
148
 
117
 
                     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
118
152
                        self.velocity.y = -400
119
153
                        self.jumping = true
120
154
                     end
182
216
      end
183
217
   else
184
218
      -- handle sprites
185
 
      local dim
186
 
      if dir == 'x' then
187
 
         dim = 'width'
188
 
      elseif dir == 'y' then
189
 
         dim = 'height'
190
 
      else
191
 
         print 'dir ??'
192
 
      end
 
219
      local dim = util.dim(dir)
193
220
 
194
221
      local negMove = (other[dir] - self[dir]) + other[dim]
195
222
      local posMove = (self[dir] + self[dim]) - other[dir]
205
232
   other[dir] = other[dir] + chg
206
233
end
207
234
 
 
235
-- don't use zoetrope physics
 
236
function Sprite:update (elapsed)
 
237
   if self.onUpdate then self:onUpdate(elapsed) end
 
238
end
 
239
 
208
240
GameView = View:extend {
209
241
   onNew = function (self)
210
242
              self:loadLayers('data/map.lua')
212
244
              self:clampTo(self.map)
213
245
           end,
214
246
   onUpdate = function (self)
 
247
                 --print('drawTook: ', the.drawTook)
215
248
                 --print('tick')
216
249
                 --the.player:collide(self.map)
217
250
                 --self.map:collide(the.player)
218
 
              end
 
251
              end,
 
252
   draw = function (self, x, y)
 
253
             View.draw(self, x, y)
 
254
 
 
255
             love.graphics.print('FPS:' .. love.timer.getFPS(), 20, 20)
 
256
          end
219
257
}
220
258
 
221
259
the.app = App:new {
223
261
              self.view = GameView:new()
224
262
              self.console:watch('onGround', 'the.player.onGround')
225
263
              self.console:watch('onWall', 'the.player.onWall')
 
264
              self.console:watch('updateTook', 'the.updateTook')
 
265
              self.console:watch('drawTook', 'the.drawTook')
 
266
 
 
267
              --the.profiler = newProfiler('time', 2000)
 
268
              --the.profiler = newProfiler()
 
269
              --the.profiler:start()
226
270
           end,
227
271
   onUpdate = function (self, dt)
228
 
                 if the.keys:justPressed('escape') and 
229
 
                   not self.console.visible then
 
272
                 if the.keys:justPressed('escape') then
 
273
                    if the.profiler then
 
274
                       the.profiler:stop()
 
275
                       local outfile = io.open( "profile.txt", "w+" )
 
276
                       the.profiler:report( outfile )
 
277
                       outfile:close()
 
278
                    end
 
279
 
230
280
                    self.quit()
231
281
                 end
232
 
              end
233
 
}
 
 
b'\\ No newline at end of file'
 
282
              end,
 
283
   update = function (self, dt)
 
284
               the.updateStart = love.timer.getMicroTime()
 
285
               App.update(self, dt)
 
286
               if the.updateStart then
 
287
                  the.updateTook = love.timer.getMicroTime() - the.updateStart
 
288
               end
 
289
            end
 
290
}