|
| 1 | +var Bounds = require('./lib/geometry/Bounds'); |
| 2 | +var Class = require('../../utils/Class'); |
| 3 | +var Composite = require('./lib/body/Composite'); |
| 4 | +var Constraint = require('./lib/constraint/Constraint'); |
| 5 | +var Detector = require('./lib/collision/Detector'); |
| 6 | +var Merge = require('../../utils/object/Merge'); |
| 7 | +var Sleeping = require('./lib/core/Sleeping'); |
| 8 | +var Vertices = require('./lib/geometry/Vertices'); |
| 9 | + |
| 10 | +var PointerConstraint = new Class({ |
| 11 | + |
| 12 | + initialize: |
| 13 | + |
| 14 | + function PointerConstraint (scene, world, options) |
| 15 | + { |
| 16 | + if (options === undefined) { options = {}; } |
| 17 | + |
| 18 | + // Defaults |
| 19 | + var defaults = { |
| 20 | + label: 'Pointer Constraint', |
| 21 | + pointA: { x: 0, y: 0 }, |
| 22 | + pointB: { x: 0, y: 0 }, |
| 23 | + damping: 0, |
| 24 | + length: 0.01, |
| 25 | + stiffness: 0.1, |
| 26 | + angularStiffness: 1, |
| 27 | + collisionFilter: { |
| 28 | + category: 0x0001, |
| 29 | + mask: 0xFFFFFFFF, |
| 30 | + group: 0 |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + this.scene = scene; |
| 35 | + |
| 36 | + this.world = world; |
| 37 | + |
| 38 | + this.pointer = null; |
| 39 | + |
| 40 | + this.active = true; |
| 41 | + |
| 42 | + this.constraint = Constraint.create(Merge(options, defaults)); |
| 43 | + |
| 44 | + this.world.events.on('BEFORE_UPDATE_EVENT', this.update, 0, this); |
| 45 | + |
| 46 | + scene.sys.events.on('POINTER_DOWN_EVENT', this.onDown, 0, this); |
| 47 | + |
| 48 | + scene.sys.events.on('POINTER_UP_EVENT', this.onUp, 0, this); |
| 49 | + }, |
| 50 | + |
| 51 | + onDown: function (event) |
| 52 | + { |
| 53 | + this.pointer = event.pointer; |
| 54 | + }, |
| 55 | + |
| 56 | + onUp: function (event) |
| 57 | + { |
| 58 | + this.pointer = null; |
| 59 | + }, |
| 60 | + |
| 61 | + getBodyPart: function (body, position) |
| 62 | + { |
| 63 | + var constraint = this.constraint; |
| 64 | + |
| 65 | + var start = (body.parts.length > 1) ? 1 : 0; |
| 66 | + |
| 67 | + for (var i = start; i < body.parts.length; i++) |
| 68 | + { |
| 69 | + var part = body.parts[i]; |
| 70 | + |
| 71 | + if (Vertices.contains(part.vertices, position)) |
| 72 | + { |
| 73 | + constraint.bodyB = body; |
| 74 | + |
| 75 | + constraint.pointA.x = position.x; |
| 76 | + constraint.pointA.y = position.y; |
| 77 | + |
| 78 | + constraint.pointB.x = position.x - body.position.x; |
| 79 | + constraint.pointB.y = position.y - body.position.y; |
| 80 | + |
| 81 | + constraint.angleB = body.angle; |
| 82 | + |
| 83 | + Sleeping.set(body, false); |
| 84 | + |
| 85 | + return true; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return false; |
| 90 | + }, |
| 91 | + |
| 92 | + update: function () |
| 93 | + { |
| 94 | + if (!this.active) |
| 95 | + { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + var pointer = this.pointer; |
| 100 | + var constraint = this.constraint; |
| 101 | + |
| 102 | + if (!pointer) |
| 103 | + { |
| 104 | + // Pointer is up / released |
| 105 | + if (constraint.bodyB) |
| 106 | + { |
| 107 | + constraint.bodyB = null; |
| 108 | + } |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + var position = pointer.position; |
| 113 | + |
| 114 | + if (constraint.bodyB) |
| 115 | + { |
| 116 | + // Pointer is down and we have bodyB, so wake it up |
| 117 | + Sleeping.set(constraint.bodyB, false); |
| 118 | + |
| 119 | + constraint.pointA.x = position.x; |
| 120 | + constraint.pointA.y = position.y; |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + var bodies = Composite.allBodies(this.world.localWorld); |
| 125 | + |
| 126 | + // Pointer is down and no bodyB, so check if we've hit anything |
| 127 | + for (var i = 0; i < bodies.length; i++) |
| 128 | + { |
| 129 | + var body = bodies[i]; |
| 130 | + |
| 131 | + if (Bounds.contains(body.bounds, position) && |
| 132 | + Detector.canCollide(body.collisionFilter, constraint.collisionFilter)) |
| 133 | + { |
| 134 | + if (this.getBodyPart(body, position)) |
| 135 | + { |
| 136 | + break; |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + }, |
| 143 | + |
| 144 | + destroy: function () |
| 145 | + { |
| 146 | + this.world.remove(this.constraint); |
| 147 | + |
| 148 | + this.constraint = null; |
| 149 | + |
| 150 | + this.world.events.off('BEFORE_UPDATE_EVENT', this.update); |
| 151 | + |
| 152 | + this.scene.sys.events.off('POINTER_DOWN_EVENT', this.onDown, 0, this); |
| 153 | + |
| 154 | + this.scene.sys.events.off('POINTER_UP_EVENT', this.onUp, 0, this); |
| 155 | + } |
| 156 | + |
| 157 | +}); |
| 158 | + |
| 159 | +module.exports = PointerConstraint; |
0 commit comments