BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / objectComponents / renderer.js

renderer.js

engine optimisations
timfa Tim Falken · pushed by timfa · 3 months ago
74d8cdb3 History
renderer.js 4.1 KiB Download Raw
1class Renderer
2{
3 sprite = null;
4 gameObject = null;
5 subImage = 0;
6 alpha = 1;
7 mirrorX = false;
8 mirrorY = false;
9
10 defaultAnimationSpeed = 1;
11
12 cycle = null;
13
14 visible = true;
15
16 shader = null;
17 shaderUniforms = null;
18
19 // Optional maps for builtin sprite effects (framework `image` instances).
20 bumpMap = null;
21 secondaryMap = null;
22
23 // Dynamic lights for bumpmap: [{ x, y, color: [r,g,b]|{r,g,b} }, ...]
24 lights = null;
25
26 // RenderLayer name; null uses RenderLayer.DEFAULT_NAME ("default").
27 layer = null;
28
29 constructor (parentObject, sprite)
30 {
31 this.gameObject = parentObject;
32 this.sprite = sprite;
33 this.shader = null;
34 this.shaderUniforms = null;
35 this.bumpMap = null;
36 this.secondaryMap = null;
37 this.lights = null;
38 this.layer = null;
39 }
40
41 get layerName ()
42 {
43 return this.layer || RenderLayer.DEFAULT_NAME;
44 }
45
46 update (deltaTime)
47 {
48 if (this.cycle)
49 {
50 if (!this.cycle.cycleSpeed || this.cycle.cycleSpeed == -1)
51 {
52 this.subImage = this.cycle.cycleArray[this.cycle.default];
53 this.cycle.current = this.cycle.default;
54 return;
55 }
56 this.cycle.timer += deltaTime;
57
58 if (this.cycle.timer > this.cycle.cycleSpeed)
59 {
60 this.cycle.timer -= this.cycle.cycleSpeed;
61 let next;
62
63 if (this.cycle.loop)
64 {
65 next = (this.cycle.current + 1) % this.cycle.cycleArray.length;
66 }
67 else
68 {
69 if (this.cycle.current + 1 >= this.cycle.cycleArray.length)
70 {
71 if (this.cycle.chain)
72 {
73 this.cycle.chainDefaultIndex = this.cycle.chainDefaultIndex || this.cycle.default;
74 this.cycle.chainSpeed = this.cycle.chainSpeed || this.cycle.cycleSpeed;
75 this.setAnimation(this.cycle.chain, this.cycle.chainDefaultIndex, this.cycle.chainSpeed);
76 }
77
78 return;
79 }
80 else
81 {
82 next = this.cycle.current + 1;
83 }
84 }
85
86 this.updateCycleSprite(next);
87 }
88 }
89 }
90
91 updateCycleSprite (index)
92 {
93 this.subImage = this.cycle.cycleArray[index];
94 this.cycle.current = index;
95 }
96
97 draw ()
98 {
99 if (this.sprite)
100 {
101 var offX = this.gameObject.scene.cameraPosition.x;
102 var offY = this.gameObject.scene.cameraPosition.y;
103
104 this.drawSprite(this.gameObject.transform.position.x + offX,
105 this.gameObject.transform.position.y + offY,
106 this.gameObject.transform.size.x,
107 this.gameObject.transform.size.y,
108 this.gameObject.transform.rotation);
109 }
110 }
111
112 drawSprite (dx, dy, dw, dh, rotation)
113 {
114 if (isNaN(this.subImage))
115 {
116 this.subImage = this.g_subImgOld;
117 }
118 else
119 {
120 this.g_subImgOld = this.subImage;
121 }
122
123 let textures = null;
124 if (this.bumpMap || this.secondaryMap)
125 {
126 textures = {};
127 if (this.bumpMap)
128 { textures.u_bump = this.bumpMap; }
129 if (this.secondaryMap)
130 { textures.u_secondary = this.secondaryMap; }
131 }
132
133 this.sprite.draw(
134 this.gameObject.scene,
135 Math.round(dx),
136 Math.round(dy),
137 dw,
138 dh,
139 rotation,
140 this.mirrorX,
141 this.mirrorY,
142 this.alpha,
143 this.subImage,
144 this.shader,
145 this.shaderUniforms,
146 textures,
147 this.lights
148 );
149 }
150
151 get currentAnimationName ()
152 {
153 return this.cycle ? this.cycle.label : null;
154 }
155
156 setAnimation (label, defaultIndex = 0, cycleSpeed = -1)
157 {
158 if (cycleSpeed < 0)
159 {
160 cycleSpeed = this.defaultAnimationSpeed;
161 }
162
163 if (!this.sprite || !this.sprite.animationCycles || !this.sprite.animationCycles[label])
164 {
165 console.warn("Renderer.setAnimation: unknown animation '" + label + "'");
166 return;
167 }
168
169 if (this.cycle)
170 {
171 if (this.cycle.label == label)
172 {
173 this.cycle.default = defaultIndex;
174 this.cycle.cycleSpeed = cycleSpeed;
175 return;
176 }
177 }
178
179 const cycleData = this.sprite.animationCycles[label];
180
181 this.cycle = {
182 label: label,
183 cycleArray: cycleData.frames,
184 default: defaultIndex,
185 current: defaultIndex,
186 cycleSpeed: cycleSpeed,
187 loop: cycleData.loop,
188 chain: cycleData.chain,
189 chainDefaultIndex: cycleData.chainDefaultIndex,
190 chainSpeed: cycleData.chainSpeed,
191 timer: 0
192 };
193
194 this.subImage = this.cycle.cycleArray[this.cycle.current];
195 }
196
197 setAnimationCycleSpeed (cycleSpeed)
198 {
199 if (this.cycle)
200 {
201 this.cycle.cycleSpeed = cycleSpeed;
202 }
203 }
204
205 setSubImage (subImage)
206 {
207 this.subImage = subImage;
208 this.cycle = null;
209 }
210}