BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / objectComponents / renderer.js

renderer.js

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