]> git.r.bdr.sh - rbdr/sumo/blame_incremental - lib/systems/create_coupling_line.js
Colorize the winner renderer
[rbdr/sumo] / lib / systems / create_coupling_line.js
... / ...
CommitLineData
1import { System } from '@serpentity/serpentity';
2import { Graphics } from 'pixi.js';
3
4import PositionComponent from '@serpentity/components.position';
5
6import RenderableCoupleNode from '../nodes/renderable_couple';
7
8/**
9 * Renders renderable objects using pixi
10 *
11 * @extends {external:Serpentity.System}
12 * @class CreateCouplingLineSystem
13 * @param {object} config a configuration object to extend.
14 */
15export default class CreateCouplingLineSystem extends System {
16
17 constructor(config = {}) {
18
19 super();
20
21 /**
22 * The node collection of coupled entities entities
23 *
24 * @property {external:Serpentity.NodeCollection} coupledEntities
25 * @instance
26 * @memberof CreateCouplingLineSystem
27 */
28 this.coupledEntities = null;
29 }
30
31 /**
32 * Initializes system when added. Requests renderable nodes and
33 *
34 * @function added
35 * @memberof CreateCouplingLineSystem
36 * @instance
37 * @param {external:Serpentity.Engine} engine the serpentity engine to
38 * which we are getting added
39 */
40 added(engine) {
41
42 this.coupledEntities = engine.getNodes(RenderableCoupleNode);
43 }
44
45 /**
46 * Clears system resources when removed.
47 *
48 * @function removed
49 * @instance
50 * @memberof CreateCouplingLineSystem
51 */
52 removed() {
53
54 this.coupledEntities = null;
55 }
56
57 /**
58 * Runs on every update of the loop. Does nothing.
59 *
60 * @function update
61 * @instance
62 * @param {Number} currentFrameDuration the duration of the current
63 * frame
64 * @memberof CreateCouplingLineSystem
65 */
66 update(currentFrameDuration) {
67
68 for (const rootEntity of this.coupledEntities) {
69
70 const rootGraphic = rootEntity.container.container;
71 rootGraphic.removeChildren();
72
73 let lineGraphic = new Graphics();
74 lineGraphic = lineGraphic.lineStyle(5, 0xffffff, 0.9);
75
76 let initialPosition = false;
77
78 for (const coupledEntity of rootEntity.coupledEntities.coupledEntities) {
79 if (coupledEntity.hasComponent(PositionComponent)) {
80
81 const position = coupledEntity.getComponent(PositionComponent);
82
83 if (!initialPosition) {
84 lineGraphic = lineGraphic.moveTo(position.x, position.y);
85 initialPosition = true;
86 continue;
87 }
88 lineGraphic = lineGraphic.lineTo(position.x, position.y);
89 }
90 }
91
92 rootGraphic.addChild(lineGraphic);
93 }
94 }
95};
96