]> git.r.bdr.sh - rbdr/sumo/blame_incremental - lib/systems/physics_to_attributes.js
Physics Engine (#4)
[rbdr/sumo] / lib / systems / physics_to_attributes.js
... / ...
CommitLineData
1import { System } from '@serpentity/serpentity';
2
3import PhysicalWithAttributesNode from '../nodes/physical_with_attributes';
4
5/**
6 * Distribuets physics data to the related components
7 *
8 * @extends {external:Serpentity.System}
9 * @class PhysicsToAttributesSystem
10 * @param {object} config a configuration object to extend.
11 */
12export default class PhysicsToAttributesSystem extends System {
13
14 constructor(config = {}) {
15
16 super();
17
18 /**
19 * The node collection of physics entities
20 *
21 * @property {external:Serpentity.NodeCollection} physicalEntities
22 * @instance
23 * @memberof PhysicsToAttributesSystem
24 */
25 this.physicalEntities = null;
26 }
27
28 /**
29 * Initializes system when added. Requests physics nodes
30 *
31 * @function added
32 * @memberof PhysicsToAttributesSystem
33 * @instance
34 * @param {external:Serpentity.Engine} engine the serpentity engine to
35 * which we are getting added
36 */
37 added(engine) {
38
39 this.physicalEntities = engine.getNodes(PhysicalWithAttributesNode);
40 }
41
42 /**
43 * Clears system resources when removed.
44 *
45 * @function removed
46 * @instance
47 * @memberof PhysicsToAttributesSystem
48 */
49 removed() {
50
51 this.physicalEntities = null;
52 }
53
54 /**
55 * Runs on every update of the loop. Updates the other components
56 * based on physics
57 *
58 * @function update
59 * @instance
60 * @param {Number} currentFrameDuration the duration of the current
61 * frame
62 * @memberof PhysicsToAttributesSystem
63 */
64 update(currentFrameDuration) {
65
66 for (const physicalEntity of this.physicalEntities) {
67 physicalEntity.position.x = physicalEntity.body.body.position.x;
68 physicalEntity.position.y = physicalEntity.body.body.position.y;
69 physicalEntity.angle.angle = physicalEntity.body.body.angle;
70 }
71 }
72};