BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / objectComponents / renderer.js

renderer.js

ab969f12 History
renderer.js 4.8 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 this.subImage = this.cycle.cycleArray[index];
69 this.cycle.current = index;
70 }
71
72 draw()
73 {
74 if(this.sprite)
75 {
76 var offX = this.gameObject.scene.cameraPosition.x;
77 var offY = this.gameObject.scene.cameraPosition.y;
78
79 this.drawSprite(this.gameObject.transform.position.x + offX,
80 this.gameObject.transform.position.y + offY,
81 this.gameObject.transform.size.x,
82 this.gameObject.transform.size.y,
83 this.gameObject.transform.rotation);
84 }
85 }
86
87 drawSprite()
88 {
89 if(isNaN(this.subImage))
90 {
91 this.subImage = this.g_subImgOld;
92 }
93 else
94 {
95 this.g_subImgOld = this.subImage;
96 }
97
98 var subImage = Math.round(this.subImage);
99 subImage %= this.sprite.subImages;
100
101 var c = subImage % this.sprite.subImagesPerRow;
102 var r = Math.floor(subImage / this.sprite.subImagesPerRow);
103 var sw = (this.sprite.g_image.width / this.sprite.subImagesPerRow);
104 var sh = (this.sprite.g_image.height / Math.ceil(this.sprite.subImages / this.sprite.subImagesPerRow));
105 var sx = sw * c;
106 var sy = sh * r;
107
108 this.sprite.draw(
109 this.gameObject.scene,
110 sx, sy, sw, sh,
111 Math.round(this.gameObject.transform.position.x),
112 Math.round(this.gameObject.transform.position.y),
113 this.gameObject.transform.size.x,
114 this.gameObject.transform.size.y,
115 this.gameObject.transform.rotation,
116 this.mirrorX,
117 this.mirrorY,
118 this.alpha
119 );
120 }
121
122 get currentAnimationName()
123 {
124 return this.cycle? this.cycle.label : null;
125 }
126
127 setAnimation(label, defaultIndex = 0, cycleSpeed = -1)
128 {
129 if(cycleSpeed < 0)
130 {
131 cycleSpeed = this.defaultAnimationSpeed;
132 }
133
134 if(this.cycle)
135 {
136 if(this.cycle.label == label)
137 {
138 this.cycle.defaultIndex = defaultIndex;
139 this.cycle.cycleSpeed = cycleSpeed;
140 return;
141 }
142 }
143
144 let cycleData = this.sprite.animationCycles[label];
145
146 this.cycle = {
147 label: label,
148 cycleArray: cycleData.frames,
149 default: defaultIndex,
150 current: defaultIndex,
151 cycleSpeed: cycleSpeed,
152 loop: cycleData.loop,
153 chain: cycleData.chain,
154 chainDefaultIndex: cycleData.chainDefaultIndex,
155 chainSpeed: cycleData.chainSpeed,
156 timer: 0
157 };
158
159 this.subImage = this.cycle.cycleArray[this.cycle.current];
160 }
161
162 setAnimationCycleSpeed(cycleSpeed)
163 {
164 if(this.cycle)
165 {
166 this.cycle.cycleSpeed = cycleSpeed;
167 }
168 }
169
170 setSubImage(subImage)
171 {
172 this.subImage = subImage;
173 this.cycle = null;
174 }
175}