BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / objectComponents / renderer.js

renderer.js

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