BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / objectComponents / renderer.js

renderer.js

renderer.js 3.6 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 // RenderLayer name; null uses RenderLayer.DEFAULT_NAME ("default").
20 layer = null;
21
22 constructor (parentObject, sprite)
23 {
24 this.gameObject = parentObject;
25 this.sprite = sprite;
26 this.shader = null;
27 this.shaderUniforms = null;
28 this.layer = null;
29 }
30
31 get layerName ()
32 {
33 return this.layer || RenderLayer.DEFAULT_NAME;
34 }
35
36 update (deltaTime)
37 {
38 if (this.cycle)
39 {
40 if (!this.cycle.cycleSpeed || this.cycle.cycleSpeed == -1)
41 {
42 this.subImage = this.cycle.cycleArray[this.cycle.default];
43 this.cycle.current = this.cycle.default;
44 return;
45 }
46 this.cycle.timer += deltaTime;
47
48 if (this.cycle.timer > this.cycle.cycleSpeed)
49 {
50 this.cycle.timer -= this.cycle.cycleSpeed;
51 let next;
52
53 if (this.cycle.loop)
54 {
55 next = (this.cycle.current + 1) % this.cycle.cycleArray.length;
56 }
57 else
58 {
59 if (this.cycle.current + 1 >= this.cycle.cycleArray.length)
60 {
61 if (this.cycle.chain)
62 {
63 this.cycle.chainDefaultIndex = this.cycle.chainDefaultIndex || this.cycle.default;
64 this.cycle.chainSpeed = this.cycle.chainSpeed || this.cycle.cycleSpeed;
65 this.setAnimation(this.cycle.chain, this.cycle.chainDefaultIndex, this.cycle.chainSpeed);
66 }
67
68 return;
69 }
70 else
71 {
72 next = this.cycle.current + 1;
73 }
74 }
75
76 this.updateCycleSprite(next);
77 }
78 }
79 }
80
81 updateCycleSprite (index)
82 {
83 this.subImage = this.cycle.cycleArray[index];
84 this.cycle.current = index;
85 }
86
87 draw ()
88 {
89 if (this.sprite)
90 {
91 var offX = this.gameObject.scene.cameraPosition.x;
92 var offY = this.gameObject.scene.cameraPosition.y;
93
94 this.drawSprite(this.gameObject.transform.position.x + offX,
95 this.gameObject.transform.position.y + offY,
96 this.gameObject.transform.size.x,
97 this.gameObject.transform.size.y,
98 this.gameObject.transform.rotation);
99 }
100 }
101
102 drawSprite (dx, dy, dw, dh, rotation)
103 {
104 if (isNaN(this.subImage))
105 {
106 this.subImage = this.g_subImgOld;
107 }
108 else
109 {
110 this.g_subImgOld = this.subImage;
111 }
112
113 this.sprite.draw(
114 this.gameObject.scene,
115 Math.round(dx),
116 Math.round(dy),
117 dw,
118 dh,
119 rotation,
120 this.mirrorX,
121 this.mirrorY,
122 this.alpha,
123 this.subImage,
124 this.shader,
125 this.shaderUniforms
126 );
127 }
128
129 get currentAnimationName ()
130 {
131 return this.cycle ? this.cycle.label : null;
132 }
133
134 setAnimation (label, defaultIndex = 0, cycleSpeed = -1)
135 {
136 if (cycleSpeed < 0)
137 {
138 cycleSpeed = this.defaultAnimationSpeed;
139 }
140
141 if (!this.sprite || !this.sprite.animationCycles || !this.sprite.animationCycles[label])
142 {
143 console.warn("Renderer.setAnimation: unknown animation '" + label + "'");
144 return;
145 }
146
147 if (this.cycle)
148 {
149 if (this.cycle.label == label)
150 {
151 this.cycle.default = defaultIndex;
152 this.cycle.cycleSpeed = cycleSpeed;
153 return;
154 }
155 }
156
157 const cycleData = this.sprite.animationCycles[label];
158
159 this.cycle = {
160 label: label,
161 cycleArray: cycleData.frames,
162 default: defaultIndex,
163 current: defaultIndex,
164 cycleSpeed: cycleSpeed,
165 loop: cycleData.loop,
166 chain: cycleData.chain,
167 chainDefaultIndex: cycleData.chainDefaultIndex,
168 chainSpeed: cycleData.chainSpeed,
169 timer: 0
170 };
171
172 this.subImage = this.cycle.cycleArray[this.cycle.current];
173 }
174
175 setAnimationCycleSpeed (cycleSpeed)
176 {
177 if (this.cycle)
178 {
179 this.cycle.cycleSpeed = cycleSpeed;
180 }
181 }
182
183 setSubImage (subImage)
184 {
185 this.subImage = subImage;
186 this.cycle = null;
187 }
188}