/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-05 16:54:58 UTC
  • Revision ID: josh@9ix.org-20130305165458-1w0u0ojz1wd7f7hu
use built-in maxVelocity system

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
350
373
                if self.onStartFrame then self:onStartFrame(elapsed) end
351
374
        end,
352
375
 
353
 
        doPhysics = function (self, elapsed)
 
376
        update = function (self, elapsed)
354
377
                local vel = self.velocity
355
378
                local acc = self.acceleration
356
379
                local drag = self.drag
373
396
 
374
397
                -- physics
375
398
                        
 
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
                
376
403
                if acc.x and acc.x ~= 0 then
377
404
                        vel.x = vel.x + acc.x * elapsed
378
405
                else
421
448
                if maxVel.y and vel.y > maxVel.y then vel.y = maxVel.y end
422
449
                if minVel.rotation and vel.rotation < minVel.rotation then vel.rotation = minVel.rotation end
423
450
                if maxVel.rotation and vel.rotation > maxVel.rotation then vel.rotation = maxVel.rotation 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
 
                    self:doPhysics(elapsed)
431
 
 
432
 
                    if self.onUpdate then self:onUpdate(elapsed) end
 
451
                
 
452
                if self.onUpdate then self:onUpdate(elapsed) end
433
453
        end,
434
454
 
435
455
        endFrame = function (self, elapsed)
437
457
        end,
438
458
 
439
459
        draw = function (self, x, y)
440
 
                -- subclasses do interesting things here
441
 
        end,
442
 
 
443
 
        collidedWith = function (self, other, xOverlap, yOverlap)
444
 
                if self.onCollide then self:onCollide(other, xOverlap, yOverlap) end
 
460
                if self.onDraw then self:onDraw(x, y) end
445
461
        end
446
462
}