BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / objectComponents / renderer.js

renderer.js

subImage calc in image
timfa wsadhelios · pushed by timfa · 4 years ago
94864fd3 History
renderer.js 4.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 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 this.sprite.draw(
99 this.gameObject.scene,
100 Math.round(this.gameObject.transform.position.x),
101 Math.round(this.gameObject.transform.position.y),
102 this.gameObject.transform.size.x,
103 this.gameObject.transform.size.y,
104 this.gameObject.transform.rotation,
105 this.mirrorX,
106 this.mirrorY,
107 this.alpha,
108 this.subImage
109 );
110 }
111
112 get currentAnimationName()
113 {
114 return this.cycle? this.cycle.label : null;
115 }
116
117 setAnimation(label, defaultIndex = 0, cycleSpeed = -1)
118 {
119 if(cycleSpeed < 0)
120 {
121 cycleSpeed = this.defaultAnimationSpeed;
122 }
123
124 if(this.cycle)
125 {
126 if(this.cycle.label == label)
127 {
128 this.cycle.defaultIndex = defaultIndex;
129 this.cycle.cycleSpeed = cycleSpeed;
130 return;
131 }
132 }
133
134 let cycleData = this.sprite.animationCycles[label];
135
136 this.cycle = {
137 label: label,
138 cycleArray: cycleData.frames,
139 default: defaultIndex,
140 current: defaultIndex,
141 cycleSpeed: cycleSpeed,
142 loop: cycleData.loop,
143 chain: cycleData.chain,
144 chainDefaultIndex: cycleData.chainDefaultIndex,
145 chainSpeed: cycleData.chainSpeed,
146 timer: 0
147 };
148
149 this.subImage = this.cycle.cycleArray[this.cycle.current];
150 }
151
152 setAnimationCycleSpeed(cycleSpeed)
153 {
154 if(this.cycle)
155 {
156 this.cycle.cycleSpeed = cycleSpeed;
157 }
158 }
159
160 setSubImage(subImage)
161 {
162 this.subImage = subImage;
163 this.cycle = null;
164 }
165}