Game
- add dispose() on all class
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/20 17:02:47 by edbernar #+# #+# */
|
||||
/* Updated: 2024/08/27 14:31:15 by edbernar ### ########.fr */
|
||||
/* Updated: 2024/08/28 11:17:08 by edbernar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -84,8 +84,15 @@ class Ball
|
||||
{
|
||||
console.warn("Don't forget to remove function initMoveBallTmp");
|
||||
const speedBallTmp = 0.1;
|
||||
let warn = false;
|
||||
|
||||
document.addEventListener('keypress', (e) => {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/20 14:52:55 by hubourge #+# #+# */
|
||||
/* Updated: 2024/08/27 19:33:00 by edbernar ### ########.fr */
|
||||
/* Updated: 2024/08/28 11:04:41 by edbernar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -103,6 +103,7 @@ class Map
|
||||
}
|
||||
scene.remove(elem.mesh);
|
||||
});
|
||||
this.arrObject = null;
|
||||
if (interval2)
|
||||
clearInterval(interval2);
|
||||
scene = null;
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/21 10:34:49 by edbernar #+# #+# */
|
||||
/* Updated: 2024/08/27 14:27:49 by edbernar ### ########.fr */
|
||||
/* Updated: 2024/08/28 11:39:59 by edbernar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -34,10 +34,11 @@ class Opponent
|
||||
this.object = object;
|
||||
this.limits = map.limits;
|
||||
this.object.position.set(0, 0.3, -map.mapLength / 2 + 0.2);
|
||||
this.cleanup = new FinalizationRegistry((heldValue) => {
|
||||
}
|
||||
|
||||
dispose()
|
||||
{
|
||||
opponentExist = false;
|
||||
})
|
||||
this.cleanup.register(this, null);
|
||||
}
|
||||
|
||||
update()
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/18 00:30:31 by edbernar #+# #+# */
|
||||
/* Updated: 2024/08/27 14:27:39 by edbernar ### ########.fr */
|
||||
/* Updated: 2024/08/28 11:38:45 by edbernar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -45,16 +45,16 @@ import * as THREE from 'three';
|
||||
*/
|
||||
|
||||
let playerExist = false;
|
||||
let isOnPointAnim = false;
|
||||
let pressedButton = [];
|
||||
|
||||
class Player
|
||||
{
|
||||
pressedButton = [];
|
||||
object = null;
|
||||
camera = null;
|
||||
speed = 4;
|
||||
cameraFixed = false;
|
||||
interval = null;
|
||||
isOnPointAnim = false;
|
||||
limits = {};
|
||||
previousTime = Date.now();
|
||||
deltaTime = 1;
|
||||
@ -64,6 +64,8 @@ class Player
|
||||
if (playerExist)
|
||||
throw Error("Player is already init.");
|
||||
playerExist = true;
|
||||
isOnPointAnim = false;
|
||||
pressedButton = [];
|
||||
this.object = object;
|
||||
this.limits = map.playerLimits;
|
||||
this.camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 10000);
|
||||
@ -73,48 +75,21 @@ class Player
|
||||
this.object.position.y + 0.7,
|
||||
this.object.position.z + 1.5
|
||||
);
|
||||
this.cleanup = new FinalizationRegistry((heldValue) => {
|
||||
document.addEventListener('keydown', addKeyInArr);
|
||||
document.addEventListener('keyup', remKeyInArr);
|
||||
document.addEventListener('keypress', simplePressKey);
|
||||
}
|
||||
|
||||
dispose()
|
||||
{
|
||||
playerExist = false;
|
||||
})
|
||||
this.cleanup.register(this, null);
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
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 };
|
@ -6,7 +6,7 @@
|
||||
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/18 00:53:53 by edbernar #+# #+# */
|
||||
/* Updated: 2024/08/27 19:18:17 by edbernar ### ########.fr */
|
||||
/* Updated: 2024/08/28 11:46:06 by edbernar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -120,6 +120,16 @@ class Game
|
||||
renderer = null;
|
||||
if (map)
|
||||
map.dispose();
|
||||
map = null;
|
||||
if (ball)
|
||||
ball.dispose();
|
||||
ball = null;
|
||||
if (player)
|
||||
player.dispose();
|
||||
player = null;
|
||||
if (opponent)
|
||||
opponent.dispose();
|
||||
opponent = null;
|
||||
if (scene)
|
||||
{
|
||||
scene.children.forEach(child => {
|
||||
@ -174,7 +184,3 @@ function loop()
|
||||
}
|
||||
|
||||
Game.create();
|
||||
|
||||
setTimeout(() => {
|
||||
Game.dispose();
|
||||
}, 5000);
|
Reference in New Issue
Block a user