/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:
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
78
62
                  if maxVel[dir] and vel[dir] > maxVel[dir] then vel[dir] = maxVel[dir] end
79
63
 
80
64
                  if vel[dir] ~= 0 then self[dir] = self[dir] + vel[dir] * elapsed end
81
 
 
82
 
                  if self[dir] < 0 then self[dir] = 0 end
83
 
                  local edge = the.view.map[util.dim(dir)] -
84
 
                               the.player[util.dim(dir)]
85
 
                  -- TODO: take map position into account
86
 
                  if self[dir] > edge then self[dir] = edge end
87
65
               end,
88
66
   onStartFrame = function (self)
89
67
                     -- this is all in startframe so it happens before
98
76
                        self:play('jump')
99
77
                     end
100
78
 
 
79
                     self.velocity.x = 0
101
80
                     self.acceleration.y = 800
102
81
 
103
82
                     if self.onWall then
104
83
                        self.acceleration.y = 0
105
84
 
106
 
                        if self.onWall == 'right' then
107
 
                           self:play('climbRight')
108
 
                        elseif self.onWall == 'left' then
109
 
                           self:play('climbLeft')
110
 
                        end
111
 
 
112
85
                        if the.keys:pressed('up') then
113
86
                           self.velocity.y = -200
 
87
                           self:play('stand')
114
88
                        elseif the.keys:pressed('down') then
115
89
                           self.velocity.y = 200
 
90
                           self:play('stand')
116
91
                        else
117
92
                           self.velocity.y = 0
118
 
                           self:freeze(self.sequences[self.currentName].frames[1])
 
93
                           self:play('stand')
119
94
                        end
120
95
                     end
121
96
 
122
97
                     if the.keys:pressed('left') then
123
98
                        self.velocity.x = -200
124
99
                        if self.onGround then self:play('walk') end
125
 
                        if self.onWall == 'right' then
126
 
                           self.onWall = false
127
 
                           self.leftWallAt = love.timer.getTime()
128
 
                        end
 
100
                        if self.onWall == 'right' then self.onWall = false end
 
101
                        if self.onWall == 'right' then self.onWall = false end
129
102
                     elseif the.keys:pressed('right') then
130
103
                        self.velocity.x = 200
131
104
                        if self.onGround then self:play('walk') end
132
 
                        if self.onWall == 'left' then
133
 
                           self.onWall = false
134
 
                           self.leftWallAt = love.timer.getTime()
135
 
                        end
 
105
                        if self.onWall == 'left' then self.onWall = false end
136
106
                     else
137
 
                        if not self.onWall then
138
 
                           if self.onGround then self:play('stand') end
139
 
                           self.velocity.x = 0
140
 
                        end
 
107
                        if self.onGround then self:play('stand') end
141
108
                     end
142
109
 
143
 
                     if the.keys:justPressed('up') and
144
 
                      (self.onGround or the.console.visible or
145
 
                       (love.timer.getTime() - self.leftWallAt < .1) ) then
 
110
                     if the.keys:justPressed('up') and self.onGround then
146
111
                        self.velocity.y = -400
147
112
                        self.jumping = true
148
113
                     end
154
119
               self:collide(the.view.map)
155
120
 
156
121
               -- handle X collisions
157
 
               self.onWall = false
158
122
               for _, col in ipairs(self.collisions) do
159
123
                  col.other:displaceDir(self, 'x')
160
124
                  if self.velocity.x > 0 then
210
174
      end
211
175
   else
212
176
      -- handle sprites
213
 
      local dim = util.dim(dir)
 
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
214
185
 
215
186
      local negMove = (other[dir] - self[dir]) + other[dim]
216
187
      local posMove = (self[dir] + self[dim]) - other[dir]
244
215
              self.view = GameView:new()
245
216
              self.console:watch('onGround', 'the.player.onGround')
246
217
              self.console:watch('onWall', 'the.player.onWall')
247
 
              self.console:watch('updateTook', 'the.updateTook')
248
 
              self.console:watch('drawTook', 'the.drawTook')
249
 
 
250
 
              --the.profiler = newProfiler('time', 2000)
251
 
              --the.profiler = newProfiler()
252
 
              --the.profiler:start()
253
218
           end,
254
219
   onUpdate = function (self, dt)
255
 
                 if the.keys:justPressed('escape') then
256
 
                    if the.profiler then
257
 
                       the.profiler:stop()
258
 
                       local outfile = io.open( "profile.txt", "w+" )
259
 
                       the.profiler:report( outfile )
260
 
                       outfile:close()
261
 
                    end
262
 
 
 
220
                 if the.keys:justPressed('escape') and 
 
221
                   not self.console.visible then
263
222
                    self.quit()
264
223
                 end
265
 
              end,
266
 
   update = function (self, dt)
267
 
               the.updateStart = love.timer.getMicroTime()
268
 
               App.update(self, dt)
269
 
               if the.updateStart then
270
 
                  the.updateTook = love.timer.getMicroTime() - the.updateStart
271
 
               end
272
 
            end
273
 
}
 
224
              end
 
225
}
 
 
b'\\ No newline at end of file'