mirror of
https://github.com/liabru/matter-js.git
synced 2024-11-30 10:20:52 -05:00
added body sleepStart and sleepEnd events, closes #91
This commit is contained in:
parent
796faeb069
commit
bc26469df2
3 changed files with 43 additions and 0 deletions
|
@ -862,6 +862,13 @@
|
||||||
|
|
||||||
World.add(_world, stack);
|
World.add(_world, stack);
|
||||||
|
|
||||||
|
for (var i = 0; i < stack.bodies.length; i++) {
|
||||||
|
Events.on(stack.bodies[i], 'sleepStart sleepEnd', function(event) {
|
||||||
|
var body = this;
|
||||||
|
console.log('body id', body.id, 'sleeping:', body.isSleeping);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
_engine.enableSleeping = true;
|
_engine.enableSleeping = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -604,6 +604,32 @@ var Body = {};
|
||||||
return properties;
|
return properties;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* Events Documentation
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fired when a body starts sleeping (where `this` is the body).
|
||||||
|
*
|
||||||
|
* @event sleepStart
|
||||||
|
* @this {body} The body that has started sleeping
|
||||||
|
* @param {} event An event object
|
||||||
|
* @param {} event.source The source object of the event
|
||||||
|
* @param {} event.name The name of the event
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fired when a body ends sleeping (where `this` is the body).
|
||||||
|
*
|
||||||
|
* @event sleepEnd
|
||||||
|
* @this {body} The body that has ended sleeping
|
||||||
|
* @param {} event An event object
|
||||||
|
* @param {} event.source The source object of the event
|
||||||
|
* @param {} event.name The name of the event
|
||||||
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
* Properties Documentation
|
* Properties Documentation
|
||||||
|
|
|
@ -92,6 +92,8 @@ var Sleeping = {};
|
||||||
* @param {boolean} isSleeping
|
* @param {boolean} isSleeping
|
||||||
*/
|
*/
|
||||||
Sleeping.set = function(body, isSleeping) {
|
Sleeping.set = function(body, isSleeping) {
|
||||||
|
var wasSleeping = body.isSleeping;
|
||||||
|
|
||||||
if (isSleeping) {
|
if (isSleeping) {
|
||||||
body.isSleeping = true;
|
body.isSleeping = true;
|
||||||
body.sleepCounter = body.sleepThreshold;
|
body.sleepCounter = body.sleepThreshold;
|
||||||
|
@ -106,9 +108,17 @@ var Sleeping = {};
|
||||||
body.speed = 0;
|
body.speed = 0;
|
||||||
body.angularSpeed = 0;
|
body.angularSpeed = 0;
|
||||||
body.motion = 0;
|
body.motion = 0;
|
||||||
|
|
||||||
|
if (!wasSleeping) {
|
||||||
|
Events.trigger(body, 'sleepStart');
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
body.isSleeping = false;
|
body.isSleeping = false;
|
||||||
body.sleepCounter = 0;
|
body.sleepCounter = 0;
|
||||||
|
|
||||||
|
if (wasSleeping) {
|
||||||
|
Events.trigger(body, 'sleepEnd');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue