From e35ddd247747a6c532b84ffbaac44482e47d6c03 Mon Sep 17 00:00:00 2001 From: Kum1ta Date: Wed, 28 Aug 2024 11:46:10 +0200 Subject: [PATCH] Game - add dispose() on all class --- site/real_game/class/Ball.js | 23 +++++- site/real_game/class/Map.js | 3 +- site/real_game/class/Opponent.js | 11 +-- site/real_game/class/Player.js | 136 +++++++++++++++++-------------- site/real_game/main.js | 16 ++-- 5 files changed, 116 insertions(+), 73 deletions(-) diff --git a/site/real_game/class/Ball.js b/site/real_game/class/Ball.js index b5c1222..39814f8 100644 --- a/site/real_game/class/Ball.js +++ b/site/real_game/class/Ball.js @@ -6,7 +6,7 @@ /* By: edbernar { + if (!this.object && !warn) + { + console.warn("EventListener in initMoveBallTmp() is still here"); + warn = true; + return ; + } if (e.key == '4') this.object.position.x -= speedBallTmp; if (e.key == '6') @@ -102,7 +109,19 @@ class Ball dispose() { - + if (this.interval) + clearInterval(this.interval); + this.interval = null; + if (this.object) + { + if (this.object.geometry) + this.object.geometry.dispose(); + if (this.object.material) + this.object.material.dispose(); + if (this.object.texture) + this.object.texture.dispose(); + } + this.object = null; } } diff --git a/site/real_game/class/Map.js b/site/real_game/class/Map.js index 37051ae..1c7fcb2 100644 --- a/site/real_game/class/Map.js +++ b/site/real_game/class/Map.js @@ -6,7 +6,7 @@ /* By: edbernar { - opponentExist = false; - }) - this.cleanup.register(this, null); + } + + dispose() + { + opponentExist = false; } update() diff --git a/site/real_game/class/Player.js b/site/real_game/class/Player.js index c987262..3de80d1 100644 --- a/site/real_game/class/Player.js +++ b/site/real_game/class/Player.js @@ -6,7 +6,7 @@ /* By: edbernar { - playerExist = false; - }) - this.cleanup.register(this, null); + document.addEventListener('keydown', addKeyInArr); + document.addEventListener('keyup', remKeyInArr); + document.addEventListener('keypress', simplePressKey); + } - document.addEventListener('keydown', (e) => { - let i; - - i = 0; - while (i < this.pressedButton.length && e.key != this.pressedButton[i]) - i++; - if (i == this.pressedButton.length) - this.pressedButton.push(e.key); - }); - - document.addEventListener('keyup', (e) => { - let i; - - i = 0; - while (i < this.pressedButton.length && e.key != this.pressedButton[i]) - i++; - if (i != this.pressedButton.length) - this.pressedButton.splice(i, 1); - }); - - document.addEventListener('keypress', (e) => { - if (e.key == 'm' && !this.isOnPointAnim) - { - this.cameraFixed = !this.cameraFixed; - if (!this.cameraFixed) - { - this.setCameraPosition( - this.object.position.x, - this.object.position.y - (this.object.position.y >= this.limits.up ? 0.7 : -0.7), - this.object.position.z + 1.5 - ); - this.camera.rotation.set(0, 0, 0); - } - else - this.setCameraPosition(0, 1.5, this.object.position.z + 3); - } - }); + dispose() + { + playerExist = false; + isOnPointAnim = false; + document.removeEventListener('keydown', addKeyInArr); + document.removeEventListener('keyup', remKeyInArr); + document.removeEventListener('keypress', simplePressKey); + pressedButton = []; + if (this.interval) + clearInterval(interval); } pointAnimation(map) @@ -133,7 +108,7 @@ class Player }, 300) setTimeout(() => { tmpCamera.position.set(this.limits.left, this.limits.up / 2 + 0.5, map.centerPos.z); - this.isOnPointAnim = true; + isOnPointAnim = true; this.camera = tmpCamera; interval = setInterval(() => { tmpCamera.lookAt(this.object.position); @@ -152,7 +127,7 @@ class Player setTimeout(() => { this.camera = tmp; this.object.material.color.copy(startColor); - this.isOnPointAnim = false; + isOnPointAnim = false; if (!this.cameraFixed) { this.setCameraPosition( @@ -184,7 +159,7 @@ class Player }, 300) setTimeout(() => { tmpCamera.position.set(this.limits.left, this.limits.up / 2 + 0.5, map.centerPos.z); - this.isOnPointAnim = true; + isOnPointAnim = true; this.camera = tmpCamera; interval = setInterval(() => { tmpCamera.lookAt(oppponentObject.position); @@ -204,7 +179,7 @@ class Player setTimeout(() => { this.camera = tmp; oppponentObject.material.color.copy(startColor); - this.isOnPointAnim = false; + isOnPointAnim = false; if (!this.cameraFixed) { this.setCameraPosition( @@ -229,15 +204,15 @@ class Player let i; i = 0; - while (i < this.pressedButton.length) + while (i < pressedButton.length) { - if (this.pressedButton[i] == 'w' && this.object.position.y < this.limits.up) + if (pressedButton[i] == 'w' && this.object.position.y < this.limits.up) { if (this.interval) clearInterval(this.interval); this.interval = setInterval(() => { this.object.position.y += this.speed / 40; - if (!this.cameraFixed && !this.isOnPointAnim) + if (!this.cameraFixed && !isOnPointAnim) this.camera.position.y += (this.speed / 80); if (this.object.position.y >= this.limits.up) { @@ -246,13 +221,13 @@ class Player } }, 5); } - if (this.pressedButton[i] == 's' && this.object.position.y > this.limits.down) + if (pressedButton[i] == 's' && this.object.position.y > this.limits.down) { if (this.interval) clearInterval(this.interval); this.interval = setInterval(() => { this.object.position.y -= this.speed / 40; - if (!this.cameraFixed && !this.isOnPointAnim) + if (!this.cameraFixed && !isOnPointAnim) this.camera.position.y -= (this.speed / 80); if (this.object.position.y <= this.limits.down) { @@ -262,16 +237,16 @@ class Player } }, 5); } - if (this.pressedButton[i] == 'd' && this.object.position.x < this.limits.right) + if (pressedButton[i] == 'd' && this.object.position.x < this.limits.right) { this.object.position.x += this.speed * this.deltaTime; - if (!this.cameraFixed && !this.isOnPointAnim) + if (!this.cameraFixed && !isOnPointAnim) this.camera.position.x += this.speed * this.deltaTime; } - if (this.pressedButton[i] == 'a' && this.object.position.x > this.limits.left) + if (pressedButton[i] == 'a' && this.object.position.x > this.limits.left) { this.object.position.x -= this.speed * this.deltaTime; - if (!this.cameraFixed && !this.isOnPointAnim) + if (!this.cameraFixed && !isOnPointAnim) this.camera.position.x -= this.speed * this.deltaTime; } i++; @@ -284,4 +259,45 @@ class Player } }; +function addKeyInArr(e) +{ + let i; + + i = 0; + while (i < pressedButton.length && e.key != pressedButton[i]) + i++; + if (i == pressedButton.length) + pressedButton.push(e.key); +} + +function remKeyInArr(e) +{ + let i; + + i = 0; + while (i < pressedButton.length && e.key != pressedButton[i]) + i++; + if (i != pressedButton.length) + pressedButton.splice(i, 1); +} + +function simplePressKey(e) +{ + if (e.key == 'm' && !isOnPointAnim) + { + this.cameraFixed = !this.cameraFixed; + if (!this.cameraFixed) + { + this.setCameraPosition( + this.object.position.x, + this.object.position.y - (this.object.position.y >= this.limits.up ? 0.7 : -0.7), + this.object.position.z + 1.5 + ); + this.camera.rotation.set(0, 0, 0); + } + else + this.setCameraPosition(0, 1.5, this.object.position.z + 3); + } +} + export { Player, playerExist }; \ No newline at end of file diff --git a/site/real_game/main.js b/site/real_game/main.js index 113807d..cc7f479 100644 --- a/site/real_game/main.js +++ b/site/real_game/main.js @@ -6,7 +6,7 @@ /* By: edbernar { @@ -174,7 +184,3 @@ function loop() } Game.create(); - -setTimeout(() => { - Game.dispose(); -}, 5000); \ No newline at end of file