1 import { System
} from '@serpentity/serpentity';
2 import { SAT
} from 'matter-js';
4 import BodyComponent
from '../components/body';
5 import PointsColliderNode
from '../nodes/points_collider';
6 import PointsNode
from '../nodes/points';
9 * Handles grabbing between entities
11 * @extends {external:Serpentity.System}
12 * @class DetectPointsCollisionSystem
13 * @param {object} config a configuration object to extend.
15 export default class DetectPointsCollisionSystem
extends System
{
17 constructor(config
= {}) {
22 * The collection of points colliders
24 * @property {external:Serpentity.NodeCollection} pointsColliders
26 * @memberof DetectPointsCollisionSystem
28 this.pointsColliders
= null;
31 * The collection of points keepers
33 * @property {external:Serpentity.NodeCollection} pointsKeepers
35 * @memberof DetectPointsCollisionSystem
37 this.pointsKeepers
= null;
41 * Initializes system when added. Requests points colliders and points
45 * @memberof DetectPointsCollisionSystem
47 * @param {external:Serpentity.Engine} engine the serpentity engine to
48 * which we are getting added
52 this.pointsCollider
= engine
.getNodes(PointsColliderNode
);
53 this.pointsKeepers
= engine
.getNodes(PointsNode
);
57 * Clears system resources when removed.
61 * @memberof DetectPointsCollisionSystem
65 this.pointsCollider
= null;
66 this.pointsKeepers
= null;
70 * Runs on every update of the loop. Triggers grab and manages cooldown
74 * @param {Number} currentFrameDuration the duration of the current
76 * @memberof DetectPointsCollisionSystem
78 update(currentFrameDuration
) {
82 for (const collider
of this.pointsCollider
) {
83 const collisionTargetBody
= collider
.pointsCollider
.collisionTarget
.getComponent(BodyComponent
);
84 const pointsTarget
= collider
.pointsCollider
.pointsTarget
;
86 if (collisionTargetBody
) {
87 const collision
= SAT
.collides(collider
.body
.body
, collisionTargetBody
.body
);
88 if (collision
.collided
) {
89 points
[pointsTarget
] = (points
[pointsTarget
] || 0) + 1;
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
];