/ld27

To get this branch, use:
bzr branch /bzr/ld27

« back to all changes in this revision

Viewing changes to zoetrope/sprites/animation.lua

  • Committer: Josh C
  • Date: 2014-07-03 14:41:57 UTC
  • Revision ID: josh@9ix.org-20140703144157-xydxt62xzcdq6bdk
cluke009 zoetrope + my spritebatch changes

Show diffs side-by-side

added added

removed removed

7
7
-- Event: onEndSequence
8
8
-- Called whenever an animation sequence ends. It is passed the name
9
9
-- of the sequence that just ended.
 
10
--
10
11
-- Extends:
11
12
--              <Sprite>
12
13
 
13
 
Animation = Sprite:extend{
 
14
Animation = Sprite:extend
 
15
{
14
16
        -- Property: paused
15
17
        -- Set this to true to freeze the animation on the current frame.
16
18
        paused = false,
62
63
                return obj
63
64
        end,
64
65
 
65
 
        -- Method: play 
 
66
        -- Method: play
66
67
        -- Begins playing an animation in the sprite's library.
67
68
        -- If the animation is already playing, this has no effect.
68
69
        --
76
77
                if self.currentName == name and not self.paused then
77
78
                        return
78
79
                end
79
 
                
 
80
 
80
81
                assert(self.sequences[name], 'no animation sequence named "' .. name .. '"')
81
 
                
 
82
 
82
83
                self.currentName = name
83
84
                self.currentSequence = self.sequences[name]
84
85
                self.frameIndex = 0
101
102
                if self.currentSequence then
102
103
                        index = index or self.currentSequence[self.frameIndex]
103
104
                end
104
 
                
 
105
 
105
106
                index = index or self.currentFrame or 1
106
107
 
107
108
                if self._set.image ~= self.image then
124
125
        --              nothing
125
126
 
126
127
        updateQuad = function (self)
127
 
                if self.image then 
 
128
                if self.image then
128
129
                        self._imageObj = Cached:image(self.image)
129
130
                        if not self.width then self.width = self._imageObj:getHeight() end
130
131
                        if not self.height then self.height = self.width end
165
166
 
166
167
                if self.currentSequence and not self.paused then
167
168
                        self.frameTimer = self.frameTimer - elapsed
168
 
                        
 
169
 
169
170
                        if self.frameTimer <= 0 then
170
171
                                self.frameIndex = self.frameIndex + 1
171
172
 
201
202
                end
202
203
 
203
204
                if not self.visible or not self.image or self.alpha <= 0 then return end
204
 
                
 
205
 
205
206
                -- if our image changed, update the quad
206
 
                
 
207
 
207
208
                if self._set.image ~= self.image then
208
209
                        self:updateQuad()
209
210
                end
210
 
                
 
211
 
211
212
                -- set color if needed
212
213
 
213
214
                local colored = self.alpha ~= 1 or self.tint[1] ~= 1 or self.tint[2] ~= 1 or self.tint[3] ~= 1
223
224
 
224
225
                if self.flipX then scaleX = scaleX * -1 end
225
226
                if self.flipY then scaleY = scaleY * -1 end
226
 
                        
227
 
                love.graphics.drawq(self._imageObj, self._quad, x + self.width / 2, y + self.height / 2, self.rotation,
228
 
                                                        scaleX, scaleY, self.width / 2, self.height / 2)
229
 
                
 
227
 
 
228
                local origX = self.origin.x or (self.width / 2)
 
229
                local origY = self.origin.y or (self.height / 2)
 
230
 
 
231
                love.graphics.draw(self._imageObj, self._quad, x + origX, y + origY, self.rotation,
 
232
                                                        scaleX, scaleY, origX, origY)
 
233
 
230
234
                -- reset color
231
 
                
 
235
 
232
236
                if colored then
233
237
                        love.graphics.setColor(255, 255, 255, 255)
234
238
                end
235
239
        end,
236
240
 
237
241
        __tostring = function (self)
238
 
                local result = 'Animation (x: ' .. self.x .. ', y: ' .. self.y ..
239
 
                                           ', w: ' .. self.width .. ', h: ' .. self.height .. ', '
 
242
                local result = 'Animation (x: ' .. tostring(self.x) .. ', y: ' .. tostring(self.y) ..
 
243
                                           ', w: ' .. tostring(self.width) .. ', h: ' .. tostring(self.height) .. ', '
240
244
 
241
245
                if self.currentName then
242
246
                        result = result .. 'playing ' .. self.currentName .. ', '
243
247
                end
244
248
 
245
 
                result = result .. ' frame ' .. self.currentFrame .. ', '
 
249
                result = result .. ' frame ' .. tostring(self.currentFrame) .. ', '
246
250
 
247
251
                if self.active then
248
252
                        result = result .. 'active, '