BlueGit

timfa frameworkv3

Merge branch 'animation' into 'master'

Updated renderer with update + animation cycling

See merge request Timfa/frameworkv3!2

timfa Tim Falken · pushed by timfa

Authored 2022-10-17 · 4 years ago

Hash
6c4ab492e2a0aaa7f460071e6152a0d0286458c3
3 files changed +107 −27
framework/engine/gameobject.js +1 −0
@@ -18,6 +18,7 @@ class GameObject
18 18 {
19 19 this.transform.update(deltaTime);
20 20 this.collider.update(deltaTime);
21 this.renderer.update(deltaTime);
21 22 }
22 23
23 24 destroy()
@@ -18,6 +18,7 @@ class GameObject
18 {
18 {
19 this.transform.update(deltaTime);
19 this.transform.update(deltaTime);
20 this.collider.update(deltaTime);
20 this.collider.update(deltaTime);
21 this.renderer.update(deltaTime);
21 }
22 }
22
23
23 destroy()
24 destroy()
framework/engine/image.js +37 −27
@@ -1,31 +1,26 @@
1 function image(image, subImgTotal, perRow)
2 {
3 subImgTotal = subImgTotal || 1;
4 perRow = perRow || 1;
5
6 this.subImages = subImgTotal;
7 this.subImagesPerRow = perRow;
8 this.g_image = image;
9 image.id="loadedImage";
10 document.body.append(image);
11
12 Object.defineProperty(this, "width",
13 {
14 get: function()
15 {
16 return this.g_image.width / this.subImagesPerRow;
17 }
18 });
1 class image{
2 subImages = null;
3 subImagesPerRow = null;
19 4
20 Object.defineProperty(this, "height",
5 g_image = null;
6
7 animationCycles = {};
8
9 constructor(image, subImgTotal = 1, perRow = 1)
21 10 {
22 get: function()
23 {
24 return this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);
25 }
26 });
11 subImgTotal = subImgTotal || 1;
12 perRow = perRow || 1;
13
14 this.subImages = subImgTotal;
15 this.subImagesPerRow = perRow;
16 this.g_image = image;
27 17
28 this.draw = function(scene, sx, sy, sw, sh, dx, dy, dw, dh, rotation, alpha)
18 image.id="loadedImage";
19 document.body.append(image);
20 console.log("Image loaded: " + image.src);
21 }
22
23 draw(scene, sx, sy, sw, sh, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha)
29 24 {
30 25 var shouldDraw = true;
31 26
@@ -51,12 +46,27 @@ function image(image, subImgTotal, perRow)
51 46 context.translate(dx, dy);
52 47 context.rotate(rotation * (Math.PI/180));
53 48
54 context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh);
49 (mirrorX || mirrorY) && context.scale(mirrorX?-1:1, mirrorY?-1:1);
55 50
51 context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh);
52
56 53 context.restore();
57 54 context.globalAlpha = oldAlpha;
58 55 }
59 56 }
60 57
61 console.log("Image loaded: " + image.src);
58 addAnimationCycle(label, cycle)
59 {
60 this.animationCycles[label] = cycle;
61 }
62
63 get width()
64 {
65 return this.g_image.width / this.subImagesPerRow;
66 }
67
68 get height()
69 {
70 return this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);
71 }
62 72 }
@@ -1,31 +1,26 @@
1function image(image, subImgTotal, perRow)
1class image{
2{
2 subImages = null;
3 subImgTotal = subImgTotal || 1;
3 subImagesPerRow = null;
4 perRow = perRow || 1;
5
6 this.subImages = subImgTotal;
7 this.subImagesPerRow = perRow;
8 this.g_image = image;
9 image.id="loadedImage";
10 document.body.append(image);
11
12 Object.defineProperty(this, "width",
13 {
14 get: function()
15 {
16 return this.g_image.width / this.subImagesPerRow;
17 }
18 });
19
4
20 Object.defineProperty(this, "height",
5 g_image = null;
6
7 animationCycles = {};
8
9 constructor(image, subImgTotal = 1, perRow = 1)
21 {
10 {
22 get: function()
11 subImgTotal = subImgTotal || 1;
23 {
12 perRow = perRow || 1;
24 return this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);
13
25 }
14 this.subImages = subImgTotal;
26 });
15 this.subImagesPerRow = perRow;
16 this.g_image = image;
27
17
28 this.draw = function(scene, sx, sy, sw, sh, dx, dy, dw, dh, rotation, alpha)
18 image.id="loadedImage";
19 document.body.append(image);
20 console.log("Image loaded: " + image.src);
21 }
22
23 draw(scene, sx, sy, sw, sh, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha)
29 {
24 {
30 var shouldDraw = true;
25 var shouldDraw = true;
31
26
@@ -51,12 +46,27 @@ function image(image, subImgTotal, perRow)
51 context.translate(dx, dy);
46 context.translate(dx, dy);
52 context.rotate(rotation * (Math.PI/180));
47 context.rotate(rotation * (Math.PI/180));
53
48
54 context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh);
49 (mirrorX || mirrorY) && context.scale(mirrorX?-1:1, mirrorY?-1:1);
55
50
51 context.drawImage(this.g_image, sx, sy, sw, sh, -dw/2, -dh/2, dw, dh);
52
56 context.restore();
53 context.restore();
57 context.globalAlpha = oldAlpha;
54 context.globalAlpha = oldAlpha;
58 }
55 }
59 }
56 }
60
57
61 console.log("Image loaded: " + image.src);
58 addAnimationCycle(label, cycle)
59 {
60 this.animationCycles[label] = cycle;
61 }
62
63 get width()
64 {
65 return this.g_image.width / this.subImagesPerRow;
66 }
67
68 get height()
69 {
70 return this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);
71 }
62}
72}
framework/engine/objectComponents/renderer.js +69 −0
@@ -4,6 +4,10 @@ class Renderer
4 4 gameObject = null;
5 5 subImage = 0;
6 6 alpha = 1;
7 mirrorX = false;
8 mirrorY = false;
9
10 cycle = null;
7 11
8 12 visible = true;
9 13
@@ -13,6 +17,34 @@ class Renderer
13 17 this.sprite = sprite;
14 18 }
15 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
16 48 draw()
17 49 {
18 50 if(this.sprite)
@@ -57,7 +89,44 @@ class Renderer
57 89 this.gameObject.transform.size.x,
58 90 this.gameObject.transform.size.y,
59 91 this.gameObject.transform.rotation,
92 this.mirrorX,
93 this.mirrorY,
60 94 this.alpha
61 95 );
62 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 }
63 132 }
@@ -4,6 +4,10 @@ class Renderer
4 gameObject = null;
4 gameObject = null;
5 subImage = 0;
5 subImage = 0;
6 alpha = 1;
6 alpha = 1;
7 mirrorX = false;
8 mirrorY = false;
9
10 cycle = null;
7
11
8 visible = true;
12 visible = true;
9
13
@@ -13,6 +17,34 @@ class Renderer
13 this.sprite = sprite;
17 this.sprite = sprite;
14 }
18 }
15
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
16 draw()
48 draw()
17 {
49 {
18 if(this.sprite)
50 if(this.sprite)
@@ -57,7 +89,44 @@ class Renderer
57 this.gameObject.transform.size.x,
89 this.gameObject.transform.size.x,
58 this.gameObject.transform.size.y,
90 this.gameObject.transform.size.y,
59 this.gameObject.transform.rotation,
91 this.gameObject.transform.rotation,
92 this.mirrorX,
93 this.mirrorY,
60 this.alpha
94 this.alpha
61 );
95 );
62 }
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 }
63}
132}