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