BlueGit

blue-star speeltuin

tweak controls, animations

timfa Tim · pushed by timfa

Authored 2022-10-18 · 4 years ago

Hash
8a6b1f4c8b89440ddbcdb03a6fbfbf847359886f
Parent
af3d26e9
4 files changed +117 −68
build/framework.js +5 −4
@@ -174,7 +174,7 @@ class image{
174 174 this.subImagesPerRow = perRow;
175 175 this.g_image = image;
176 176
177 this.id="loadedImage";
177 image.id="loadedImage";
178 178 document.body.append(image);
179 179 console.log("Image loaded: " + image.src);
180 180 }
@@ -403,9 +403,8 @@ class Renderer
403 403 {
404 404 this.cycle.timer -= this.cycle.cycleSpeed;
405 405
406 this.cycle.cycleArray[this.cycle.current + 1] ?
407 this.updateCycleSprite(this.cycle.current + 1) :
408 this.updateCycleSprite(0);
406 let next = (this.cycle.current + 1) % this.cycle.cycleArray.length;
407 this.updateCycleSprite(next)
409 408 }
410 409 }
411 410 }
@@ -484,6 +483,8 @@ class Renderer
484 483 current: defaultIndex,
485 484 cycleSpeed: cycleSpeed,
486 485 timer: 0};
486
487 this.subImage = this.cycle.cycleArray[this.cycle.current];
487 488 }
488 489
489 490 setAnimationCycleSpeed(cycleSpeed)
@@ -174,7 +174,7 @@ class image{
174 this.subImagesPerRow = perRow;
174 this.subImagesPerRow = perRow;
175 this.g_image = image;
175 this.g_image = image;
176
176
177 this.id="loadedImage";
177 image.id="loadedImage";
178 document.body.append(image);
178 document.body.append(image);
179 console.log("Image loaded: " + image.src);
179 console.log("Image loaded: " + image.src);
180 }
180 }
@@ -403,9 +403,8 @@ class Renderer
403 {
403 {
404 this.cycle.timer -= this.cycle.cycleSpeed;
404 this.cycle.timer -= this.cycle.cycleSpeed;
405
405
406 this.cycle.cycleArray[this.cycle.current + 1] ?
406 let next = (this.cycle.current + 1) % this.cycle.cycleArray.length;
407 this.updateCycleSprite(this.cycle.current + 1) :
407 this.updateCycleSprite(next)
408 this.updateCycleSprite(0);
409 }
408 }
410 }
409 }
411 }
410 }
@@ -484,6 +483,8 @@ class Renderer
484 current: defaultIndex,
483 current: defaultIndex,
485 cycleSpeed: cycleSpeed,
484 cycleSpeed: cycleSpeed,
486 timer: 0};
485 timer: 0};
486
487 this.subImage = this.cycle.cycleArray[this.cycle.current];
487 }
488 }
488
489
489 setAnimationCycleSpeed(cycleSpeed)
490 setAnimationCycleSpeed(cycleSpeed)
src/game.js +17 −12
@@ -1,7 +1,7 @@
1 1 console.log("Game started!");
2 var game = new Game();
2 3 window.onload = function()
3 4 {
4 var game = new Game();
5 5
6 6 game.preloadImagesThenStart(
7 7 [
@@ -50,13 +50,17 @@ window.onload = function()
50 50
51 51 function setupChaoAnimations(image)
52 52 {
53 image.addAnimationCycle("walkDown", [0, 1, 2])
54 image.addAnimationCycle("walkLeft", [3, 4, 5])
55 image.addAnimationCycle("walkUp", [6, 7, 8])
53 image.addAnimationCycle("walkDown", [0, 1, 0, 2])
54 image.addAnimationCycle("walkLeft", [3, 4, 3, 5])
55 image.addAnimationCycle("walkUp", [6, 7, 6, 8])
56 56
57 image.addAnimationCycle("flyDown", [9, 10, 11])
58 image.addAnimationCycle("flyLeft", [12, 13, 14])
59 image.addAnimationCycle("flyUp", [15, 16, 17])
57 image.addAnimationCycle("standDown", [0])
58 image.addAnimationCycle("standLeft", [3])
59 image.addAnimationCycle("standUp", [6])
60
61 image.addAnimationCycle("flyDown", [9, 10, 11, 10])
62 image.addAnimationCycle("flyLeft", [12, 13, 14, 13])
63 image.addAnimationCycle("flyUp", [15, 16, 17, 16])
60 64
61 65 image.addAnimationCycle("tripDown", [18, 19, 20])
62 66 image.addAnimationCycle("tripLeft", [21, 22, 23])
@@ -65,7 +69,7 @@ function setupChaoAnimations(image)
65 69 image.addAnimationCycle("sitDown", [27])
66 70 image.addAnimationCycle("sitLeft", [28])
67 71
68 image.addAnimationCycle("tantrum", [29, 30, 31, 32, 33])
72 image.addAnimationCycle("tantrum", [29, 30, 29, 31, 32, 33])
69 73
70 74 image.addAnimationCycle("swimStruggle", [34, 35])
71 75 image.addAnimationCycle("beginSit", [36, 37, 38])
@@ -75,9 +79,10 @@ function setupChaoAnimations(image)
75 79
76 80 image.addAnimationCycle("hungry", [43, 44])
77 81
78 image.addAnimationCycle("no", [45, 46, 47])
82 image.addAnimationCycle("no", [45, 46, 45, 47])
79 83 image.addAnimationCycle("think", [48])
80 image.addAnimationCycle("happy", [49, 50, 51, 52])
84 image.addAnimationCycle("happy", [49, 50, 49, 51])
85 image.addAnimationCycle("sad", [52])
81 86 image.addAnimationCycle("sleep", [53, 54])
82 87
83 88 image.addAnimationCycle("swim", [55, 56, 57])
@@ -94,6 +99,6 @@ createMainScene = function(game)
94 99 game.loadScene(scene);
95 100
96 101 scene.addObject(new GrassTiler(50, 50, game.images.arrow, 0));
97 frog = new Frog(60,60, game.images.frog_basic, 0);
98 scene.addObject(frog);
102 chao = new Chao(60,60, game.images.chao_default, 0);
103 scene.addObject(chao);
99 104 }
@@ -1,7 +1,7 @@
1console.log("Game started!");
1console.log("Game started!");
2var game = new Game();
2window.onload = function()
3window.onload = function()
3{
4{
4 var game = new Game();
5
5
6 game.preloadImagesThenStart(
6 game.preloadImagesThenStart(
7 [
7 [
@@ -50,13 +50,17 @@ window.onload = function()
50
50
51function setupChaoAnimations(image)
51function setupChaoAnimations(image)
52{
52{
53 image.addAnimationCycle("walkDown", [0, 1, 2])
53 image.addAnimationCycle("walkDown", [0, 1, 0, 2])
54 image.addAnimationCycle("walkLeft", [3, 4, 5])
54 image.addAnimationCycle("walkLeft", [3, 4, 3, 5])
55 image.addAnimationCycle("walkUp", [6, 7, 8])
55 image.addAnimationCycle("walkUp", [6, 7, 6, 8])
56
56
57 image.addAnimationCycle("flyDown", [9, 10, 11])
57 image.addAnimationCycle("standDown", [0])
58 image.addAnimationCycle("flyLeft", [12, 13, 14])
58 image.addAnimationCycle("standLeft", [3])
59 image.addAnimationCycle("flyUp", [15, 16, 17])
59 image.addAnimationCycle("standUp", [6])
60
61 image.addAnimationCycle("flyDown", [9, 10, 11, 10])
62 image.addAnimationCycle("flyLeft", [12, 13, 14, 13])
63 image.addAnimationCycle("flyUp", [15, 16, 17, 16])
60
64
61 image.addAnimationCycle("tripDown", [18, 19, 20])
65 image.addAnimationCycle("tripDown", [18, 19, 20])
62 image.addAnimationCycle("tripLeft", [21, 22, 23])
66 image.addAnimationCycle("tripLeft", [21, 22, 23])
@@ -65,7 +69,7 @@ function setupChaoAnimations(image)
65 image.addAnimationCycle("sitDown", [27])
69 image.addAnimationCycle("sitDown", [27])
66 image.addAnimationCycle("sitLeft", [28])
70 image.addAnimationCycle("sitLeft", [28])
67
71
68 image.addAnimationCycle("tantrum", [29, 30, 31, 32, 33])
72 image.addAnimationCycle("tantrum", [29, 30, 29, 31, 32, 33])
69
73
70 image.addAnimationCycle("swimStruggle", [34, 35])
74 image.addAnimationCycle("swimStruggle", [34, 35])
71 image.addAnimationCycle("beginSit", [36, 37, 38])
75 image.addAnimationCycle("beginSit", [36, 37, 38])
@@ -75,9 +79,10 @@ function setupChaoAnimations(image)
75
79
76 image.addAnimationCycle("hungry", [43, 44])
80 image.addAnimationCycle("hungry", [43, 44])
77
81
78 image.addAnimationCycle("no", [45, 46, 47])
82 image.addAnimationCycle("no", [45, 46, 45, 47])
79 image.addAnimationCycle("think", [48])
83 image.addAnimationCycle("think", [48])
80 image.addAnimationCycle("happy", [49, 50, 51, 52])
84 image.addAnimationCycle("happy", [49, 50, 49, 51])
85 image.addAnimationCycle("sad", [52])
81 image.addAnimationCycle("sleep", [53, 54])
86 image.addAnimationCycle("sleep", [53, 54])
82
87
83 image.addAnimationCycle("swim", [55, 56, 57])
88 image.addAnimationCycle("swim", [55, 56, 57])
@@ -94,6 +99,6 @@ createMainScene = function(game)
94 game.loadScene(scene);
99 game.loadScene(scene);
95
100
96 scene.addObject(new GrassTiler(50, 50, game.images.arrow, 0));
101 scene.addObject(new GrassTiler(50, 50, game.images.arrow, 0));
97 frog = new Frog(60,60, game.images.frog_basic, 0);
102 chao = new Chao(60,60, game.images.chao_default, 0);
98 scene.addObject(frog);
103 scene.addObject(chao);
99}
104}
src/objects/arrow.js deleted +0 −52
@@ -1,52 +0,0 @@
1 class Arrow extends GameObject
2 {
3 speed = 250;
4
5 constructor(x, y, sprite, layer)
6 {
7 super(x, y, sprite, layer);
8 this.transform.angularVelocity = 40;
9 }
10
11 onClick(btn)
12 {
13 console.log(btn);
14 }
15
16 update(deltaTime)
17 {
18 super.update(deltaTime);
19
20 let oldPos = this.transform.position.getCopy();
21
22 if(GameInput.isHeld(GameInput.keys.ArrowRight))
23 {
24 this.transform.position.x += this.speed * deltaTime;
25 CookieManager.setCookie("arrow", "right");
26 }
27
28 if(GameInput.isHeld(GameInput.keys.ArrowLeft))
29 {
30 this.transform.position.x -= this.speed * deltaTime;
31 CookieManager.setCookie("arrow", "left")
32 }
33
34 if(GameInput.isHeld(GameInput.keys.ArrowUp))
35 {
36 this.transform.position.y -= this.speed * deltaTime;
37 CookieManager.setCookie("arrow", "up")
38 }
39
40 if(GameInput.isHeld(GameInput.keys.ArrowDown))
41 {
42 this.transform.position.y += this.speed * deltaTime;
43 CookieManager.setCookie("arrow", "down")
44 }
45
46 if(oldPos.distanceTo(this.transform.position) > 1)
47 {
48 this.transform.pointTowards(oldPos)
49 this.transform.rotation += 180;
50 }
51 }
52 }
@@ -1,52 +0,0 @@
1class Arrow extends GameObject
2{
3 speed = 250;
4
5 constructor(x, y, sprite, layer)
6 {
7 super(x, y, sprite, layer);
8 this.transform.angularVelocity = 40;
9 }
10
11 onClick(btn)
12 {
13 console.log(btn);
14 }
15
16 update(deltaTime)
17 {
18 super.update(deltaTime);
19
20 let oldPos = this.transform.position.getCopy();
21
22 if(GameInput.isHeld(GameInput.keys.ArrowRight))
23 {
24 this.transform.position.x += this.speed * deltaTime;
25 CookieManager.setCookie("arrow", "right");
26 }
27
28 if(GameInput.isHeld(GameInput.keys.ArrowLeft))
29 {
30 this.transform.position.x -= this.speed * deltaTime;
31 CookieManager.setCookie("arrow", "left")
32 }
33
34 if(GameInput.isHeld(GameInput.keys.ArrowUp))
35 {
36 this.transform.position.y -= this.speed * deltaTime;
37 CookieManager.setCookie("arrow", "up")
38 }
39
40 if(GameInput.isHeld(GameInput.keys.ArrowDown))
41 {
42 this.transform.position.y += this.speed * deltaTime;
43 CookieManager.setCookie("arrow", "down")
44 }
45
46 if(oldPos.distanceTo(this.transform.position) > 1)
47 {
48 this.transform.pointTowards(oldPos)
49 this.transform.rotation += 180;
50 }
51 }
52}
src/objects/chao.js added +95 −0
@@ -0,0 +1,95 @@
1 class Chao extends GameObject
2 {
3 speed = 30;
4
5 constructor(x, y, sprite, layer)
6 {
7 super(x, y, sprite, layer);
8 this.wasHeld = {}
9 }
10
11 onClick(btn)
12 {
13 console.log(btn);
14 }
15
16 anyHeld()
17 {
18 return this.wasHeld.right || this.wasHeld.left || this.wasHeld.up || this.wasHeld.down;
19 }
20
21 update(deltaTime)
22 {
23 super.update(deltaTime);
24 this.wasHeld = this.wasHeld || {}
25
26 if(this.wasHeld.right && !GameInput.isHeld(GameInput.keys.ArrowRight))
27 {
28 this.wasHeld.right = false;
29 if(!this.anyHeld())
30 this.renderer.setAnimation("standLeft", 0, .2);
31 }
32
33 if(this.wasHeld.left && !GameInput.isHeld(GameInput.keys.ArrowLeft))
34 {
35 this.wasHeld.left = false;
36 if(!this.anyHeld())
37 this.renderer.setAnimation("standLeft", 0, .2);
38 }
39
40 if(this.wasHeld.up && !GameInput.isHeld(GameInput.keys.ArrowUp))
41 {
42 this.wasHeld.up = false;
43 if(!this.anyHeld())
44 this.renderer.setAnimation("standUp", 0, .2);
45 }
46
47 if(this.wasHeld.down && !GameInput.isHeld(GameInput.keys.ArrowDown))
48 {
49 this.wasHeld.down = false;
50 if(!this.anyHeld())
51 this.renderer.setAnimation("standDown", 0, .2);
52 }
53
54 if(GameInput.isHeld(GameInput.keys.ArrowRight) && !this.wasHeld.left && !this.wasHeld.up && !this.wasHeld.down)
55 {
56 this.transform.position.x += this.speed * deltaTime;
57 CookieManager.setCookie("arrow", "right");
58
59 if(!this.wasHeld.right)
60 this.renderer.setAnimation("walkLeft", 0, .2);
61 this.renderer.mirrorX = true;
62 this.wasHeld.right = true;
63 }
64 else if(GameInput.isHeld(GameInput.keys.ArrowLeft) && !this.wasHeld.right && !this.wasHeld.up && !this.wasHeld.down)
65 {
66 this.transform.position.x -= this.speed * deltaTime;
67 CookieManager.setCookie("arrow", "left")
68
69 if(!this.wasHeld.left)
70 this.renderer.setAnimation("walkLeft", 0, .2);
71 this.renderer.mirrorX = false;
72 this.wasHeld.left = true;
73 }
74 else if(GameInput.isHeld(GameInput.keys.ArrowUp) && !this.wasHeld.left && !this.wasHeld.right && !this.wasHeld.down)
75 {
76 this.transform.position.y -= this.speed * deltaTime;
77 CookieManager.setCookie("arrow", "up")
78
79 if(!this.wasHeld.up)
80 this.renderer.setAnimation("walkUp", 0, .2);
81 this.renderer.mirrorX = false;
82 this.wasHeld.up = true;
83 }
84 else if(GameInput.isHeld(GameInput.keys.ArrowDown) && !this.wasHeld.left && !this.wasHeld.up && !this.wasHeld.right)
85 {
86 this.transform.position.y += this.speed * deltaTime;
87 CookieManager.setCookie("arrow", "down")
88
89 if(!this.wasHeld.down)
90 this.renderer.setAnimation("walkDown", 0, .2);
91 this.renderer.mirrorX = false;
92 this.wasHeld.down = true;
93 }
94 }
95 }
@@ -0,0 +1,95 @@
1class Chao extends GameObject
2{
3 speed = 30;
4
5 constructor(x, y, sprite, layer)
6 {
7 super(x, y, sprite, layer);
8 this.wasHeld = {}
9 }
10
11 onClick(btn)
12 {
13 console.log(btn);
14 }
15
16 anyHeld()
17 {
18 return this.wasHeld.right || this.wasHeld.left || this.wasHeld.up || this.wasHeld.down;
19 }
20
21 update(deltaTime)
22 {
23 super.update(deltaTime);
24 this.wasHeld = this.wasHeld || {}
25
26 if(this.wasHeld.right && !GameInput.isHeld(GameInput.keys.ArrowRight))
27 {
28 this.wasHeld.right = false;
29 if(!this.anyHeld())
30 this.renderer.setAnimation("standLeft", 0, .2);
31 }
32
33 if(this.wasHeld.left && !GameInput.isHeld(GameInput.keys.ArrowLeft))
34 {
35 this.wasHeld.left = false;
36 if(!this.anyHeld())
37 this.renderer.setAnimation("standLeft", 0, .2);
38 }
39
40 if(this.wasHeld.up && !GameInput.isHeld(GameInput.keys.ArrowUp))
41 {
42 this.wasHeld.up = false;
43 if(!this.anyHeld())
44 this.renderer.setAnimation("standUp", 0, .2);
45 }
46
47 if(this.wasHeld.down && !GameInput.isHeld(GameInput.keys.ArrowDown))
48 {
49 this.wasHeld.down = false;
50 if(!this.anyHeld())
51 this.renderer.setAnimation("standDown", 0, .2);
52 }
53
54 if(GameInput.isHeld(GameInput.keys.ArrowRight) && !this.wasHeld.left && !this.wasHeld.up && !this.wasHeld.down)
55 {
56 this.transform.position.x += this.speed * deltaTime;
57 CookieManager.setCookie("arrow", "right");
58
59 if(!this.wasHeld.right)
60 this.renderer.setAnimation("walkLeft", 0, .2);
61 this.renderer.mirrorX = true;
62 this.wasHeld.right = true;
63 }
64 else if(GameInput.isHeld(GameInput.keys.ArrowLeft) && !this.wasHeld.right && !this.wasHeld.up && !this.wasHeld.down)
65 {
66 this.transform.position.x -= this.speed * deltaTime;
67 CookieManager.setCookie("arrow", "left")
68
69 if(!this.wasHeld.left)
70 this.renderer.setAnimation("walkLeft", 0, .2);
71 this.renderer.mirrorX = false;
72 this.wasHeld.left = true;
73 }
74 else if(GameInput.isHeld(GameInput.keys.ArrowUp) && !this.wasHeld.left && !this.wasHeld.right && !this.wasHeld.down)
75 {
76 this.transform.position.y -= this.speed * deltaTime;
77 CookieManager.setCookie("arrow", "up")
78
79 if(!this.wasHeld.up)
80 this.renderer.setAnimation("walkUp", 0, .2);
81 this.renderer.mirrorX = false;
82 this.wasHeld.up = true;
83 }
84 else if(GameInput.isHeld(GameInput.keys.ArrowDown) && !this.wasHeld.left && !this.wasHeld.up && !this.wasHeld.right)
85 {
86 this.transform.position.y += this.speed * deltaTime;
87 CookieManager.setCookie("arrow", "down")
88
89 if(!this.wasHeld.down)
90 this.renderer.setAnimation("walkDown", 0, .2);
91 this.renderer.mirrorX = false;
92 this.wasHeld.down = true;
93 }
94 }
95}