aboutsummaryrefslogtreecommitdiff
path: root/lib/systems/grab.js
blob: 7d7d73b12794b68351b6b5b40388d4303bb370e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { System } from '@serpentity/serpentity';
import { Body, Constraint, SAT, World } from 'matter-js';

import GrabberNode from '../nodes/grabber';
import GrabbableNode from '../nodes/grabbable';

import Config from '../config';

const internals = {
  kGrabRadius: 50,
  kNoEngine: 'No matter js physics engine found. Make sure you set the `engine` key in the config object when initializing.'
};

/**
 * Handles grabbing between entities
 *
 * @extends {external:Serpentity.System}
 * @class GrabSystem
 * @param {object} config a configuration object to extend.
 */
export default class GrabSystem extends System {

  constructor(config = {}) {

    super();

    /**
     * The node collection of grabbers
     *
     * @property {external:Serpentity.NodeCollection} grabbers
     * @instance
     * @memberof GrabSystem
     */
    this.grabbers = null;

    /**
     * The node collection of grabbables
     *
     * @property {external:Serpentity.NodeCollection} grabbables
     * @instance
     * @memberof GrabSystem
     */
    this.grabbables = null;

    /**
     * The matter-js engine we will use to add and remove constraints
     *
     * @property {external:MatterJs.Engine} engine
     * @instance
     * @memberof GrabSystem
     */
    this.engine = config.engine;

    if (!this.engine) {
      throw new Error(internals.kNoEngine);
    }
  }

  /**
   * Initializes system when added. Requests grabber and grabbable nodes
   *
   * @function added
   * @memberof GrabSystem
   * @instance
   * @param {external:Serpentity.Engine} engine the serpentity engine to
   * which we are getting added
   */
  added(engine) {

    this.grabbers = engine.getNodes(GrabberNode);
    this.grabbables = engine.getNodes(GrabbableNode);
  }

  /**
   * Clears system resources when removed.
   *
   * @function removed
   * @instance
   * @memberof GrabSystem
   */
  removed() {

    this.grabbers = null;
    this.grabbables = null;
  }

  /**
   * Runs on every update of the loop. Triggers grab and manages cooldown
   *
   * @function update
   * @instance
   * @param {Number} currentFrameDuration the duration of the current
   * frame
   * @memberof GrabSystem
   */
  update(currentFrameDuration) {

    for (const grabber of this.grabbers) {

      const grab = grabber.grab;

      if (grab.grabbing && !grab.locked) {
        this._grab(grabber);
      }

      const isGrabReleased = !grab.grabbing || grab.currentCooldown >= grab.cooldown;
      if (grab.constraint && isGrabReleased) {
        this._release(grabber);
      }

      if (!grab.grabbing && grab.locked && grab.currentCooldown >= grab.cooldown) {
        this._unlock(grabber);
      }

      if (grab.locked) {
        grab.currentCooldown += currentFrameDuration;
      }

      grab.grabbing = 0;
    }
  }

  // Executes the dash action

  _grab(grabber) {

    const grab = grabber.grab;

    grab.locked = true;
    grab.currentCooldown = 0;

    Body.setPosition(grabber.grabArea.area, grabber.body.body.position);

    for (const grabbable of this.grabbables) {

      if (grabbable.entity === grabber.entity) {
        continue;
      }

      const collision = SAT.collides(grabber.grabArea.area, grabbable.body.body);
      if (collision.collided) {
        grab.constraint = this._createConstraint(grabber.body.body, grabbable.body.body);
      }
    }
  }

  // Executes the unlock action

  _unlock(grabber) {

    grabber.grab.locked = false;
  }

  // Releases a constraint

  _release(grabber) {

    World.remove(this.engine.world, grabber.grab.constraint);
    grabber.grab.currentCooldown = 0;
    grabber.grab.constraint = null;
  }

  // Performs a grab between two entities

  _createConstraint(grabber, grabbable) {

    const constraint = Constraint.create({ // Attach the sensor to the body
      bodyA: grabber,
      bodyB: grabbable,
      damping: 0,
      length: internals.kGrabRadius / Config.meterSize,
      stiffness: 1
    });

    World.add(this.engine.world, [constraint]);

    return constraint;
  }
}