Game
- add dispose() on all class
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/08/20 17:02:47 by edbernar #+# #+# */
|
/* 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");
|
console.warn("Don't forget to remove function initMoveBallTmp");
|
||||||
const speedBallTmp = 0.1;
|
const speedBallTmp = 0.1;
|
||||||
|
let warn = false;
|
||||||
|
|
||||||
document.addEventListener('keypress', (e) => {
|
document.addEventListener('keypress', (e) => {
|
||||||
|
if (!this.object && !warn)
|
||||||
|
{
|
||||||
|
console.warn("EventListener in initMoveBallTmp() is still here");
|
||||||
|
warn = true;
|
||||||
|
return ;
|
||||||
|
}
|
||||||
if (e.key == '4')
|
if (e.key == '4')
|
||||||
this.object.position.x -= speedBallTmp;
|
this.object.position.x -= speedBallTmp;
|
||||||
if (e.key == '6')
|
if (e.key == '6')
|
||||||
@ -102,7 +109,19 @@ class Ball
|
|||||||
|
|
||||||
dispose()
|
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. +#+ +:+ +#+ */
|
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/08/20 14:52:55 by hubourge #+# #+# */
|
/* 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);
|
scene.remove(elem.mesh);
|
||||||
});
|
});
|
||||||
|
this.arrObject = null;
|
||||||
if (interval2)
|
if (interval2)
|
||||||
clearInterval(interval2);
|
clearInterval(interval2);
|
||||||
scene = null;
|
scene = null;
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/08/21 10:34:49 by edbernar #+# #+# */
|
/* 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.object = object;
|
||||||
this.limits = map.limits;
|
this.limits = map.limits;
|
||||||
this.object.position.set(0, 0.3, -map.mapLength / 2 + 0.2);
|
this.object.position.set(0, 0.3, -map.mapLength / 2 + 0.2);
|
||||||
this.cleanup = new FinalizationRegistry((heldValue) => {
|
}
|
||||||
opponentExist = false;
|
|
||||||
})
|
dispose()
|
||||||
this.cleanup.register(this, null);
|
{
|
||||||
|
opponentExist = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
update()
|
update()
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/08/18 00:30:31 by edbernar #+# #+# */
|
/* 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -44,26 +44,28 @@ import * as THREE from 'three';
|
|||||||
- Ajouter une fonction pour l'animation de point marqué (OK)
|
- Ajouter une fonction pour l'animation de point marqué (OK)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
let playerExist = false;
|
let playerExist = false;
|
||||||
|
let isOnPointAnim = false;
|
||||||
|
let pressedButton = [];
|
||||||
|
|
||||||
class Player
|
class Player
|
||||||
{
|
{
|
||||||
pressedButton = [];
|
|
||||||
object = null;
|
object = null;
|
||||||
camera = null;
|
camera = null;
|
||||||
speed = 4;
|
speed = 4;
|
||||||
cameraFixed = false;
|
cameraFixed = false;
|
||||||
interval = null;
|
interval = null;
|
||||||
isOnPointAnim = false;
|
|
||||||
limits = {};
|
limits = {};
|
||||||
previousTime = Date.now();
|
previousTime = Date.now();
|
||||||
deltaTime = 1;
|
deltaTime = 1;
|
||||||
|
|
||||||
constructor (object, map)
|
constructor (object, map)
|
||||||
{
|
{
|
||||||
if (playerExist)
|
if (playerExist)
|
||||||
throw Error("Player is already init.");
|
throw Error("Player is already init.");
|
||||||
playerExist = true;
|
playerExist = true;
|
||||||
|
isOnPointAnim = false;
|
||||||
|
pressedButton = [];
|
||||||
this.object = object;
|
this.object = object;
|
||||||
this.limits = map.playerLimits;
|
this.limits = map.playerLimits;
|
||||||
this.camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 10000);
|
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.y + 0.7,
|
||||||
this.object.position.z + 1.5
|
this.object.position.z + 1.5
|
||||||
);
|
);
|
||||||
this.cleanup = new FinalizationRegistry((heldValue) => {
|
document.addEventListener('keydown', addKeyInArr);
|
||||||
playerExist = false;
|
document.addEventListener('keyup', remKeyInArr);
|
||||||
})
|
document.addEventListener('keypress', simplePressKey);
|
||||||
this.cleanup.register(this, null);
|
}
|
||||||
|
|
||||||
document.addEventListener('keydown', (e) => {
|
dispose()
|
||||||
let i;
|
{
|
||||||
|
playerExist = false;
|
||||||
i = 0;
|
isOnPointAnim = false;
|
||||||
while (i < this.pressedButton.length && e.key != this.pressedButton[i])
|
document.removeEventListener('keydown', addKeyInArr);
|
||||||
i++;
|
document.removeEventListener('keyup', remKeyInArr);
|
||||||
if (i == this.pressedButton.length)
|
document.removeEventListener('keypress', simplePressKey);
|
||||||
this.pressedButton.push(e.key);
|
pressedButton = [];
|
||||||
});
|
if (this.interval)
|
||||||
|
clearInterval(interval);
|
||||||
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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pointAnimation(map)
|
pointAnimation(map)
|
||||||
@ -133,7 +108,7 @@ class Player
|
|||||||
}, 300)
|
}, 300)
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
tmpCamera.position.set(this.limits.left, this.limits.up / 2 + 0.5, map.centerPos.z);
|
tmpCamera.position.set(this.limits.left, this.limits.up / 2 + 0.5, map.centerPos.z);
|
||||||
this.isOnPointAnim = true;
|
isOnPointAnim = true;
|
||||||
this.camera = tmpCamera;
|
this.camera = tmpCamera;
|
||||||
interval = setInterval(() => {
|
interval = setInterval(() => {
|
||||||
tmpCamera.lookAt(this.object.position);
|
tmpCamera.lookAt(this.object.position);
|
||||||
@ -152,7 +127,7 @@ class Player
|
|||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.camera = tmp;
|
this.camera = tmp;
|
||||||
this.object.material.color.copy(startColor);
|
this.object.material.color.copy(startColor);
|
||||||
this.isOnPointAnim = false;
|
isOnPointAnim = false;
|
||||||
if (!this.cameraFixed)
|
if (!this.cameraFixed)
|
||||||
{
|
{
|
||||||
this.setCameraPosition(
|
this.setCameraPosition(
|
||||||
@ -184,7 +159,7 @@ class Player
|
|||||||
}, 300)
|
}, 300)
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
tmpCamera.position.set(this.limits.left, this.limits.up / 2 + 0.5, map.centerPos.z);
|
tmpCamera.position.set(this.limits.left, this.limits.up / 2 + 0.5, map.centerPos.z);
|
||||||
this.isOnPointAnim = true;
|
isOnPointAnim = true;
|
||||||
this.camera = tmpCamera;
|
this.camera = tmpCamera;
|
||||||
interval = setInterval(() => {
|
interval = setInterval(() => {
|
||||||
tmpCamera.lookAt(oppponentObject.position);
|
tmpCamera.lookAt(oppponentObject.position);
|
||||||
@ -204,7 +179,7 @@ class Player
|
|||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.camera = tmp;
|
this.camera = tmp;
|
||||||
oppponentObject.material.color.copy(startColor);
|
oppponentObject.material.color.copy(startColor);
|
||||||
this.isOnPointAnim = false;
|
isOnPointAnim = false;
|
||||||
if (!this.cameraFixed)
|
if (!this.cameraFixed)
|
||||||
{
|
{
|
||||||
this.setCameraPosition(
|
this.setCameraPosition(
|
||||||
@ -229,15 +204,15 @@ class Player
|
|||||||
let i;
|
let i;
|
||||||
|
|
||||||
i = 0;
|
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)
|
if (this.interval)
|
||||||
clearInterval(this.interval);
|
clearInterval(this.interval);
|
||||||
this.interval = setInterval(() => {
|
this.interval = setInterval(() => {
|
||||||
this.object.position.y += this.speed / 40;
|
this.object.position.y += this.speed / 40;
|
||||||
if (!this.cameraFixed && !this.isOnPointAnim)
|
if (!this.cameraFixed && !isOnPointAnim)
|
||||||
this.camera.position.y += (this.speed / 80);
|
this.camera.position.y += (this.speed / 80);
|
||||||
if (this.object.position.y >= this.limits.up)
|
if (this.object.position.y >= this.limits.up)
|
||||||
{
|
{
|
||||||
@ -246,13 +221,13 @@ class Player
|
|||||||
}
|
}
|
||||||
}, 5);
|
}, 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)
|
if (this.interval)
|
||||||
clearInterval(this.interval);
|
clearInterval(this.interval);
|
||||||
this.interval = setInterval(() => {
|
this.interval = setInterval(() => {
|
||||||
this.object.position.y -= this.speed / 40;
|
this.object.position.y -= this.speed / 40;
|
||||||
if (!this.cameraFixed && !this.isOnPointAnim)
|
if (!this.cameraFixed && !isOnPointAnim)
|
||||||
this.camera.position.y -= (this.speed / 80);
|
this.camera.position.y -= (this.speed / 80);
|
||||||
if (this.object.position.y <= this.limits.down)
|
if (this.object.position.y <= this.limits.down)
|
||||||
{
|
{
|
||||||
@ -262,16 +237,16 @@ class Player
|
|||||||
}
|
}
|
||||||
}, 5);
|
}, 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;
|
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;
|
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;
|
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;
|
this.camera.position.x -= this.speed * this.deltaTime;
|
||||||
}
|
}
|
||||||
i++;
|
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 };
|
export { Player, playerExist };
|
@ -6,7 +6,7 @@
|
|||||||
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/08/18 00:53:53 by edbernar #+# #+# */
|
/* 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;
|
renderer = null;
|
||||||
if (map)
|
if (map)
|
||||||
map.dispose();
|
map.dispose();
|
||||||
|
map = null;
|
||||||
|
if (ball)
|
||||||
|
ball.dispose();
|
||||||
|
ball = null;
|
||||||
|
if (player)
|
||||||
|
player.dispose();
|
||||||
|
player = null;
|
||||||
|
if (opponent)
|
||||||
|
opponent.dispose();
|
||||||
|
opponent = null;
|
||||||
if (scene)
|
if (scene)
|
||||||
{
|
{
|
||||||
scene.children.forEach(child => {
|
scene.children.forEach(child => {
|
||||||
@ -174,7 +184,3 @@ function loop()
|
|||||||
}
|
}
|
||||||
|
|
||||||
Game.create();
|
Game.create();
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
Game.dispose();
|
|
||||||
}, 5000);
|
|
Reference in New Issue
Block a user