]> git.r.bdr.sh - rbdr/sumo/blame - lib/systems/detect_points_collision.js
Merge branch 'feature/rbdr-gitlab-pipeline' into 'develop'
[rbdr/sumo] / lib / systems / detect_points_collision.js
CommitLineData
3100e053
RBR
1import { System } from '@serpentity/serpentity';
2import { SAT } from 'matter-js';
3
4import BodyComponent from '../components/body';
5import PointsColliderNode from '../nodes/points_collider';
6import PointsNode from '../nodes/points';
7
8/**
9 * Handles grabbing between entities
10 *
11 * @extends {external:Serpentity.System}
12 * @class DetectPointsCollisionSystem
13 * @param {object} config a configuration object to extend.
14 */
15export default class DetectPointsCollisionSystem extends System {
16
17 constructor(config = {}) {
18
19 super();
20
21 /**
22 * The collection of points colliders
23 *
24 * @property {external:Serpentity.NodeCollection} pointsColliders
25 * @instance
26 * @memberof DetectPointsCollisionSystem
27 */
28 this.pointsColliders = null;
29
30 /**
31 * The collection of points keepers
32 *
33 * @property {external:Serpentity.NodeCollection} pointsKeepers
34 * @instance
35 * @memberof DetectPointsCollisionSystem
36 */
37 this.pointsKeepers = null;
38 }
39
40 /**
41 * Initializes system when added. Requests points colliders and points
42 * accumulators
43 *
44 * @function added
45 * @memberof DetectPointsCollisionSystem
46 * @instance
47 * @param {external:Serpentity.Engine} engine the serpentity engine to
48 * which we are getting added
49 */
50 added(engine) {
51
52 this.pointsCollider = engine.getNodes(PointsColliderNode);
53 this.pointsKeepers = engine.getNodes(PointsNode);
54 }
55
56 /**
57 * Clears system resources when removed.
58 *
59 * @function removed
60 * @instance
61 * @memberof DetectPointsCollisionSystem
62 */
63 removed() {
64
65 this.pointsCollider = null;
66 this.pointsKeepers = null;
67 }
68
69 /**
70 * Runs on every update of the loop. Triggers grab and manages cooldown
71 *
72 * @function update
73 * @instance
74 * @param {Number} currentFrameDuration the duration of the current
75 * frame
76 * @memberof DetectPointsCollisionSystem
77 */
78 update(currentFrameDuration) {
79
80 const points = {};
81
82 for (const collider of this.pointsCollider) {
83 const collisionTargetBody = collider.pointsCollider.collisionTarget.getComponent(BodyComponent);
84 const pointsTarget = collider.pointsCollider.pointsTarget;
85
86 if (collisionTargetBody) {
87 const collision = SAT.collides(collider.body.body, collisionTargetBody.body);
88 if (collision.collided) {
89 points[pointsTarget] = (points[pointsTarget] || 0) + 1;
90 }
91 }
92 }
93
94 for (const pointsKeeper of this.pointsKeepers) {
95 for (const pointsLabel of Object.keys(points)) {
96 pointsKeeper.points[pointsLabel] = (pointsKeeper.points[pointsLabel] || 0) + points[pointsLabel];
97 }
98 }
99 }
100};