BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / objectComponents / renderer.js

renderer.js

autoformat all
timfa Tim · pushed by timfa · 4 years ago
fc3febb8 History
renderer.js 4.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 ()
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(this.gameObject.transform.position.x),
102 Math.round(this.gameObject.transform.position.y),
103 this.gameObject.transform.size.x,
104 this.gameObject.transform.size.y,
105 this.gameObject.transform.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.cycle)
126 {
127 if (this.cycle.label == label)
128 {
129 this.cycle.defaultIndex = defaultIndex;
130 this.cycle.cycleSpeed = cycleSpeed;
131 return;
132 }
133 }
134
135 let cycleData = this.sprite.animationCycles[label];
136
137 this.cycle = {
138 label: label,
139 cycleArray: cycleData.frames,
140 default: defaultIndex,
141 current: defaultIndex,
142 cycleSpeed: cycleSpeed,
143 loop: cycleData.loop,
144 chain: cycleData.chain,
145 chainDefaultIndex: cycleData.chainDefaultIndex,
146 chainSpeed: cycleData.chainSpeed,
147 timer: 0
148 };
149
150 this.subImage = this.cycle.cycleArray[this.cycle.current];
151 }
152
153 setAnimationCycleSpeed (cycleSpeed)
154 {
155 if (this.cycle)
156 {
157 this.cycle.cycleSpeed = cycleSpeed;
158 }
159 }
160
161 setSubImage (subImage)
162 {
163 this.subImage = subImage;
164 this.cycle = null;
165 }
166}