/zoeplat

To get this branch, use:
bzr branch http://9ix.org/bzr/zoeplat

« back to all changes in this revision

Viewing changes to player.lua

  • Committer: Josh C
  • Date: 2013-04-10 01:20:40 UTC
  • Revision ID: josh@9ix.org-20130410012040-lv7v1sc3vj7ismja
link

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Player = Animation:extend {
2
 
   image = 'data/player.png',
3
 
   height = 32,
4
 
   width = 32,
5
 
   sequences = {
6
 
      stand = { frames = { 1 }, fps = 1 },
7
 
      walk = { frames = { 2, 3 }, fps = 5 },
8
 
      jump = { frames = { 4 }, fps = 1 },
9
 
      climbLeft = { frames = { 5, 6 }, fps = 5 },
10
 
      climbRight = { frames = { 7, 8 }, fps = 5 }
11
 
   },
12
 
   collisions = {},
13
 
   onWall = false,
14
 
   leftWallAt = 0,
15
 
   jump = love.audio.newSource('data/jump.ogg', 'static'),
16
 
   onNew = function (self)
17
 
              self.velocity.y = 0
18
 
              self.maxVelocity.y = 400
19
 
           end,
20
 
   onStartFrame = function (self)
21
 
                     -- this is all in startframe so it happens before
22
 
                     -- physics calc at beginning of update
23
 
 
24
 
                     -- jumping/falling updates could go in EndFrame...
25
 
                     self.falling = self.velocity.y > 0
26
 
                     if self.falling then self.jumping = false end
27
 
                     --print(self.jumping, self.falling)
28
 
 
29
 
                     if (not self.onGround) and (not self.onWall) then
30
 
                        self:play('jump')
31
 
                     end
32
 
 
33
 
                     self.acceleration.y = 800
34
 
 
35
 
                     if self.onWall then
36
 
                        self.acceleration.y = 0
37
 
 
38
 
                        if self.onWall == 'right' then
39
 
                           self:play('climbRight')
40
 
                        elseif self.onWall == 'left' then
41
 
                           self:play('climbLeft')
42
 
                        end
43
 
 
44
 
                        if the.keys:pressed('up') then
45
 
                           self.velocity.y = -200
46
 
                        elseif the.keys:pressed('down') then
47
 
                           self.velocity.y = 200
48
 
                        else
49
 
                           self.velocity.y = 0
50
 
                           self:freeze(self.sequences[self.currentName].frames[1])
51
 
                        end
52
 
                     end
53
 
 
54
 
                     if the.keys:pressed('left') then
55
 
                        self.velocity.x = -200
56
 
                        if self.onGround then self:play('walk') end
57
 
                        if self.onWall == 'right' then
58
 
                           self.onWall = false
59
 
                           self.leftWallAt = love.timer.getTime()
60
 
                        end
61
 
                     elseif the.keys:pressed('right') then
62
 
                        self.velocity.x = 200
63
 
                        if self.onGround then self:play('walk') end
64
 
                        if self.onWall == 'left' then
65
 
                           self.onWall = false
66
 
                           self.leftWallAt = love.timer.getTime()
67
 
                        end
68
 
                     else
69
 
                        if not self.onWall then
70
 
                           if self.onGround then self:play('stand') end
71
 
                           self.velocity.x = 0
72
 
                        end
73
 
                     end
74
 
 
75
 
                     if the.keys:justPressed('up') and
76
 
                      (self.onGround or
77
 
                       (DEBUG and the.console.visible) or
78
 
                       (love.timer.getTime() - self.leftWallAt < .1) ) then
79
 
                        self.velocity.y = -400
80
 
                        self.jumping = true
81
 
                        --love.audio.play(self.jump)
82
 
                     end
83
 
                  end,
84
 
   update = function (self, elapsed)
85
 
               -- NOTE: this is an override, not a callback
86
 
 
87
 
               self:doPhysics('x', elapsed)
88
 
               self:collide(the.view.map)
89
 
 
90
 
               -- handle X collisions
91
 
               self.onWall = false
92
 
               for _, col in ipairs(self.collisions) do
93
 
                  col.other:displaceDir(self, 'x')
94
 
                  if self.velocity.x > 0 then
95
 
                     self.onWall = 'right'
96
 
                  elseif self.velocity.x < 0 then
97
 
                     self.onWall = 'left'
98
 
                  else
99
 
                     print 'x ??'
100
 
                  end
101
 
               end
102
 
 
103
 
               self.onGround = false -- right before Y collision callbacks
104
 
               self:doPhysics('y', elapsed)
105
 
               self:collide(the.view.map)
106
 
 
107
 
               -- handle Y collisions
108
 
               for _, col in ipairs(self.collisions) do
109
 
                  if self.velocity.y > 0 then
110
 
                     self.onGround = true
111
 
                  end
112
 
 
113
 
                  col.other:displaceDir(self, 'y')
114
 
                  self.velocity.y = 0
115
 
                  self.jumping = false
116
 
               end
117
 
 
118
 
               -- text blob
119
 
               if not self.text then
120
 
                  self.text = Text:new{wordWrap = true, width = 50, tint = {0,0,0}}
121
 
                  self.textfill = Fill:new{width = 54, border = {0,0,255}}
122
 
                  --the.view:add(self.textfill)
123
 
                  --the.view:add(self.text)
124
 
               end
125
 
               self.text.text = "Blah blah big text etc etc wrapping"
126
 
               self.text:centerAround(self.x+16, self.y+16, 'horizontal')
127
 
               _, texth = self.text:getSize()
128
 
               self.text.y = self.y - texth - 4
129
 
               self.textfill.x = self.text.x - 2
130
 
               self.textfill.y = self.text.y - 2
131
 
               self.textfill.height = texth + 4
132
 
 
133
 
               Animation.update(self, elapsed)
134
 
            end,
135
 
   collide = function (self, ...)
136
 
                self.collisions = {}
137
 
                Animation.collide(self, ...)
138
 
                -- I could return a true/false value here if I wanted to...
139
 
             end,
140
 
   onCollide = function (self, other, xOverlap, yOverlap)
141
 
                  if other == the.view.map then return end
142
 
 
143
 
                  table.insert(self.collisions, {other = other,
144
 
                                                 xOverlap = xOverlap,
145
 
                                                 yOverlap = yOverlap })
146
 
               end
147
 
}
148