/zoeplat

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

« back to all changes in this revision

Viewing changes to zoetrope/core/sprite.lua

  • Committer: Josh C
  • Date: 2013-03-04 23:27:03 UTC
  • Revision ID: josh@9ix.org-20130304232703-nob2mg5wbo5co5is
basic tiles, map, player, movement

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
-- Extends:
16
16
--              <Class>
17
17
--
 
18
-- Event: onDraw
 
19
-- Called after drawing takes place.
 
20
--
18
21
-- Event: onUpdate
19
22
-- Called once each frame, with the elapsed time since the last frame in seconds.
20
23
--
201
204
        end,
202
205
 
203
206
        -- Method: collide
204
 
        -- Checks whether this sprite collides with other <Sprite>s ad <Group>s. If a collision is detected,
 
207
        -- Checks whether sprites collide by checking rectangles. If a collision is detected,
205
208
        -- onCollide() is called on both this sprite and the one it collides with, passing
206
209
        -- the amount of horizontal and vertical overlap between the sprites in pixels.
207
210
        --
208
211
        -- Arguments:
209
 
        --              ... - any number of <Sprite>s or <Group>s to collide with.
 
212
        --              other - <Sprite> or <Group> to collide
210
213
        --
211
214
        -- Returns:
212
 
        --              nothing
213
 
 
214
 
        collide = function (self, ...)
215
 
                Collision:check(self, ...)
 
215
        --              boolean, whether any collision was detected
 
216
 
 
217
        collide = function (self, other)
 
218
                if not self.solid or not other.solid or self == other then return false end
 
219
 
 
220
                if other.sprites then
 
221
                        return other:collide(self)
 
222
                else
 
223
                        local xOverlap, yOverlap = self:overlap(other.x, other.y, other.width, other.height)
 
224
 
 
225
                        if xOverlap ~= 0 or yOverlap ~= 0 then  
 
226
                                if self.onCollide then
 
227
                                        self:onCollide(other, xOverlap, yOverlap)
 
228
                                end
 
229
                                
 
230
                                if other.onCollide then
 
231
                                        other:onCollide(self, xOverlap, yOverlap)
 
232
                                end
 
233
 
 
234
                                return true
 
235
                        end
 
236
                end
 
237
 
 
238
                return false
216
239
        end,
217
240
 
218
241
        -- Method: displace
434
457
        end,
435
458
 
436
459
        draw = function (self, x, y)
437
 
                -- subclasses do interesting things here
438
 
        end,
439
 
 
440
 
        collidedWith = function (self, other, xOverlap, yOverlap)
441
 
                if self.onCollide then self:onCollide(other, xOverlap, yOverlap) end
 
460
                if self.onDraw then self:onDraw(x, y) end
442
461
        end
443
462
}