BlueGit

timfa frameworkv3

fix input system

timfa Tim · pushed by timfa

Authored 2022-10-15 · 4 years ago

Hash
54bd89074533bc1236f75760828440d1729e3e16
Parent
b4a9b1eb
3 files changed +13 −5
framework/engine/game.js +3 −0
@@ -12,6 +12,7 @@ class Game
12 12
13 13 startGame() {
14 14 this.update();
15 GameInput.g_init();
15 16 }
16 17
17 18 loadScene(scene)
@@ -29,6 +30,8 @@ class Game
29 30 if(isNaN(stepTimeCurr))
30 31 stepTimeCurr = 0;
31 32
33 GameInput.g_updateKeys();
34
32 35 for(let i = 0; i < this.activeScenes.length; i++)
33 36 {
34 37 this.activeScenes[i].update(stepTimeCurr);
@@ -12,6 +12,7 @@ class Game
12
12
13 startGame() {
13 startGame() {
14 this.update();
14 this.update();
15 GameInput.g_init();
15 }
16 }
16
17
17 loadScene(scene)
18 loadScene(scene)
@@ -29,6 +30,8 @@ class Game
29 if(isNaN(stepTimeCurr))
30 if(isNaN(stepTimeCurr))
30 stepTimeCurr = 0;
31 stepTimeCurr = 0;
31
32
33 GameInput.g_updateKeys();
34
32 for(let i = 0; i < this.activeScenes.length; i++)
35 for(let i = 0; i < this.activeScenes.length; i++)
33 {
36 {
34 this.activeScenes[i].update(stepTimeCurr);
37 this.activeScenes[i].update(stepTimeCurr);
framework/engine/objectComponents/collider.js +5 −5
@@ -28,12 +28,12 @@ class Collider
28 28 var hit = false;
29 29 var clickPos = GameInput.mousePosition.subtract(this.gameObject.scene.position).subtract(this.gameObject.scene.cameraPosition);
30 30
31 var hw = this.transform.size.x / 2;
32 var hh = this.transform.size.y / 2;
31 var hw = this.gameObject.transform.size.x / 2;
32 var hh = this.gameObject.transform.size.y / 2;
33 33
34 if(clickPos.x > this.transform.position.x - hw && clickPos.x < this.transform.position.x + hw)
34 if(clickPos.x > this.gameObject.transform.position.x - hw && clickPos.x < this.gameObject.transform.position.x + hw)
35 35 {
36 if(clickPos.y > this.transform.position.y - hh && clickPos.y < this.transform.position.y + hh)
36 if(clickPos.y > this.gameObject.transform.position.y - hh && clickPos.y < this.gameObject.transform.position.y + hh)
37 37 {
38 38 hit = true;
39 39 }
@@ -101,7 +101,7 @@ class Collider
101 101
102 102 for(var pi = 0; pi < p.length; pi++)
103 103 {
104 if(p[pi][0] > this.transform.position.x - hw && p[pi][0] < this.transform.position.x + hw)
104 if(p[pi][0] > this.gameObject.transform.position.x - hw && p[pi][0] < this.gameObject.transform.position.x + hw)
105 105 {
106 106 if(p[pi][1] > this.gameObject.transform.position.y - hh && p[pi][1] < this.gameObject.transform.position.y + hh)
107 107 {
@@ -28,12 +28,12 @@ class Collider
28 var hit = false;
28 var hit = false;
29 var clickPos = GameInput.mousePosition.subtract(this.gameObject.scene.position).subtract(this.gameObject.scene.cameraPosition);
29 var clickPos = GameInput.mousePosition.subtract(this.gameObject.scene.position).subtract(this.gameObject.scene.cameraPosition);
30
30
31 var hw = this.transform.size.x / 2;
31 var hw = this.gameObject.transform.size.x / 2;
32 var hh = this.transform.size.y / 2;
32 var hh = this.gameObject.transform.size.y / 2;
33
33
34 if(clickPos.x > this.transform.position.x - hw && clickPos.x < this.transform.position.x + hw)
34 if(clickPos.x > this.gameObject.transform.position.x - hw && clickPos.x < this.gameObject.transform.position.x + hw)
35 {
35 {
36 if(clickPos.y > this.transform.position.y - hh && clickPos.y < this.transform.position.y + hh)
36 if(clickPos.y > this.gameObject.transform.position.y - hh && clickPos.y < this.gameObject.transform.position.y + hh)
37 {
37 {
38 hit = true;
38 hit = true;
39 }
39 }
@@ -101,7 +101,7 @@ class Collider
101
101
102 for(var pi = 0; pi < p.length; pi++)
102 for(var pi = 0; pi < p.length; pi++)
103 {
103 {
104 if(p[pi][0] > this.transform.position.x - hw && p[pi][0] < this.transform.position.x + hw)
104 if(p[pi][0] > this.gameObject.transform.position.x - hw && p[pi][0] < this.gameObject.transform.position.x + hw)
105 {
105 {
106 if(p[pi][1] > this.gameObject.transform.position.y - hh && p[pi][1] < this.gameObject.transform.position.y + hh)
106 if(p[pi][1] > this.gameObject.transform.position.y - hh && p[pi][1] < this.gameObject.transform.position.y + hh)
107 {
107 {
framework/util/input.js +5 −0
@@ -2,6 +2,7 @@
2 2
3 3 var GameInput =
4 4 {
5 keys: {},
5 6 g_keyStatus: [],
6 7 g_pKeyStatus: [],
7 8
@@ -69,6 +70,9 @@ var GameInput =
69 70
70 71 document.onmousemove = function(e)
71 72 {
73 if(!GameInput.trueMousePosition)
74 GameInput.trueMousePosition = new vector();
75
72 76 GameInput.trueMousePosition.x = e.x;
73 77 GameInput.trueMousePosition.y = e.y;
74 78 }
@@ -82,6 +86,7 @@ var GameInput =
82 86
83 87 g_setKeyStatus: function(e)
84 88 {
89 GameInput.keys[e.key] = e.which;
85 90 GameInput.g_keyStatus[e.which] = (e.type == "keydown");
86 91
87 92 if(!GameInput.g_pressBlacklist[e.which])
@@ -2,6 +2,7 @@
2
2
3var GameInput =
3var GameInput =
4{
4{
5 keys: {},
5 g_keyStatus: [],
6 g_keyStatus: [],
6 g_pKeyStatus: [],
7 g_pKeyStatus: [],
7
8
@@ -69,6 +70,9 @@ var GameInput =
69
70
70 document.onmousemove = function(e)
71 document.onmousemove = function(e)
71 {
72 {
73 if(!GameInput.trueMousePosition)
74 GameInput.trueMousePosition = new vector();
75
72 GameInput.trueMousePosition.x = e.x;
76 GameInput.trueMousePosition.x = e.x;
73 GameInput.trueMousePosition.y = e.y;
77 GameInput.trueMousePosition.y = e.y;
74 }
78 }
@@ -82,6 +86,7 @@ var GameInput =
82
86
83 g_setKeyStatus: function(e)
87 g_setKeyStatus: function(e)
84 {
88 {
89 GameInput.keys[e.key] = e.which;
85 GameInput.g_keyStatus[e.which] = (e.type == "keydown");
90 GameInput.g_keyStatus[e.which] = (e.type == "keydown");
86
91
87 if(!GameInput.g_pressBlacklist[e.which])
92 if(!GameInput.g_pressBlacklist[e.which])