BlueGit

blue-star speeltuin

arrow moving

timfa Tim · pushed by timfa

Authored 2022-10-15 · 4 years ago

Hash
0808beee8e24332b4f10818dfd0031bf37eb24ab
Parent
d0f209f3
2 files changed +48 −10
build/framework.js +18 −10
@@ -17,6 +17,7 @@ class Game
17 17
18 18 startGame() {
19 19 this.update();
20 GameInput.g_init();
20 21 }
21 22
22 23 loadScene(scene)
@@ -34,6 +35,8 @@ class Game
34 35 if(isNaN(stepTimeCurr))
35 36 stepTimeCurr = 0;
36 37
38 GameInput.g_updateKeys();
39
37 40 for(let i = 0; i < this.activeScenes.length; i++)
38 41 {
39 42 this.activeScenes[i].update(stepTimeCurr);
@@ -200,12 +203,12 @@ class Collider
200 203 var hit = false;
201 204 var clickPos = GameInput.mousePosition.subtract(this.gameObject.scene.position).subtract(this.gameObject.scene.cameraPosition);
202 205
203 var hw = this.transform.size.x / 2;
204 var hh = this.transform.size.y / 2;
206 var hw = this.gameObject.transform.size.x / 2;
207 var hh = this.gameObject.transform.size.y / 2;
205 208
206 if(clickPos.x > this.transform.position.x - hw && clickPos.x < this.transform.position.x + hw)
209 if(clickPos.x > this.gameObject.transform.position.x - hw && clickPos.x < this.gameObject.transform.position.x + hw)
207 210 {
208 if(clickPos.y > this.transform.position.y - hh && clickPos.y < this.transform.position.y + hh)
211 if(clickPos.y > this.gameObject.transform.position.y - hh && clickPos.y < this.gameObject.transform.position.y + hh)
209 212 {
210 213 hit = true;
211 214 }
@@ -273,7 +276,7 @@ class Collider
273 276
274 277 for(var pi = 0; pi < p.length; pi++)
275 278 {
276 if(p[pi][0] > this.transform.position.x - hw && p[pi][0] < this.transform.position.x + hw)
279 if(p[pi][0] > this.gameObject.transform.position.x - hw && p[pi][0] < this.gameObject.transform.position.x + hw)
277 280 {
278 281 if(p[pi][1] > this.gameObject.transform.position.y - hh && p[pi][1] < this.gameObject.transform.position.y + hh)
279 282 {
@@ -551,17 +554,17 @@ class Scene
551 554
552 555 if(this.fullscreen)
553 556 {
554 this.size.width = window.innerWidth;
555 this.size.height = window.innerHeight;
557 this.size.x = window.innerWidth;
558 this.size.y = window.innerHeight;
556 559 }
557 560
558 this.canvas.width = this.size.width;
559 this.canvas.height = this.size.height;
561 this.canvas.width = this.size.x;
562 this.canvas.height = this.size.y;
560 563
561 564 this.canvas.style = 'position: absolute; left: ' + this.position.x + 'px; top: ' + this.position.y + 'px;'; //todo: only call when changed?
562 565
563 566 this.context.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
564 this.context.fillRect(0, 0, this.size.width, this.size.height);
567 this.context.fillRect(0, 0, this.size.x, this.size.y);
565 568
566 569 for(var i = 0; i < this.activeObjects.length; i++)
567 570 {
@@ -1139,6 +1142,7 @@ var ease = {
1139 1142
1140 1143 var GameInput =
1141 1144 {
1145 keys: {},
1142 1146 g_keyStatus: [],
1143 1147 g_pKeyStatus: [],
1144 1148
@@ -1206,6 +1210,9 @@ var GameInput =
1206 1210
1207 1211 document.onmousemove = function(e)
1208 1212 {
1213 if(!GameInput.trueMousePosition)
1214 GameInput.trueMousePosition = new vector();
1215
1209 1216 GameInput.trueMousePosition.x = e.x;
1210 1217 GameInput.trueMousePosition.y = e.y;
1211 1218 }
@@ -1219,6 +1226,7 @@ var GameInput =
1219 1226
1220 1227 g_setKeyStatus: function(e)
1221 1228 {
1229 GameInput.keys[e.key] = e.which;
1222 1230 GameInput.g_keyStatus[e.which] = (e.type == "keydown");
1223 1231
1224 1232 if(!GameInput.g_pressBlacklist[e.which])
@@ -17,6 +17,7 @@ class Game
17
17
18 startGame() {
18 startGame() {
19 this.update();
19 this.update();
20 GameInput.g_init();
20 }
21 }
21
22
22 loadScene(scene)
23 loadScene(scene)
@@ -34,6 +35,8 @@ class Game
34 if(isNaN(stepTimeCurr))
35 if(isNaN(stepTimeCurr))
35 stepTimeCurr = 0;
36 stepTimeCurr = 0;
36
37
38 GameInput.g_updateKeys();
39
37 for(let i = 0; i < this.activeScenes.length; i++)
40 for(let i = 0; i < this.activeScenes.length; i++)
38 {
41 {
39 this.activeScenes[i].update(stepTimeCurr);
42 this.activeScenes[i].update(stepTimeCurr);
@@ -200,12 +203,12 @@ class Collider
200 var hit = false;
203 var hit = false;
201 var clickPos = GameInput.mousePosition.subtract(this.gameObject.scene.position).subtract(this.gameObject.scene.cameraPosition);
204 var clickPos = GameInput.mousePosition.subtract(this.gameObject.scene.position).subtract(this.gameObject.scene.cameraPosition);
202
205
203 var hw = this.transform.size.x / 2;
206 var hw = this.gameObject.transform.size.x / 2;
204 var hh = this.transform.size.y / 2;
207 var hh = this.gameObject.transform.size.y / 2;
205
208
206 if(clickPos.x > this.transform.position.x - hw && clickPos.x < this.transform.position.x + hw)
209 if(clickPos.x > this.gameObject.transform.position.x - hw && clickPos.x < this.gameObject.transform.position.x + hw)
207 {
210 {
208 if(clickPos.y > this.transform.position.y - hh && clickPos.y < this.transform.position.y + hh)
211 if(clickPos.y > this.gameObject.transform.position.y - hh && clickPos.y < this.gameObject.transform.position.y + hh)
209 {
212 {
210 hit = true;
213 hit = true;
211 }
214 }
@@ -273,7 +276,7 @@ class Collider
273
276
274 for(var pi = 0; pi < p.length; pi++)
277 for(var pi = 0; pi < p.length; pi++)
275 {
278 {
276 if(p[pi][0] > this.transform.position.x - hw && p[pi][0] < this.transform.position.x + hw)
279 if(p[pi][0] > this.gameObject.transform.position.x - hw && p[pi][0] < this.gameObject.transform.position.x + hw)
277 {
280 {
278 if(p[pi][1] > this.gameObject.transform.position.y - hh && p[pi][1] < this.gameObject.transform.position.y + hh)
281 if(p[pi][1] > this.gameObject.transform.position.y - hh && p[pi][1] < this.gameObject.transform.position.y + hh)
279 {
282 {
@@ -551,17 +554,17 @@ class Scene
551
554
552 if(this.fullscreen)
555 if(this.fullscreen)
553 {
556 {
554 this.size.width = window.innerWidth;
557 this.size.x = window.innerWidth;
555 this.size.height = window.innerHeight;
558 this.size.y = window.innerHeight;
556 }
559 }
557
560
558 this.canvas.width = this.size.width;
561 this.canvas.width = this.size.x;
559 this.canvas.height = this.size.height;
562 this.canvas.height = this.size.y;
560
563
561 this.canvas.style = 'position: absolute; left: ' + this.position.x + 'px; top: ' + this.position.y + 'px;'; //todo: only call when changed?
564 this.canvas.style = 'position: absolute; left: ' + this.position.x + 'px; top: ' + this.position.y + 'px;'; //todo: only call when changed?
562
565
563 this.context.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
566 this.context.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
564 this.context.fillRect(0, 0, this.size.width, this.size.height);
567 this.context.fillRect(0, 0, this.size.x, this.size.y);
565
568
566 for(var i = 0; i < this.activeObjects.length; i++)
569 for(var i = 0; i < this.activeObjects.length; i++)
567 {
570 {
@@ -1139,6 +1142,7 @@ var ease = {
1139
1142
1140var GameInput =
1143var GameInput =
1141{
1144{
1145 keys: {},
1142 g_keyStatus: [],
1146 g_keyStatus: [],
1143 g_pKeyStatus: [],
1147 g_pKeyStatus: [],
1144
1148
@@ -1206,6 +1210,9 @@ var GameInput =
1206
1210
1207 document.onmousemove = function(e)
1211 document.onmousemove = function(e)
1208 {
1212 {
1213 if(!GameInput.trueMousePosition)
1214 GameInput.trueMousePosition = new vector();
1215
1209 GameInput.trueMousePosition.x = e.x;
1216 GameInput.trueMousePosition.x = e.x;
1210 GameInput.trueMousePosition.y = e.y;
1217 GameInput.trueMousePosition.y = e.y;
1211 }
1218 }
@@ -1219,6 +1226,7 @@ var GameInput =
1219
1226
1220 g_setKeyStatus: function(e)
1227 g_setKeyStatus: function(e)
1221 {
1228 {
1229 GameInput.keys[e.key] = e.which;
1222 GameInput.g_keyStatus[e.which] = (e.type == "keydown");
1230 GameInput.g_keyStatus[e.which] = (e.type == "keydown");
1223
1231
1224 if(!GameInput.g_pressBlacklist[e.which])
1232 if(!GameInput.g_pressBlacklist[e.which])
src/objects/arrow.js +30 −0
@@ -1,5 +1,7 @@
1 1 class Arrow extends GameObject
2 2 {
3 speed = 250;
4
3 5 constructor(x, y, sprite, layer)
4 6 {
5 7 super(x, y, sprite, layer);
@@ -9,5 +11,33 @@ class Arrow extends GameObject
9 11 update(deltaTime)
10 12 {
11 13 super.update(deltaTime);
14
15 let oldPos = this.transform.position.getCopy();
16
17 if(GameInput.isHeld(GameInput.keys.ArrowRight))
18 {
19 this.transform.position.x += this.speed * deltaTime;
20 }
21
22 if(GameInput.isHeld(GameInput.keys.ArrowLeft))
23 {
24 this.transform.position.x -= this.speed * deltaTime;
25 }
26
27 if(GameInput.isHeld(GameInput.keys.ArrowUp))
28 {
29 this.transform.position.y -= this.speed * deltaTime;
30 }
31
32 if(GameInput.isHeld(GameInput.keys.ArrowDown))
33 {
34 this.transform.position.y += this.speed * deltaTime;
35 }
36
37 if(oldPos.distanceTo(this.transform.position) > 1)
38 {
39 this.transform.pointTowards(oldPos)
40 this.transform.rotation += 180;
41 }
12 42 }
13 43 }
@@ -1,5 +1,7 @@
1class Arrow extends GameObject
1class Arrow extends GameObject
2{
2{
3 speed = 250;
4
3 constructor(x, y, sprite, layer)
5 constructor(x, y, sprite, layer)
4 {
6 {
5 super(x, y, sprite, layer);
7 super(x, y, sprite, layer);
@@ -9,5 +11,33 @@ class Arrow extends GameObject
9 update(deltaTime)
11 update(deltaTime)
10 {
12 {
11 super.update(deltaTime);
13 super.update(deltaTime);
14
15 let oldPos = this.transform.position.getCopy();
16
17 if(GameInput.isHeld(GameInput.keys.ArrowRight))
18 {
19 this.transform.position.x += this.speed * deltaTime;
20 }
21
22 if(GameInput.isHeld(GameInput.keys.ArrowLeft))
23 {
24 this.transform.position.x -= this.speed * deltaTime;
25 }
26
27 if(GameInput.isHeld(GameInput.keys.ArrowUp))
28 {
29 this.transform.position.y -= this.speed * deltaTime;
30 }
31
32 if(GameInput.isHeld(GameInput.keys.ArrowDown))
33 {
34 this.transform.position.y += this.speed * deltaTime;
35 }
36
37 if(oldPos.distanceTo(this.transform.position) > 1)
38 {
39 this.transform.pointTowards(oldPos)
40 this.transform.rotation += 180;
41 }
12 }
42 }
13}
43}