/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
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
 
                    if self.onUpdate then self:onUpdate(elapsed) end
 
451
                
 
452
                if self.onUpdate then self:onUpdate(elapsed) end
431
453
        end,
432
454
 
433
455
        endFrame = function (self, elapsed)
435
457
        end,
436
458
 
437
459
        draw = function (self, x, y)
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
 
460
                if self.onDraw then self:onDraw(x, y) end
443
461
        end
444
462
}