grab.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. AFRAME.registerComponent('grab', require('./src/misc/grab'));
  3. },{"./src/misc/grab":2}],2:[function(require,module,exports){
  4. /**
  5. * Based on aframe/examples/showcase/tracked-controls.
  6. *
  7. * Handles events coming from the hand-controls.
  8. * Determines if the entity is grabbed or released.
  9. * Updates its position to move along the controller.
  10. */
  11. module.exports = {
  12. init: function () {
  13. this.GRABBED_STATE = 'grabbed';
  14. this.grabbing = false;
  15. this.hitEl = /** @type {AFRAME.Element} */ null;
  16. this.physics = /** @type {AFRAME.System} */ this.el.sceneEl.systems.physics;
  17. this.constraint = /** @type {CANNON.Constraint} */ null;
  18. // Bind event handlers
  19. this.onHit = this.onHit.bind(this);
  20. this.onGripOpen = this.onGripOpen.bind(this);
  21. this.onGripClose = this.onGripClose.bind(this);
  22. },
  23. play: function () {
  24. var el = this.el;
  25. el.addEventListener('hit', this.onHit);
  26. el.addEventListener('gripdown', this.onGripClose);
  27. el.addEventListener('gripup', this.onGripOpen);
  28. el.addEventListener('trackpaddown', this.onGripClose);
  29. el.addEventListener('trackpadup', this.onGripOpen);
  30. el.addEventListener('triggerdown', this.onGripClose);
  31. el.addEventListener('triggerup', this.onGripOpen);
  32. },
  33. pause: function () {
  34. var el = this.el;
  35. el.removeEventListener('hit', this.onHit);
  36. el.removeEventListener('gripdown', this.onGripClose);
  37. el.removeEventListener('gripup', this.onGripOpen);
  38. el.removeEventListener('trackpaddown', this.onGripClose);
  39. el.removeEventListener('trackpadup', this.onGripOpen);
  40. el.removeEventListener('triggerdown', this.onGripClose);
  41. el.removeEventListener('triggerup', this.onGripOpen);
  42. },
  43. onGripClose: function (evt) {
  44. this.grabbing = true;
  45. },
  46. onGripOpen: function (evt) {
  47. var hitEl = this.hitEl;
  48. this.grabbing = false;
  49. if (!hitEl) { return; }
  50. hitEl.removeState(this.GRABBED_STATE);
  51. this.hitEl = undefined;
  52. this.physics.world.removeConstraint(this.constraint);
  53. this.constraint = null;
  54. },
  55. onHit: function (evt) {
  56. var hitEl = evt.detail.el;
  57. // If the element is already grabbed (it could be grabbed by another controller).
  58. // If the hand is not grabbing the element does not stick.
  59. // If we're already grabbing something you can't grab again.
  60. if (!hitEl || hitEl.is(this.GRABBED_STATE) || !this.grabbing || this.hitEl) { return; }
  61. hitEl.addState(this.GRABBED_STATE);
  62. this.hitEl = hitEl;
  63. this.constraint = new CANNON.LockConstraint(this.el.body, hitEl.body);
  64. this.physics.world.addConstraint(this.constraint);
  65. }
  66. };
  67. },{}]},{},[1]);