/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 17:21:28 UTC
  • Revision ID: josh@9ix.org-20130312172128-l336vwqawo81c6sm
really easy version of knowing when we reached the top of a wall.  
similar to how we're doing onGround.

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
 
}
20
7
 
21
8
Player = Animation:extend {
22
9
   image = 'data/player.png',
25
12
   sequences = {
26
13
      stand = { frames = { 1 }, fps = 1 },
27
14
      walk = { frames = { 2, 3 }, fps = 5 },
28
 
      jump = { frames = { 4 }, fps = 1 },
29
 
      climbLeft = { frames = { 5, 6 }, fps = 5 },
30
 
      climbRight = { frames = { 7, 8 }, fps = 5 }
 
15
      jump = { frames = { 4 }, fps = 1 }
31
16
   },
32
17
   collisions = {},
33
18
   onWall = false,
34
 
   leftWallAt = 0,
35
19
   onNew = function (self)
36
20
              self.velocity.y = 0
37
21
              self.maxVelocity.y = 400
77
61
                  if minVel[dir] and vel[dir] < minVel[dir] then vel[dir] = minVel[dir] end
78
62
                  if maxVel[dir] and vel[dir] > maxVel[dir] then vel[dir] = maxVel[dir] end
79
63
 
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
 
 
86
64
                  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
93
65
               end,
94
66
   onStartFrame = function (self)
95
67
                     -- this is all in startframe so it happens before
109
81
                     if self.onWall then
110
82
                        self.acceleration.y = 0
111
83
 
112
 
                        if self.onWall == 'right' then
113
 
                           self:play('climbRight')
114
 
                        elseif self.onWall == 'left' then
115
 
                           self:play('climbLeft')
116
 
                        end
117
 
 
118
84
                        if the.keys:pressed('up') then
119
85
                           self.velocity.y = -200
 
86
                           self:play('stand')
120
87
                        elseif the.keys:pressed('down') then
121
88
                           self.velocity.y = 200
 
89
                           self:play('stand')
122
90
                        else
123
91
                           self.velocity.y = 0
124
 
                           self:freeze(self.sequences[self.currentName].frames[1])
 
92
                           self:play('stand')
125
93
                        end
126
94
                     end
127
95
 
128
96
                     if the.keys:pressed('left') then
129
97
                        self.velocity.x = -200
130
98
                        if self.onGround then self:play('walk') end
131
 
                        if self.onWall == 'right' then
132
 
                           self.onWall = false
133
 
                           self.leftWallAt = love.timer.getTime()
134
 
                        end
 
99
                        if self.onWall == 'right' then self.onWall = false end
135
100
                     elseif the.keys:pressed('right') then
136
101
                        self.velocity.x = 200
137
102
                        if self.onGround then self:play('walk') end
138
 
                        if self.onWall == 'left' then
139
 
                           self.onWall = false
140
 
                           self.leftWallAt = love.timer.getTime()
141
 
                        end
 
103
                        if self.onWall == 'left' then self.onWall = false end
142
104
                     else
 
105
                        if self.onGround then self:play('stand') end
143
106
                        if not self.onWall then
144
 
                           if self.onGround then self:play('stand') end
145
107
                           self.velocity.x = 0
146
108
                        end
147
109
                     end
148
110
 
149
 
                     if the.keys:justPressed('up') and
150
 
                      (self.onGround or the.console.visible or
151
 
                       (love.timer.getTime() - self.leftWallAt < .1) ) then
 
111
                     if the.keys:justPressed('up') and self.onGround then
152
112
                        self.velocity.y = -400
153
113
                        self.jumping = true
154
114
                     end
216
176
      end
217
177
   else
218
178
      -- handle sprites
219
 
      local dim = util.dim(dir)
 
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
220
187
 
221
188
      local negMove = (other[dir] - self[dir]) + other[dim]
222
189
      local posMove = (self[dir] + self[dim]) - other[dir]
232
199
   other[dir] = other[dir] + chg
233
200
end
234
201
 
235
 
function Sprite:update (elapsed)
236
 
   if self.onUpdate then self:onUpdate(elapsed) end
237
 
end
238
 
 
239
202
GameView = View:extend {
240
203
   onNew = function (self)
241
204
              self:loadLayers('data/map.lua')
244
206
              self:clampTo(self.map)
245
207
           end,
246
208
   onUpdate = function (self)
247
 
                 --print('drawTook: ', the.drawTook)
248
209
                 --print('tick')
249
210
                 --the.player:collide(self.map)
250
211
                 --self.map:collide(the.player)
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
 
212
              end
257
213
}
258
214
 
259
215
the.app = App:new {
261
217
              self.view = GameView:new()
262
218
              self.console:watch('onGround', 'the.player.onGround')
263
219
              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()
270
220
           end,
271
221
   onUpdate = function (self, dt)
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
 
 
 
222
                 if the.keys:justPressed('escape') and 
 
223
                   not self.console.visible then
280
224
                    self.quit()
281
225
                 end
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
 
}
 
226
              end
 
227
}
 
 
b'\\ No newline at end of file'