/zoeplat

To get this branch, use:
bzr branch http://9ix.org/bzr/zoeplat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
Player = Animation:extend {
   image = 'data/player.png',
   height = 32,
   width = 32,
   sequences = {
      stand = { frames = { 1 }, fps = 1 },
      walk = { frames = { 2, 3 }, fps = 5 },
      jump = { frames = { 4 }, fps = 1 },
      climbLeft = { frames = { 5, 6 }, fps = 5 },
      climbRight = { frames = { 7, 8 }, fps = 5 }
   },
   collisions = {},
   onWall = false,
   leftWallAt = 0,
   onNew = function (self)
              self.velocity.y = 0
              self.maxVelocity.y = 400
           end,
   onStartFrame = function (self)
                     -- this is all in startframe so it happens before
                     -- physics calc at beginning of update

                     -- jumping/falling updates could go in EndFrame...
                     self.falling = self.velocity.y > 0
                     if self.falling then self.jumping = false end
                     --print(self.jumping, self.falling)

                     if (not self.onGround) and (not self.onWall) then
                        self:play('jump')
                     end

                     self.acceleration.y = 800

                     if self.onWall then
                        self.acceleration.y = 0

                        if self.onWall == 'right' then
                           self:play('climbRight')
                        elseif self.onWall == 'left' then
                           self:play('climbLeft')
                        end

                        if the.keys:pressed('up') then
                           self.velocity.y = -200
                        elseif the.keys:pressed('down') then
                           self.velocity.y = 200
                        else
                           self.velocity.y = 0
                           self:freeze(self.sequences[self.currentName].frames[1])
                        end
                     end

                     if the.keys:pressed('left') then
                        self.velocity.x = -200
                        if self.onGround then self:play('walk') end
                        if self.onWall == 'right' then
                           self.onWall = false
                           self.leftWallAt = love.timer.getTime()
                        end
                     elseif the.keys:pressed('right') then
                        self.velocity.x = 200
                        if self.onGround then self:play('walk') end
                        if self.onWall == 'left' then
                           self.onWall = false
                           self.leftWallAt = love.timer.getTime()
                        end
                     else
                        if not self.onWall then
                           if self.onGround then self:play('stand') end
                           self.velocity.x = 0
                        end
                     end

                     if the.keys:justPressed('up') and
                      (self.onGround or 
                       (DEBUG and the.console.visible) or
                       (love.timer.getTime() - self.leftWallAt < .1) ) then
                        self.velocity.y = -400
                        self.jumping = true
                     end
                  end,
   update = function (self, elapsed)
               -- NOTE: this is an override, not a callback

               self:doPhysics('x', elapsed)
               self:collide(the.view.map)

               -- handle X collisions
               self.onWall = false
               for _, col in ipairs(self.collisions) do
                  col.other:displaceDir(self, 'x')
                  if self.velocity.x > 0 then
                     self.onWall = 'right'
                  elseif self.velocity.x < 0 then
                     self.onWall = 'left'
                  else
                     print 'x ??'
                  end
               end

               self.onGround = false -- right before Y collision callbacks
               self:doPhysics('y', elapsed)
               self:collide(the.view.map)

               -- handle Y collisions
               for _, col in ipairs(self.collisions) do
                  if self.velocity.y > 0 then
                     self.onGround = true
                  end

                  col.other:displaceDir(self, 'y')
                  self.velocity.y = 0
                  self.jumping = false
               end

               -- text blob
               if not self.text then
                  self.text = Text:new{wordWrap = true, width = 50, tint = {0,0,0}}
                  self.textfill = Fill:new{width = 54, border = {0,0,255}}
                  --the.view:add(self.textfill)
                  --the.view:add(self.text)
               end
               self.text.text = "Blah blah big text etc etc wrapping"
               self.text:centerAround(self.x+16, self.y+16, 'horizontal')
               _, texth = self.text:getSize()
               self.text.y = self.y - texth - 4
               self.textfill.x = self.text.x - 2
               self.textfill.y = self.text.y - 2
               self.textfill.height = texth + 4

               Animation.update(self, elapsed)
            end,
   collide = function (self, ...)
                self.collisions = {}
                Animation.collide(self, ...)
                -- I could return a true/false value here if I wanted to...
             end,
   onCollide = function (self, other, xOverlap, yOverlap)
                  if other == the.view.map then return end

                  table.insert(self.collisions, {other = other,
                                                 xOverlap = xOverlap,
                                                 yOverlap = yOverlap })
               end
}