BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / objectComponents / renderer.js

renderer.js

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