/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 23:47:59 UTC
  • Revision ID: josh@9ix.org-20130410234759-nrzne3xed1puy1v2
put sprite and player in their own files

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
   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
 
75
                      (self.onGround or the.console.visible or
 
76
                       (love.timer.getTime() - self.leftWallAt < .1) ) then
 
77
                        self.velocity.y = -400
 
78
                        self.jumping = true
 
79
                     end
 
80
                  end,
 
81
   update = function (self, elapsed)
 
82
               -- NOTE: this is an override, not a callback
 
83
 
 
84
               self:doPhysics('x', elapsed)
 
85
               self:collide(the.view.map)
 
86
 
 
87
               -- handle X collisions
 
88
               self.onWall = false
 
89
               for _, col in ipairs(self.collisions) do
 
90
                  col.other:displaceDir(self, 'x')
 
91
                  if self.velocity.x > 0 then
 
92
                     self.onWall = 'right'
 
93
                  elseif self.velocity.x < 0 then
 
94
                     self.onWall = 'left'
 
95
                  else
 
96
                     print 'x ??'
 
97
                  end
 
98
               end
 
99
 
 
100
               self.onGround = false -- right before Y collision callbacks
 
101
               self:doPhysics('y', elapsed)
 
102
               self:collide(the.view.map)
 
103
 
 
104
               -- handle Y collisions
 
105
               for _, col in ipairs(self.collisions) do
 
106
                  if self.velocity.y > 0 then
 
107
                     self.onGround = true
 
108
                  end
 
109
 
 
110
                  col.other:displaceDir(self, 'y')
 
111
                  self.velocity.y = 0
 
112
                  self.jumping = false
 
113
               end
 
114
 
 
115
               -- text blob
 
116
               if not self.text then
 
117
                  self.text = Text:new{wordWrap = true, width = 50, tint = {0,0,0}}
 
118
                  self.textfill = Fill:new{width = 54, border = {0,0,255}}
 
119
                  --the.view:add(self.textfill)
 
120
                  --the.view:add(self.text)
 
121
               end
 
122
               self.text.text = "Blah blah big text etc etc wrapping"
 
123
               self.text:centerAround(self.x+16, self.y+16, 'horizontal')
 
124
               _, texth = self.text:getSize()
 
125
               self.text.y = self.y - texth - 4
 
126
               self.textfill.x = self.text.x - 2
 
127
               self.textfill.y = self.text.y - 2
 
128
               self.textfill.height = texth + 4
 
129
 
 
130
               Animation.update(self, elapsed)
 
131
            end,
 
132
   collide = function (self, ...)
 
133
                self.collisions = {}
 
134
                Animation.collide(self, ...)
 
135
                -- I could return a true/false value here if I wanted to...
 
136
             end,
 
137
   onCollide = function (self, other, xOverlap, yOverlap)
 
138
                  if other == the.view.map then return end
 
139
 
 
140
                  table.insert(self.collisions, {other = other,
 
141
                                                 xOverlap = xOverlap,
 
142
                                                 yOverlap = yOverlap })
 
143
               end
 
144
}
 
145