BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / objectComponents / renderer.js

renderer.js

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