/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-16 19:31:02 UTC
  • Revision ID: josh@9ix.org-20130316193102-68imraus0srbj653
don't go off the edge

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
-- Extends:
16
16
--              <Class>
17
17
--
18
 
--
19
18
-- Event: onUpdate
20
19
-- Called once each frame, with the elapsed time since the last frame in seconds.
21
20
--
204
201
        end,
205
202
 
206
203
        -- Method: collide
207
 
        -- Checks whether sprites collide by checking rectangles. If a collision is detected,
 
204
        -- Checks whether this sprite collides with other <Sprite>s ad <Group>s. If a collision is detected,
208
205
        -- onCollide() is called on both this sprite and the one it collides with, passing
209
206
        -- the amount of horizontal and vertical overlap between the sprites in pixels.
210
207
        --
211
208
        -- Arguments:
212
 
        --              other - <Sprite> or <Group> to collide
 
209
        --              ... - any number of <Sprite>s or <Group>s to collide with.
213
210
        --
214
211
        -- Returns:
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
 
212
        --              nothing
 
213
 
 
214
        collide = function (self, ...)
 
215
                Collision:check(self, ...)
239
216
        end,
240
217
 
241
218
        -- Method: displace
373
350
                if self.onStartFrame then self:onStartFrame(elapsed) end
374
351
        end,
375
352
 
376
 
        update = function (self, elapsed)
 
353
        doPhysics = function (self, elapsed)
377
354
                local vel = self.velocity
378
355
                local acc = self.acceleration
379
356
                local drag = self.drag
396
373
 
397
374
                -- physics
398
375
                        
399
 
                if vel.x ~= 0 then self.x = self.x + vel.x * elapsed end
400
 
                if vel.y ~= 0 then self.y = self.y + vel.y * elapsed end
401
 
                if vel.rotation ~= 0 then self.rotation = self.rotation + vel.rotation * elapsed end
402
 
                
403
376
                if acc.x and acc.x ~= 0 then
404
377
                        vel.x = vel.x + acc.x * elapsed
405
378
                else
448
421
                if maxVel.y and vel.y > maxVel.y then vel.y = maxVel.y end
449
422
                if minVel.rotation and vel.rotation < minVel.rotation then vel.rotation = minVel.rotation end
450
423
                if maxVel.rotation and vel.rotation > maxVel.rotation then vel.rotation = maxVel.rotation end
451
 
                
452
 
                if self.onUpdate then self:onUpdate(elapsed) end
 
424
 
 
425
                if vel.x ~= 0 then self.x = self.x + vel.x * elapsed end
 
426
                if vel.y ~= 0 then self.y = self.y + vel.y * elapsed end
 
427
                if vel.rotation ~= 0 then self.rotation = self.rotation + vel.rotation * elapsed end
 
428
        end,
 
429
        update = function (self, elapsed)
 
430
                    if self.onUpdate then self:onUpdate(elapsed) end
453
431
        end,
454
432
 
455
433
        endFrame = function (self, elapsed)
457
435
        end,
458
436
 
459
437
        draw = function (self, x, y)
460
 
                if self.onDraw then self:onDraw(x, y) end
 
438
                -- subclasses do interesting things here
 
439
        end,
 
440
 
 
441
        collidedWith = function (self, other, xOverlap, yOverlap)
 
442
                if self.onCollide then self:onCollide(other, xOverlap, yOverlap) end
461
443
        end
462
444
}