BlueGit

blue-star speeltuin

Merge branch 'master' of https://gitlab.com/spark-imagination/speeltuin

timfa wsadhelios · pushed by timfa

Authored 2022-10-20 · 4 years ago

Hash
affdc9b8cd8bb88c27477f84d0aae9ba5e67129c
4 files changed +124 −21
.vscode/settings.json added +10 −0
@@ -0,0 +1,10 @@
1 {
2 "javascript.format.placeOpenBraceOnNewLineForFunctions": true,
3 "typescript.format.placeOpenBraceOnNewLineForFunctions": true,
4 "editor.formatOnSave": true,
5 "javascript.format.placeOpenBraceOnNewLineForControlBlocks": true,
6 "javascript.format.semicolons": "insert",
7 "javascript.format.insertSpaceBeforeFunctionParenthesis": true,
8 "javascript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
9 "javascript.format.insertSpaceAfterConstructor": true
10 }
@@ -0,0 +1,10 @@
1{
2 "javascript.format.placeOpenBraceOnNewLineForFunctions": true,
3 "typescript.format.placeOpenBraceOnNewLineForFunctions": true,
4 "editor.formatOnSave": true,
5 "javascript.format.placeOpenBraceOnNewLineForControlBlocks": true,
6 "javascript.format.semicolons": "insert",
7 "javascript.format.insertSpaceBeforeFunctionParenthesis": true,
8 "javascript.format.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
9 "javascript.format.insertSpaceAfterConstructor": true
10}
src/chaoBehaviour/wander.js added +13 −0
@@ -0,0 +1,13 @@
1 class BehaviourWander extends ChaoBehaviour
2 {
3 startBehaviour()
4 {
5 Chao.Instance.walkToPosition(new vector(Math.randomRange(0, Chao.Instance.scene.real_size.x), Math.randomRange(0, Chao.Instance.scene.real_size.y)));
6 }
7
8 update(deltaTime)
9 {
10 if(!Chao.Instance.walking)
11 this.endBehaviour();
12 }
13 }
@@ -0,0 +1,13 @@
1class BehaviourWander extends ChaoBehaviour
2{
3 startBehaviour()
4 {
5 Chao.Instance.walkToPosition(new vector(Math.randomRange(0, Chao.Instance.scene.real_size.x), Math.randomRange(0, Chao.Instance.scene.real_size.y)));
6 }
7
8 update(deltaTime)
9 {
10 if(!Chao.Instance.walking)
11 this.endBehaviour();
12 }
13}
src/objects/chao.js +96 −17
@@ -2,11 +2,13 @@ class Chao extends GameObject
2 2 {
3 3 speed = 40;
4 4 scenePadding = 50;
5 prevPosition = null;
5 6 targetDestination = null;
7 hBeforeV = false;
6 8
7 9 minTimeBetweenBehaviour = 3; //in seconds
8 10 maxTimeBetweenbehaviour = 10; // in seconds
9 behaviourTimer = 10;
11 behaviourTimer = 3;
10 12
11 13 behaviourList = [];
12 14 currentBehaviour = null;
@@ -15,67 +17,144 @@ class Chao extends GameObject
15 17
16 18 static Instance;
17 19
18 get busyWithBehaviour()
20 get busyWithBehaviour ()
19 21 {
20 22 return this.currentBehaviour != null;
21 23 }
22 24
23 constructor(x, y, sprite, layer)
25 get walking ()
26 {
27 return this.transform.position.distanceTo(this.targetDestination) > 5;
28 }
29
30 constructor (x, y, sprite, layer)
24 31 {
25 32 super(x, y, sprite, layer);
26 this.wasHeld = {}
33 this.wasHeld = {};
27 34 this.collider.enabled = true;
28 35 Chao.Instance = this;
36 this.targetDestination = new vector(x, y);
29 37 }
30 38
31 registerBehaviour(behaviour)
39 registerBehaviour (behaviour)
32 40 {
33 41 this.behaviourList.push(behaviour);
34 42 }
35 43
36 endBehaviour()
44 endBehaviour ()
37 45 {
46 console.log("Ending Behaviour '" + this.currentBehaviour.constructor.name + "'");
38 47 this.currentBehaviour = null;
39 48 this.behaviourTimer = Math.randomRange(this.minTimeBetweenBehaviour, this.maxTimeBetweenbehaviour);
40 49 }
41 50
42 addedToScene()
51 addedToScene ()
43 52 {
44 53 this.renderer.sprite = this.scene.game.chaoImages.random();
45 54
46 55 this.renderer.defaultAnimationSpeed = 0.2;
47 56 this.registerBehaviour(new BehaviourSneeze());
57 this.registerBehaviour(new BehaviourWander());
58
48 59 }
49 60
50 onClick(btn)
61 onClick (btn)
51 62 {
52 63 console.log(btn);
53 64 }
54 65
55 anyHeld()
66 anyHeld ()
56 67 {
57 68 return this.wasHeld.right || this.wasHeld.left || this.wasHeld.up || this.wasHeld.down;
58 69 }
59 70
60 update(deltaTime)
71 walkToPosition (position)
72 {
73 console.log("Walking towards", position);
74 this.targetDestination = position.getCopy();
75 this.prevPosition = this.transform.position.getCopy();
76 this.hBeforeV = Math.random() > 0.5;
77 }
78
79 update (deltaTime)
61 80 {
62 81 super.update(deltaTime);
63
64 if(this.busyWithBehaviour)
82 if (GameInput.isPressed(GameInput.keys.a))
83 {
84 this.walkToPosition(GameInput.mousePosition.getCopy());
85 }
86
87 if (this.busyWithBehaviour)
65 88 this.currentBehaviour.update(deltaTime);
66 89 else
67 90 {
68 this.behaviourTimer -= deltaTime;
91 if (!this.walking)
92 {
93 this.behaviourTimer -= deltaTime;
69 94
70 if(this.behaviourTimer <= 0)
95 if (this.behaviourTimer <= 0)
96 {
97 this.currentBehaviour = this.behaviourList.random();
98 console.log("Starting Behaviour '" + this.currentBehaviour.constructor.name + "'");
99 this.currentBehaviour.startBehaviour();
100 }
101 }
102 }
103
104 if (this.walking)
105 {
106 let halfTotalDistanceH = Math.abs(this.prevPosition.x - this.targetDestination.x) / 2;
107 let halfTotalDistanceV = Math.abs(this.prevPosition.y - this.targetDestination.y) / 2;
108
109 let currentDistanceH = Math.abs(this.transform.position.x - this.targetDestination.x);
110 let currentDistanceV = Math.abs(this.transform.position.y - this.targetDestination.y);
111
112 let hDir = -Math.sign(this.transform.position.x - this.targetDestination.x);
113 let vDir = -Math.sign(this.transform.position.y - this.targetDestination.y);
114
115 if (this.hBeforeV)
71 116 {
72 this.currentBehaviour = this.behaviourList.random();
73 this.currentBehaviour.startBehaviour();
117 if (currentDistanceH > halfTotalDistanceH || currentDistanceV < 1)
118 {
119 this.renderer.mirrorX = hDir > 0;
120 this.transform.velocity.x = this.speed * hDir;
121 this.transform.velocity.y = 0;
122 this.renderer.setAnimation("walkLeft");
123 }
124 else
125 {
126 this.transform.velocity.x = 0;
127 this.transform.velocity.y = this.speed * vDir;
128 this.renderer.setAnimation(vDir < 0 ? "walkUp" : "walkDown");
129 }
74 130 }
131 else
132 {
133 if (currentDistanceV > halfTotalDistanceV || currentDistanceH < 1)
134 {
135 this.transform.velocity.x = 0;
136 this.transform.velocity.y = this.speed * vDir;
137 this.renderer.setAnimation(vDir < 0 ? "walkUp" : "walkDown");
138 }
139 else
140 {
141 this.renderer.mirrorX = hDir > 0;
142 this.transform.velocity.x = this.speed * hDir;
143 this.transform.velocity.y = 0;
144 this.renderer.setAnimation("walkLeft");
145 }
146 }
147 }
148 else if (this.renderer.currentAnimationName && this.renderer.currentAnimationName.indexOf("walk") >= 0)
149 {
150 this.renderer.setAnimation(this.renderer.currentAnimationName.replace("walk", "stand"));
151
152 this.transform.velocity.x = 0;
153 this.transform.velocity.y = 0;
75 154 }
76 155 }
77 156
78 draw(context)
157 draw (context)
79 158 {
80 159 super.draw(context);
81 160 }
@@ -2,11 +2,13 @@ class Chao extends GameObject
2{
2{
3 speed = 40;
3 speed = 40;
4 scenePadding = 50;
4 scenePadding = 50;
5 prevPosition = null;
5 targetDestination = null;
6 targetDestination = null;
7 hBeforeV = false;
6
8
7 minTimeBetweenBehaviour = 3; //in seconds
9 minTimeBetweenBehaviour = 3; //in seconds
8 maxTimeBetweenbehaviour = 10; // in seconds
10 maxTimeBetweenbehaviour = 10; // in seconds
9 behaviourTimer = 10;
11 behaviourTimer = 3;
10
12
11 behaviourList = [];
13 behaviourList = [];
12 currentBehaviour = null;
14 currentBehaviour = null;
@@ -15,67 +17,144 @@ class Chao extends GameObject
15
17
16 static Instance;
18 static Instance;
17
19
18 get busyWithBehaviour()
20 get busyWithBehaviour ()
19 {
21 {
20 return this.currentBehaviour != null;
22 return this.currentBehaviour != null;
21 }
23 }
22
24
23 constructor(x, y, sprite, layer)
25 get walking ()
26 {
27 return this.transform.position.distanceTo(this.targetDestination) > 5;
28 }
29
30 constructor (x, y, sprite, layer)
24 {
31 {
25 super(x, y, sprite, layer);
32 super(x, y, sprite, layer);
26 this.wasHeld = {}
33 this.wasHeld = {};
27 this.collider.enabled = true;
34 this.collider.enabled = true;
28 Chao.Instance = this;
35 Chao.Instance = this;
36 this.targetDestination = new vector(x, y);
29 }
37 }
30
38
31 registerBehaviour(behaviour)
39 registerBehaviour (behaviour)
32 {
40 {
33 this.behaviourList.push(behaviour);
41 this.behaviourList.push(behaviour);
34 }
42 }
35
43
36 endBehaviour()
44 endBehaviour ()
37 {
45 {
46 console.log("Ending Behaviour '" + this.currentBehaviour.constructor.name + "'");
38 this.currentBehaviour = null;
47 this.currentBehaviour = null;
39 this.behaviourTimer = Math.randomRange(this.minTimeBetweenBehaviour, this.maxTimeBetweenbehaviour);
48 this.behaviourTimer = Math.randomRange(this.minTimeBetweenBehaviour, this.maxTimeBetweenbehaviour);
40 }
49 }
41
50
42 addedToScene()
51 addedToScene ()
43 {
52 {
44 this.renderer.sprite = this.scene.game.chaoImages.random();
53 this.renderer.sprite = this.scene.game.chaoImages.random();
45
54
46 this.renderer.defaultAnimationSpeed = 0.2;
55 this.renderer.defaultAnimationSpeed = 0.2;
47 this.registerBehaviour(new BehaviourSneeze());
56 this.registerBehaviour(new BehaviourSneeze());
57 this.registerBehaviour(new BehaviourWander());
58
48 }
59 }
49
60
50 onClick(btn)
61 onClick (btn)
51 {
62 {
52 console.log(btn);
63 console.log(btn);
53 }
64 }
54
65
55 anyHeld()
66 anyHeld ()
56 {
67 {
57 return this.wasHeld.right || this.wasHeld.left || this.wasHeld.up || this.wasHeld.down;
68 return this.wasHeld.right || this.wasHeld.left || this.wasHeld.up || this.wasHeld.down;
58 }
69 }
59
70
60 update(deltaTime)
71 walkToPosition (position)
72 {
73 console.log("Walking towards", position);
74 this.targetDestination = position.getCopy();
75 this.prevPosition = this.transform.position.getCopy();
76 this.hBeforeV = Math.random() > 0.5;
77 }
78
79 update (deltaTime)
61 {
80 {
62 super.update(deltaTime);
81 super.update(deltaTime);
63
82 if (GameInput.isPressed(GameInput.keys.a))
64 if(this.busyWithBehaviour)
83 {
84 this.walkToPosition(GameInput.mousePosition.getCopy());
85 }
86
87 if (this.busyWithBehaviour)
65 this.currentBehaviour.update(deltaTime);
88 this.currentBehaviour.update(deltaTime);
66 else
89 else
67 {
90 {
68 this.behaviourTimer -= deltaTime;
91 if (!this.walking)
92 {
93 this.behaviourTimer -= deltaTime;
69
94
70 if(this.behaviourTimer <= 0)
95 if (this.behaviourTimer <= 0)
96 {
97 this.currentBehaviour = this.behaviourList.random();
98 console.log("Starting Behaviour '" + this.currentBehaviour.constructor.name + "'");
99 this.currentBehaviour.startBehaviour();
100 }
101 }
102 }
103
104 if (this.walking)
105 {
106 let halfTotalDistanceH = Math.abs(this.prevPosition.x - this.targetDestination.x) / 2;
107 let halfTotalDistanceV = Math.abs(this.prevPosition.y - this.targetDestination.y) / 2;
108
109 let currentDistanceH = Math.abs(this.transform.position.x - this.targetDestination.x);
110 let currentDistanceV = Math.abs(this.transform.position.y - this.targetDestination.y);
111
112 let hDir = -Math.sign(this.transform.position.x - this.targetDestination.x);
113 let vDir = -Math.sign(this.transform.position.y - this.targetDestination.y);
114
115 if (this.hBeforeV)
71 {
116 {
72 this.currentBehaviour = this.behaviourList.random();
117 if (currentDistanceH > halfTotalDistanceH || currentDistanceV < 1)
73 this.currentBehaviour.startBehaviour();
118 {
119 this.renderer.mirrorX = hDir > 0;
120 this.transform.velocity.x = this.speed * hDir;
121 this.transform.velocity.y = 0;
122 this.renderer.setAnimation("walkLeft");
123 }
124 else
125 {
126 this.transform.velocity.x = 0;
127 this.transform.velocity.y = this.speed * vDir;
128 this.renderer.setAnimation(vDir < 0 ? "walkUp" : "walkDown");
129 }
74 }
130 }
131 else
132 {
133 if (currentDistanceV > halfTotalDistanceV || currentDistanceH < 1)
134 {
135 this.transform.velocity.x = 0;
136 this.transform.velocity.y = this.speed * vDir;
137 this.renderer.setAnimation(vDir < 0 ? "walkUp" : "walkDown");
138 }
139 else
140 {
141 this.renderer.mirrorX = hDir > 0;
142 this.transform.velocity.x = this.speed * hDir;
143 this.transform.velocity.y = 0;
144 this.renderer.setAnimation("walkLeft");
145 }
146 }
147 }
148 else if (this.renderer.currentAnimationName && this.renderer.currentAnimationName.indexOf("walk") >= 0)
149 {
150 this.renderer.setAnimation(this.renderer.currentAnimationName.replace("walk", "stand"));
151
152 this.transform.velocity.x = 0;
153 this.transform.velocity.y = 0;
75 }
154 }
76 }
155 }
77
156
78 draw(context)
157 draw (context)
79 {
158 {
80 super.draw(context);
159 super.draw(context);
81 }
160 }
src/objects/grasstiler.js +5 −4
@@ -13,16 +13,17 @@ class GrassTiler extends GameObject
13 13 {
14 14 let width = this.scene.real_size.x + this.ground_dark.width
15 15 let height = this.scene.real_size.y + this.ground_dark.height
16 let size = 32
16 17 let count = 0;
17 for(let x = 0; x < width; x += this.ground_dark.width)
18 for(let x = 0; x < width; x += size)
18 19 {
19 for(let y = 0; y < height; y += this.ground_light.height)
20 for(let y = 0; y < height; y += size)
20 21 {
21 22 count++;
22 ((count) %2 == 1? this.ground_dark : this.ground_light).draw(this.scene, x - 32, y, this.ground_dark.width, this.ground_light.height, 0, 0, 0, 1, 0)
23 ((count) %2 == 1? this.ground_dark : this.ground_light).draw(this.scene, x - (size/2), y, size, size, 0, 0, 0, 1, 0)
23 24 }
24 25
25 count = x/64 % 2;
26 count = x/size % 2;
26 27 }
27 28 }
28 29 }
@@ -13,16 +13,17 @@ class GrassTiler extends GameObject
13 {
13 {
14 let width = this.scene.real_size.x + this.ground_dark.width
14 let width = this.scene.real_size.x + this.ground_dark.width
15 let height = this.scene.real_size.y + this.ground_dark.height
15 let height = this.scene.real_size.y + this.ground_dark.height
16 let size = 32
16 let count = 0;
17 let count = 0;
17 for(let x = 0; x < width; x += this.ground_dark.width)
18 for(let x = 0; x < width; x += size)
18 {
19 {
19 for(let y = 0; y < height; y += this.ground_light.height)
20 for(let y = 0; y < height; y += size)
20 {
21 {
21 count++;
22 count++;
22 ((count) %2 == 1? this.ground_dark : this.ground_light).draw(this.scene, x - 32, y, this.ground_dark.width, this.ground_light.height, 0, 0, 0, 1, 0)
23 ((count) %2 == 1? this.ground_dark : this.ground_light).draw(this.scene, x - (size/2), y, size, size, 0, 0, 0, 1, 0)
23 }
24 }
24
25
25 count = x/64 % 2;
26 count = x/size % 2;
26 }
27 }
27 }
28 }
28}
29}