Merge remote-tracking branch 'origin/master'

This commit is contained in:
Pavle Goloskokovic 2017-11-23 12:31:42 +01:00
commit 6e8109d812
8 changed files with 256 additions and 4 deletions

View file

@ -1,6 +1,7 @@
// Phaser.Input.Pointer
var Class = require('../utils/Class');
var Vector2 = require('../math/Vector2');
// DOM event button value:
// A number representing a given button:
@ -36,8 +37,7 @@ var Pointer = new Class({
// 16 : 5th button (typically the "Browser Forward" button)
this.buttons = 0;
this.x = 0;
this.y = 0;
this.position = new Vector2();
// Coordinates and time of the pointer when Button 1 (left button), or Touch, was pressed, used for dragging objects
this.downX = 0;
@ -67,6 +67,34 @@ var Pointer = new Class({
this.justMoved = false;
},
x: {
get: function ()
{
return this.position.x;
},
set: function (value)
{
this.position.x = value;
}
},
y: {
get: function ()
{
return this.position.y;
},
set: function (value)
{
this.position.y = value;
}
},
reset: function ()
{
this.buttons = 0;

View file

@ -14,12 +14,10 @@ Matter.Resolver = require('./lib/collision/Resolver');
Matter.SAT = require('./lib/collision/SAT');
Matter.Constraint = require('./lib/constraint/Constraint');
Matter.MouseConstraint = require('./lib/constraint/MouseConstraint');
Matter.Common = require('./lib/core/Common');
Matter.Engine = require('./lib/core/Engine');
Matter.Events = require('./lib/core/Events');
Matter.Mouse = require('./lib/core/Mouse');
Matter.Sleeping = require('./lib/core/Sleeping');
Matter.Plugin = require('./lib/core/Plugin');

View file

@ -4,6 +4,7 @@ var Composites = require('./lib/factory/Composites');
var Constraint = require('./lib/constraint/Constraint');
var MatterImage = require('./MatterImage');
var MatterSprite = require('./MatterSprite');
var PointerConstraint = require('./PointerConstraint');
// When registering a factory function 'this' refers to the GameObjectFactory context.
//
@ -108,6 +109,20 @@ var Factory = new Class({
return constraint;
},
mouseSpring: function (options)
{
return this.pointerConstraint(options);
},
pointerConstraint: function (options)
{
var pointerConstraint = new PointerConstraint(this.scene, this.world, options);
this.world.add(pointerConstraint.constraint);
return pointerConstraint;
},
image: function (x, y, key, frame, options)
{
var image = new MatterImage(this.world, x, y, key, frame, options);

View file

@ -0,0 +1,159 @@
var Bounds = require('./lib/geometry/Bounds');
var Class = require('../../utils/Class');
var Composite = require('./lib/body/Composite');
var Constraint = require('./lib/constraint/Constraint');
var Detector = require('./lib/collision/Detector');
var Merge = require('../../utils/object/Merge');
var Sleeping = require('./lib/core/Sleeping');
var Vertices = require('./lib/geometry/Vertices');
var PointerConstraint = new Class({
initialize:
function PointerConstraint (scene, world, options)
{
if (options === undefined) { options = {}; }
// Defaults
var defaults = {
label: 'Pointer Constraint',
pointA: { x: 0, y: 0 },
pointB: { x: 0, y: 0 },
damping: 0,
length: 0.01,
stiffness: 0.1,
angularStiffness: 1,
collisionFilter: {
category: 0x0001,
mask: 0xFFFFFFFF,
group: 0
}
};
this.scene = scene;
this.world = world;
this.pointer = null;
this.active = true;
this.constraint = Constraint.create(Merge(options, defaults));
this.world.events.on('BEFORE_UPDATE_EVENT', this.update, 0, this);
scene.sys.events.on('POINTER_DOWN_EVENT', this.onDown, 0, this);
scene.sys.events.on('POINTER_UP_EVENT', this.onUp, 0, this);
},
onDown: function (event)
{
this.pointer = event.pointer;
},
onUp: function (event)
{
this.pointer = null;
},
getBodyPart: function (body, position)
{
var constraint = this.constraint;
var start = (body.parts.length > 1) ? 1 : 0;
for (var i = start; i < body.parts.length; i++)
{
var part = body.parts[i];
if (Vertices.contains(part.vertices, position))
{
constraint.bodyB = body;
constraint.pointA.x = position.x;
constraint.pointA.y = position.y;
constraint.pointB.x = position.x - body.position.x;
constraint.pointB.y = position.y - body.position.y;
constraint.angleB = body.angle;
Sleeping.set(body, false);
return true;
}
}
return false;
},
update: function ()
{
if (!this.active)
{
return;
}
var pointer = this.pointer;
var constraint = this.constraint;
if (!pointer)
{
// Pointer is up / released
if (constraint.bodyB)
{
constraint.bodyB = null;
}
}
else
{
var position = pointer.position;
if (constraint.bodyB)
{
// Pointer is down and we have bodyB, so wake it up
Sleeping.set(constraint.bodyB, false);
constraint.pointA.x = position.x;
constraint.pointA.y = position.y;
}
else
{
var bodies = Composite.allBodies(this.world.localWorld);
// Pointer is down and no bodyB, so check if we've hit anything
for (var i = 0; i < bodies.length; i++)
{
var body = bodies[i];
if (Bounds.contains(body.bounds, position) &&
Detector.canCollide(body.collisionFilter, constraint.collisionFilter))
{
if (this.getBodyPart(body, position))
{
break;
}
}
}
}
}
},
destroy: function ()
{
this.world.remove(this.constraint);
this.constraint = null;
this.world.events.off('BEFORE_UPDATE_EVENT', this.update);
this.scene.sys.events.off('POINTER_DOWN_EVENT', this.onDown, 0, this);
this.scene.sys.events.off('POINTER_UP_EVENT', this.onUp, 0, this);
}
});
module.exports = PointerConstraint;

View file

@ -68,6 +68,18 @@ var World = new Class({
{
var localEvents = this.events;
MatterEvents.on(this.engine, 'beforeUpdate', function (event) {
localEvents.dispatch(new PhysicsEvent.BEFORE_UPDATE(event));
});
MatterEvents.on(this.engine, 'afterUpdate', function (event) {
localEvents.dispatch(new PhysicsEvent.AFTER_UPDATE(event));
});
MatterEvents.on(this.engine, 'collisionStart', function (event) {
localEvents.dispatch(new PhysicsEvent.COLLISION_START(event.pairs));

View file

@ -0,0 +1,19 @@
var Class = require('../../../utils/Class');
var Event = require('../../../events/Event');
var AfterUpdateEvent = new Class({
Extends: Event,
initialize:
function AfterUpdateEvent (event)
{
Event.call(this, 'AFTER_UPDATE_EVENT');
this.event = event;
}
});
module.exports = AfterUpdateEvent;

View file

@ -0,0 +1,19 @@
var Class = require('../../../utils/Class');
var Event = require('../../../events/Event');
var BeforeUpdateEvent = new Class({
Extends: Event,
initialize:
function BeforeUpdateEvent (event)
{
Event.call(this, 'BEFORE_UPDATE_EVENT');
this.event = event;
}
});
module.exports = BeforeUpdateEvent;

View file

@ -2,6 +2,8 @@
module.exports = {
AFTER_UPDATE: require('./AfterUpdateEvent'),
BEFORE_UPDATE: require('./BeforeUpdateEvent'),
COLLISION_ACTIVE: require('./CollisionActiveEvent'),
COLLISION_END: require('./CollisionEndEvent'),
COLLISION_START: require('./CollisionStartEvent'),