]> git.r.bdr.sh - rbdr/sumo/blob - lib/systems/dash.js
Pixelate the text
[rbdr/sumo] / lib / systems / dash.js
1 import { System } from '@serpentity/serpentity';
2
3 const internals = {
4 kForce: 20
5 };
6
7 import DasherNode from '../nodes/dasher';
8
9 /**
10 * Applies a dash as a force on an entity. Locks it until the button is released
11 * and a cooldown period has passed
12 *
13 * @extends {external:Serpentity.System}
14 * @class DashSystem
15 * @param {object} config a configuration object to extend.
16 */
17 export default class DashSystem extends System {
18
19 constructor(config = {}) {
20
21 super();
22
23 /**
24 * The node collection of dashers
25 *
26 * @property {external:Serpentity.NodeCollection} dashers
27 * @instance
28 * @memberof DashSystem
29 */
30 this.dashers = null;
31 }
32
33 /**
34 * Initializes system when added. Requests dasher nodes
35 *
36 * @function added
37 * @memberof DashSystem
38 * @instance
39 * @param {external:Serpentity.Engine} engine the serpentity engine to
40 * which we are getting added
41 */
42 added(engine) {
43
44 this.dashers = engine.getNodes(DasherNode);
45 }
46
47 /**
48 * Clears system resources when removed.
49 *
50 * @function removed
51 * @instance
52 * @memberof DashSystem
53 */
54 removed() {
55
56 this.dashers = null;
57 }
58
59 /**
60 * Runs on every update of the loop. Triggers dash and manages cooldown
61 *
62 * @function update
63 * @instance
64 * @param {Number} currentFrameDuration the duration of the current
65 * frame
66 * @memberof DashSystem
67 */
68 update(currentFrameDuration) {
69
70 for (const dasher of this.dashers) {
71
72 const dash = dasher.dash;
73
74 if (dash.dashing && !dash.locked) {
75 this._dash(dasher);
76 }
77
78 if (!dash.dashing && dash.locked && dash.currentCooldown >= dash.cooldown) {
79 this._unlock(dasher);
80 }
81
82 if (dash.locked) {
83 dash.currentCooldown += currentFrameDuration;
84 }
85
86 dash.dashing = 0;
87 }
88 }
89
90 // Executes the dash action
91
92 _dash(dasher) {
93
94 const dash = dasher.dash;
95 const force = dasher.force;
96
97 const angle = force.lastAngle || 0;
98 dash.locked = true;
99 dash.currentCooldown = 0;
100
101 const xComponent = internals.kForce * Math.cos(angle);
102 const yComponent = internals.kForce * Math.sin(angle);
103
104 force.x += xComponent;
105 force.y += yComponent;
106 }
107
108 // Executes the unlock action
109
110 _unlock(dasher) {
111
112 dasher.dash.locked = false;
113 }
114 };
115
116