BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / objectComponents / renderer.js

renderer.js

small logics change
timfa wsadhelios · pushed by timfa · 4 years ago
b60063ca History
renderer.js 3.5 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 this.cycle.cycleArray[this.cycle.current + 1] ?
37 this.updateCycleSprite(this.cycle.current + 1) :
38 this.updateCycleSprite(0);
39 }
40 }
41 }
42
43 updateCycleSprite(index) {
44 this.subImage = this.cycle.cycleArray[index];
45 this.cycle.current = index;
46 }
47
48 draw()
49 {
50 if(this.sprite)
51 {
52 var offX = this.gameObject.scene.cameraPosition.x;
53 var offY = this.gameObject.scene.cameraPosition.y;
54
55 this.drawSprite(this.gameObject.transform.position.x + offX,
56 this.gameObject.transform.position.y + offY,
57 this.gameObject.transform.size.x,
58 this.gameObject.transform.size.y,
59 this.gameObject.transform.rotation);
60 }
61 }
62
63 drawSprite()
64 {
65 if(isNaN(this.subImage))
66 {
67 this.subImage = this.g_subImgOld;
68 }
69 else
70 {
71 this.g_subImgOld = this.subImage;
72 }
73
74 var subImage = Math.round(this.subImage);
75 subImage %= this.sprite.subImages;
76
77 var c = subImage % this.sprite.subImagesPerRow;
78 var r = Math.floor(subImage / this.sprite.subImagesPerRow);
79 var sw = (this.sprite.g_image.width / this.sprite.subImagesPerRow);
80 var sh = (this.sprite.g_image.height / Math.ceil(this.sprite.subImages / this.sprite.subImagesPerRow));
81 var sx = sw * c;
82 var sy = sh * r;
83
84 this.sprite.draw(
85 this.gameObject.scene,
86 sx, sy, sw, sh,
87 this.gameObject.transform.position.x,
88 this.gameObject.transform.position.y,
89 this.gameObject.transform.size.x,
90 this.gameObject.transform.size.y,
91 this.gameObject.transform.rotation,
92 this.mirrorX,
93 this.mirrorY,
94 this.alpha
95 );
96 }
97
98 setAnimation(label, defaultIndex, cycleSpeed = -1)
99 {
100 if(this.cycle)
101 {
102 if(this.cycle.label == label)
103 {
104 this.cycle.defaultIndex = defaultIndex;
105 this.cycle.cycleSpeed = cycleSpeed;
106 return;
107 }
108 }
109
110 this.cycle = {
111 label: label,
112 cycleArray: this.sprite.animationCycles[label],
113 default: defaultIndex,
114 current: defaultIndex,
115 cycleSpeed: cycleSpeed,
116 timer: 0};
117 }
118
119 setAnimationCycleSpeed(cycleSpeed)
120 {
121 if(this.cycle)
122 {
123 this.cycle.cycleSpeed = cycleSpeed;
124 }
125 }
126
127 setSubImage(subImage)
128 {
129 this.subImage = subImage;
130 this.cycle = null;
131 }
132}