1 import { System
} from '@serpentity/serpentity';
2 import { Body
, Vector
} from 'matter-js';
8 import PhysicalWithExternalForceNode
from '../nodes/physical_with_external_force';
11 * Applies physica from external forces (eg. controls) to the physics body
13 * @extends {external:Serpentity.System}
14 * @class ApplyForceSystem
15 * @param {object} config a configuration object to extend.
17 export default class ApplyForceSystem
extends System
{
19 constructor(config
= {}) {
24 * The node collection of entities that have external force
26 * @property {external:Serpentity.NodeCollection} physicalEntities
28 * @memberof ApplyForceSystem
30 this.physicalEntities
= null;
34 * Initializes system when added. Requests physics nodes
37 * @memberof ApplyForceSystem
39 * @param {external:Serpentity.Engine} engine the serpentity engine to
40 * which we are getting added
44 this.physicalEntities
= engine
.getNodes(PhysicalWithExternalForceNode
);
48 * Clears system resources when removed.
52 * @memberof ApplyForceSystem
56 this.physicalEntities
= null;
60 * Runs on every update of the loop. Updates the body based on the force
65 * @param {Number} currentFrameDuration the duration of the current
67 * @memberof ApplyForceSystem
69 update(currentFrameDuration
) {
71 for (const physicalEntity
of this.physicalEntities
) {
72 const body
= physicalEntity
.body
.body
;
73 const force
= physicalEntity
.force
;
74 const forceVector
= Vector
.create(force
.x
* internals
.kForce
, force
.y
* internals
.kForce
);
77 // Store the last angle and apply force on non-zero forces
79 if (force
.x
|| force
.y
) {
80 force
.lastAngle
= Math
.atan2(force
.y
, force
.x
);