Skip to content

Instantly share code, notes, and snippets.

@rvanzon
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save rvanzon/ea17d4bd6c1437d821a2 to your computer and use it in GitHub Desktop.

Select an option

Save rvanzon/ea17d4bd6c1437d821a2 to your computer and use it in GitHub Desktop.
CollisionStateEntity for MelonJS
game.CollisionStateEntity = me.Entity.extend({
init: function(x, y, settings) {
this._super(me.Entity, 'init', [x, y , settings]);
this.entitiesInside = {};
this.entitiesInsideThisFrame = {};
},
update: function (dt) {
me.collision.check(this);
// check if entities left this frame
if (this.entitiesInside) {
for (var entityGUID in this.entitiesInside) {
if (!this.entitiesInsideThisFrame || !this.entitiesInsideThisFrame[entityGUID]) {
var entity = this.entitiesInside[entityGUID];
this.onEntityDidLeave(entity);
if (entity.onDidLeaveEntity)
entity.onDidLeaveEntity(this);
delete this.entitiesInside[entityGUID];
}
}
}
// check if entities entered this frame
if (this.entitiesInsideThisFrame) {
for (var entityGUID in this.entitiesInsideThisFrame) {
if (!this.entitiesInside[entityGUID]) {
var entity = this.entitiesInsideThisFrame[entityGUID];
this.onEntityDidEnter(entity);
if (entity.onDidEnterEntity)
entity.onDidEnterEntity(this);
this.entitiesInside[entityGUID] = this.entitiesInsideThisFrame[entityGUID];
}
}
}
// clean up
if (this.entitiesInsideThisFrame)
this.entitiesInsideThisFrame = {};
return this._super(me.Entity, 'update', [dt]);
},
onCollision : function (a, b) {
if (!this.entitiesInsideThisFrame)
this.entitiesInsideThisFrame = {};
this.entitiesInsideThisFrame[b.GUID] = b;
return false;
},
onDidEnterEntity: function (entity) {
},
onDidLeaveEntity: function (entity) {
},
getEntitiesInside: function () {
return this.entitiesInside;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment