aboutsummaryrefslogtreecommitdiff
path: root/lib/systems/detect_points_collision.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/systems/detect_points_collision.js')
-rw-r--r--lib/systems/detect_points_collision.js100
1 files changed, 100 insertions, 0 deletions
diff --git a/lib/systems/detect_points_collision.js b/lib/systems/detect_points_collision.js
new file mode 100644
index 0000000..c84f3fc
--- /dev/null
+++ b/lib/systems/detect_points_collision.js
@@ -0,0 +1,100 @@
+import { System } from '@serpentity/serpentity';
+import { SAT } from 'matter-js';
+
+import BodyComponent from '../components/body';
+import PointsColliderNode from '../nodes/points_collider';
+import PointsNode from '../nodes/points';
+
+/**
+ * Handles grabbing between entities
+ *
+ * @extends {external:Serpentity.System}
+ * @class DetectPointsCollisionSystem
+ * @param {object} config a configuration object to extend.
+ */
+export default class DetectPointsCollisionSystem extends System {
+
+ constructor(config = {}) {
+
+ super();
+
+ /**
+ * The collection of points colliders
+ *
+ * @property {external:Serpentity.NodeCollection} pointsColliders
+ * @instance
+ * @memberof DetectPointsCollisionSystem
+ */
+ this.pointsColliders = null;
+
+ /**
+ * The collection of points keepers
+ *
+ * @property {external:Serpentity.NodeCollection} pointsKeepers
+ * @instance
+ * @memberof DetectPointsCollisionSystem
+ */
+ this.pointsKeepers = null;
+ }
+
+ /**
+ * Initializes system when added. Requests points colliders and points
+ * accumulators
+ *
+ * @function added
+ * @memberof DetectPointsCollisionSystem
+ * @instance
+ * @param {external:Serpentity.Engine} engine the serpentity engine to
+ * which we are getting added
+ */
+ added(engine) {
+
+ this.pointsCollider = engine.getNodes(PointsColliderNode);
+ this.pointsKeepers = engine.getNodes(PointsNode);
+ }
+
+ /**
+ * Clears system resources when removed.
+ *
+ * @function removed
+ * @instance
+ * @memberof DetectPointsCollisionSystem
+ */
+ removed() {
+
+ this.pointsCollider = null;
+ this.pointsKeepers = 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 DetectPointsCollisionSystem
+ */
+ update(currentFrameDuration) {
+
+ const points = {};
+
+ for (const collider of this.pointsCollider) {
+ const collisionTargetBody = collider.pointsCollider.collisionTarget.getComponent(BodyComponent);
+ const pointsTarget = collider.pointsCollider.pointsTarget;
+
+ if (collisionTargetBody) {
+ const collision = SAT.collides(collider.body.body, collisionTargetBody.body);
+ if (collision.collided) {
+ points[pointsTarget] = (points[pointsTarget] || 0) + 1;
+ }
+ }
+ }
+
+ for (const pointsKeeper of this.pointsKeepers) {
+ for (const pointsLabel of Object.keys(points)) {
+ pointsKeeper.points[pointsLabel] = (pointsKeeper.points[pointsLabel] || 0) + points[pointsLabel];
+ }
+ }
+ }
+};