/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-10 21:55:44 UTC
  • Revision ID: josh@9ix.org-20130310215544-iqzp3x2l10h9247f
reorganize code - separate X and Y physics so we can collide in each 
direction separately

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
DEBUG = true
3
3
 
4
4
require 'zoetrope'
5
 
__ = require 'underscore'
 
5
_ = require 'underscore'
6
6
--inspect = require 'inspect'
7
 
require 'pepperprof'
8
 
require 'getopt_alt'
9
 
 
10
 
util = {
11
 
   dim = function(dir)
12
 
      if dir == 'x' then
13
 
         return 'width'
14
 
      elseif dir == 'y' then
15
 
         return 'height'
16
 
      else
17
 
         if STRICT then error('dir '..dir) end
18
 
      end
19
 
   end
 
7
 
 
8
Player = Animation:extend {
 
9
   image = 'data/player.png',
 
10
   height = 32,
 
11
   width = 32,
 
12
   sequences = {
 
13
      stand = { frames = { 1 }, fps = 1 },
 
14
      walk = { frames = { 2, 3 }, fps = 5 },
 
15
      jump = { frames = { 4 }, fps = 1 }
 
16
   },
 
17
   onNew = function (self)
 
18
              self.velocity.y = 0
 
19
              self.maxVelocity.y = 400
 
20
           end,
 
21
   doPhysics = function (self, dir, elapsed)
 
22
                  local vel = self.velocity
 
23
                  local acc = self.acceleration
 
24
                  local drag = self.drag
 
25
                  local minVel = self.minVelocity
 
26
                  local maxVel = self.maxVelocity
 
27
 
 
28
                  -- check existence of properties
 
29
 
 
30
                  if STRICT then
 
31
                     assert(vel, 'active sprite has no velocity property')
 
32
                     assert(acc, 'active sprite has no acceleration property')
 
33
                     assert(drag, 'active sprite has no drag property')
 
34
                     assert(minVel, 'active sprite has no minVelocity property')
 
35
                     assert(maxVel, 'active sprite has no maxVelocity property')
 
36
                     assert(_.include({'x','y','rotation'}, dir), 'direction should be x, y, or rotation')
 
37
                  end
 
38
 
 
39
                  vel.x = vel.x or 0
 
40
                  vel.y = vel.y or 0
 
41
                  vel.rotation = vel.rotation or 0
 
42
 
 
43
                  -- physics
 
44
 
 
45
                  if acc[dir] and acc[dir] ~= 0 then
 
46
                     vel[dir] = vel[dir] + acc[dir] * elapsed
 
47
                  else
 
48
                     if drag[dir] then
 
49
                        if vel[dir] > 0 then
 
50
                           vel[dir] = vel[dir] - drag[dir] * elapsed
 
51
                           if vel[dir] < 0 then vel[dir] = 0 end
 
52
                        elseif vel[dir] < 0 then
 
53
                           vel[dir] = vel[dir] + drag[dir] * elapsed
 
54
                           if vel[dir] > 0 then vel[dir] = 0 end
 
55
                        end
 
56
                     end
 
57
                  end
 
58
 
 
59
                  if minVel[dir] and vel[dir] < minVel[dir] then vel[dir] = minVel[dir] end
 
60
                  if maxVel[dir] and vel[dir] > maxVel[dir] then vel[dir] = maxVel[dir] end
 
61
 
 
62
                  if vel[dir] ~= 0 then self[dir] = self[dir] + vel[dir] * elapsed end
 
63
               end,
 
64
   onStartFrame = function (self)
 
65
                     -- this is all in startframe so it happens before
 
66
                     -- physics calc at beginning of update
 
67
 
 
68
                     -- jumping/falling updates could go in EndFrame...
 
69
                     self.falling = self.velocity.y > 0
 
70
                     if self.falling then self.jumping = false end
 
71
                     --print(self.jumping, self.falling)
 
72
 
 
73
                     if not self.onGround then
 
74
                        self:play('jump')
 
75
                     end
 
76
 
 
77
                     self.velocity.x = 0
 
78
                     self.acceleration.y = 800
 
79
 
 
80
                     if the.keys:pressed('left') then
 
81
                        self.velocity.x = -200
 
82
                        if self.onGround then self:play('walk') end
 
83
                     elseif the.keys:pressed('right') then
 
84
                        self.velocity.x = 200
 
85
                        if self.onGround then self:play('walk') end
 
86
                     else
 
87
                        if self.onGround then self:play('stand') end
 
88
                     end
 
89
 
 
90
                     if the.keys:justPressed('up') and self.onGround then
 
91
                        self.velocity.y = -400
 
92
                        self.jumping = true
 
93
                     end
 
94
                  end,
 
95
   update = function (self, elapsed)
 
96
               -- NOTE: this is an override, not a callback
 
97
 
 
98
               self:doPhysics('x', elapsed)
 
99
 
 
100
               self.onGround = false -- right before Y collision callbacks
 
101
               self:doPhysics('y', elapsed)
 
102
 
 
103
               self:collide(the.view.map)
 
104
            end,
 
105
   onCollide = function (self, other, xOverlap, yOverlap)
 
106
                  -- seriously, why does this even fire?
 
107
                  if other == the.view.map then return end
 
108
 
 
109
                  --print(string.format('col s{x=%i y=%i w=%i h=%i} %s', self.x, self.y, self.width, self.height, tostring(other)))
 
110
                  --print('vel.x:'..self.velocity.x.." vel.y:"..self.velocity.y)
 
111
 
 
112
                  -- assumption: any other collision is with a solid map tile
 
113
                  if yOverlap > xOverlap then
 
114
                     other:displace(self)
 
115
 
 
116
                     if self.velocity.x > 0 then
 
117
                        self.onWall = 'right'
 
118
                     elseif self.velocity.x < 0 then
 
119
                        self.onWall = 'left'
 
120
                     else
 
121
                        print 'x ??'
 
122
                     end
 
123
                  elseif xOverlap > yOverlap then
 
124
                     -- check if we've moved since collisions were generated
 
125
                     local xov, yov = self:overlap(other.x, other.y,
 
126
                                                   other.width, other.height)
 
127
                     if xov ~= 0 and yov ~= 0 then
 
128
                        --print('y collision')
 
129
                        if self.velocity.y > 0 then
 
130
                           self.onGround = true
 
131
                        end
 
132
 
 
133
                        self.velocity.y = 0
 
134
                        other:displace(self)
 
135
                        self.jumping = false
 
136
                     end
 
137
                  else
 
138
                     print('xy ??')
 
139
                  end
 
140
 
 
141
               end,
20
142
}
21
143
 
22
144
GameView = View:extend {
24
146
              self:loadLayers('data/map.lua')
25
147
              self.focus = the.player
26
148
              self:clampTo(self.map)
27
 
 
28
 
              the.recorder = Recorder:new{mousePosInterval = 9999}
29
 
              the.app.meta:add(the.recorder)
30
 
              if the.app.record then
31
 
                 the.recorder:startRecording()
32
 
              elseif the.app.playback then
33
 
                 local storage = Storage:new{filename = 'record.lua'}
34
 
                 storage:load()
35
 
                 --print(inspect(storage.data))
36
 
                 the.recorder.record = storage.data
37
 
                 the.recorder:startPlaying()
38
 
              end
39
149
           end,
40
150
   onUpdate = function (self)
41
 
                 --print('drawTook: ', the.drawTook)
42
151
                 --print('tick')
43
152
                 --the.player:collide(self.map)
44
153
                 --self.map:collide(the.player)
45
 
              end,
46
 
   -- draw = function (self, x, y)
47
 
   --           View.draw(self, x, y)
48
 
 
49
 
   --           love.graphics.print('FPS:' .. love.timer.getFPS(), 20, 20)
50
 
   --        end
 
154
              end
51
155
}
52
156
 
53
157
the.app = App:new {
54
 
   record = true,
55
158
   onRun = function (self)
56
159
              self.view = GameView:new()
 
160
              --print(inspect(_(the.app):keys()))
57
161
              self.console:watch('onGround', 'the.player.onGround')
58
162
              self.console:watch('onWall', 'the.player.onWall')
59
 
              self.console:watch('updateTook', 'the.updateTook')
60
 
              self.console:watch('drawTook', 'the.drawTook')
61
 
              self.console:watch('recorder state', 'the.recorder.state')
62
 
 
63
 
              --the.profiler = newProfiler('time', 2000)
64
 
              --the.profiler = newProfiler()
65
 
              --the.profiler:start()
66
163
           end,
67
164
   onUpdate = function (self, dt)
68
 
                 if the.keys:justPressed('escape') then
69
 
                    if the.profiler then
70
 
                       the.profiler:stop()
71
 
                       local outfile = io.open( "profile.txt", "w+" )
72
 
                       the.profiler:report( outfile )
73
 
                       outfile:close()
74
 
                    end
75
 
 
76
 
                    if self.record then
77
 
                       if not love.filesystem.remove('record.lua') then
78
 
                          print('could not remove record.lua')
79
 
                       end
80
 
                       local storage = Storage:new{
81
 
                          data = the.recorder.record,
82
 
                          filename = 'record.lua'
83
 
                       }
84
 
                       storage:save(false)
85
 
                       --print(inspect(the.recorder.record))
86
 
                    end
87
 
 
 
165
                 if the.keys:justPressed('escape') and 
 
166
                   not self.console.visible then
88
167
                    self.quit()
89
168
                 end
90
 
              end,
91
 
   update = function (self, dt)
92
 
               the.updateStart = love.timer.getMicroTime()
93
 
               App.update(self, dt)
94
 
               if the.updateStart then
95
 
                  the.updateTook = love.timer.getMicroTime() - the.updateStart
96
 
               end
97
 
            end
98
 
}
99
 
 
100
 
function love.load (arg)
101
 
   opts = getopt(arg, '')
102
 
   if opts['p'] then
103
 
      the.app.playback = true
104
 
      the.app.record = false
105
 
   elseif opts['r'] then
106
 
      the.app.record = true
107
 
   end
108
 
 
109
 
   the.app:run()
110
 
end
 
 
b'\\ No newline at end of file'
 
169
              end
 
170
}
 
 
b'\\ No newline at end of file'