/zoeplat

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