Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
class PinballGame.Core.CollideableEntity
constructor: (entity, shape)->
@entity = entity
if shape instanceof Array
@shapes = shape
@shapes = [shape]
getCollisionShapes: -> @shapes
getEntity: -> @entity
hasShape: (givenShape) ->
if @shapes.length == 1
return true if givenShape == @shapes[0]
for shape in @getCollisionShapes()
return true if givenShape == shape
return false
setPassThrough: (value) ->
@passthrough = value
for shape in @shapes
shape.passthrough = @passthrough
class PinballGame.Core.ContactCallback
constructor: (entity, enter, leave)->
@entity = entity
@enterCallback = enter
@leaveCallback = leave
leave: (contact)->, @entity, contact) if @leaveCallback?
enter: (contact)->, @entity, contact) if @enterCallback?
class PinballGame.Core.EntityContactListener
constructor: (game, entity)->
@game = game
@entity = entity
@callbacks = []"beginContact", @onBeginContact)"endContact", @onEndContact)"preSolve", @onPresolve)
onPresolve: (presolve)->
#console.log('onPreSolve', presolve)
if presolve.contactEquations.length > 0
for ce in presolve.contactEquations
if (ce.shapeA.passthrough || ce.shapeB.passthrough || ce.bodyA.pseudoSensor || ce.bodyB.pseudoSensor)
ce.enabled = false
with: (entity, callbackEnter, callbackLeave) ->
shapes = entity.getCollisionShapes()
for shape in shapes
callback = new PinballGame.Core.ContactCallback(entity, callbackEnter, callbackLeave)
@callbacks[] = callback
onBeginContact: (contact) =>
shape = @identifyOpposite(contact)
callback = @findCallbackForShape(shape) if shape?
#console.log 'onBeginContact',
callback.enter(contact) if callback?
onEndContact: (contact) =>
shape = @identifyOpposite(contact)
callback = @findCallbackForShape(shape) if shape?
#console.log 'onEndContact',, callback
callback.leave(contact) if callback?
identifyOpposite: (contact)->
return contact.shapeA if @entity.hasShape(contact.shapeB)
return contact.shapeB if @entity.hasShape(contact.shapeA)
findCallbackForShape: (shape)->
return @callbacks[]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.