BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / objectComponents / renderer.js

renderer.js

3e38d511 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 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.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
36 if(this.cycle.loop)
37 {
38 let next = (this.cycle.current + 1) % this.cycle.cycleArray.length;
39 }
40 else
41 {
42 if(this.cycle.current + 1 > this.cycle.cycleArray.length)
43 {
44 if(this.cycle.chain)
45 {
46 this.cycle.chainDefaultIndex = this.cycle.chainDefaultIndex || this.cycle.default;
47 this.cycle.chainSpeed = this.cycle.chainSpeed || this.cycle.cycleSpeed;
48 this.setAnimation(this.cycle.chain, this.cycle.chainDefaultIndex, this.cycle.chainSpeed);
49 return;
50 }
51 }
52 else
53 {
54 this.cycle.current ++;
55 }
56 }
57 this.updateCycleSprite(next)
58 }
59 }
60 }
61
62 updateCycleSprite(index) {
63 this.subImage = this.cycle.cycleArray[index];
64 this.cycle.current = index;
65 }
66
67 draw()
68 {
69 if(this.sprite)
70 {
71 var offX = this.gameObject.scene.cameraPosition.x;
72 var offY = this.gameObject.scene.cameraPosition.y;
73
74 this.drawSprite(this.gameObject.transform.position.x + offX,
75 this.gameObject.transform.position.y + offY,
76 this.gameObject.transform.size.x,
77 this.gameObject.transform.size.y,
78 this.gameObject.transform.rotation);
79 }
80 }
81
82 drawSprite()
83 {
84 if(isNaN(this.subImage))
85 {
86 this.subImage = this.g_subImgOld;
87 }
88 else
89 {
90 this.g_subImgOld = this.subImage;
91 }
92
93 var subImage = Math.round(this.subImage);
94 subImage %= this.sprite.subImages;
95
96 var c = subImage % this.sprite.subImagesPerRow;
97 var r = Math.floor(subImage / this.sprite.subImagesPerRow);
98 var sw = (this.sprite.g_image.width / this.sprite.subImagesPerRow);
99 var sh = (this.sprite.g_image.height / Math.ceil(this.sprite.subImages / this.sprite.subImagesPerRow));
100 var sx = sw * c;
101 var sy = sh * r;
102
103 this.sprite.draw(
104 this.gameObject.scene,
105 sx, sy, sw, sh,
106 this.gameObject.transform.position.x,
107 this.gameObject.transform.position.y,
108 this.gameObject.transform.size.x,
109 this.gameObject.transform.size.y,
110 this.gameObject.transform.rotation,
111 this.mirrorX,
112 this.mirrorY,
113 this.alpha
114 );
115 }
116
117 setAnimation(label, defaultIndex, cycleSpeed = -1)
118 {
119 if(this.cycle)
120 {
121 if(this.cycle.label == label)
122 {
123 this.cycle.defaultIndex = defaultIndex;
124 this.cycle.cycleSpeed = cycleSpeed;
125 return;
126 }
127 }
128
129 this.cycle = {
130 label: label,
131 cycleArray: this.sprite.animationCycles[label],
132 default: defaultIndex,
133 current: defaultIndex,
134 cycleSpeed: cycleSpeed,
135 timer: 0};
136
137 this.subImage = this.cycle.cycleArray[this.cycle.current];
138 }
139
140 setAnimationCycleSpeed(cycleSpeed)
141 {
142 if(this.cycle)
143 {
144 this.cycle.cycleSpeed = cycleSpeed;
145 }
146 }
147
148 setSubImage(subImage)
149 {
150 this.subImage = subImage;
151 this.cycle = null;
152 }
153}