- Update class Player, Map, Ball
This commit is contained in:
Kum1ta
2024-08-21 01:02:23 +02:00
parent 23eb01a03e
commit 3498f32635
6 changed files with 165 additions and 94 deletions

View File

@ -3,29 +3,43 @@
/* ::: :::::::: */
/* Map.js :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hubourge <hubourge@student.42.fr> +#+ +:+ +#+ */
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/20 14:52:55 by hubourge #+# #+# */
/* Updated: 2024/08/20 18:29:17 by hubourge ### ########.fr */
/* Updated: 2024/08/21 00:59:01 by edbernar ### ########.fr */
/* */
/* ************************************************************************** */
import * as THREE from 'three';
/*
Todo (Eddy) :
- Ajouter la transparence sur les murs sur la distance de la balle (OK)
- Ajouter des textures selon le type : number pour couleur, string pour img (OK)
*/
class Map
{
scene = null;
arrObject = [];
centerPos = {x:-1,y:-1,z:-1}
centerPos = {
x: -1,
y: -1,
z:-1
};
playerLimits = {
up : 3,
down: 0.3,
left: -3,
right: 3,
};
mapLength = 0;
constructor(scene, length)
{
this.scene = scene;
scene.add(this.#createPlanes(7.5, length, -(Math.PI / 2), "planeBottom", true));
scene.add(this.#createPlanes(7.5, length, (Math.PI / 2), "planeTop", false));
scene.add(this.#createPlanes(7.5, length, -(Math.PI / 2), "planeBottom", true, 0xaaffff));
scene.add(this.#createPlanes(7.5, length, (Math.PI / 2), "planeTop", false, '/textures/testTmp.jpg'));
scene.add(this.#createWall(-3.5, 0.4, -length/2, "wallLeft"));
scene.add(this.#createWall(3.5, 0.4, -length/2, "wallRight"));
this.centerPos.x = 0;
@ -34,22 +48,36 @@ class Map
this.mapLength = length;
};
#createPlanes(x, y, rot, name, isBottom) // passer un materiel
#createPlanes(x, y, rot, name, isBottom, visual) // passer un materiel
{
for (let i = 0; i < this.arrObject.length; i++)
{
if (this.arrObject[i].name == name)
throw Error("Name already exist.");
}
const geometry = new THREE.PlaneGeometry(x, y);
const material = new THREE.MeshPhysicalMaterial()
const mesh = new THREE.Mesh(geometry, material);
const geometry = new THREE.PlaneGeometry(x, y);
let material = null;
let mesh = null;
if (typeof(visual) == 'string')
{
const textureLoader = new THREE.TextureLoader();
const texture = textureLoader.load(visual);
material = new THREE.MeshPhysicalMaterial({ map: texture });
}
else if (typeof(visual) == 'number')
material = new THREE.MeshPhysicalMaterial({ color: visual });
else
{
console.log("kjdsjksd");
material = new THREE.MeshPhysicalMaterial();
}
mesh = new THREE.Mesh(geometry, material);
mesh.rotateX(rot);
if (isBottom)
mesh.position.set(0, 0.15, -6);
else
mesh.position.set(0, 3.05, -6);
mesh.position.set(0, 3.15, -6);
this.arrObject.push({mesh: mesh, name: name});
mesh.receiveShadow = true;
return (mesh);
@ -67,6 +95,8 @@ class Map
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(x, y, z);
mesh.material.transparent = true;
mesh.material.opacity = 0.5;
this.arrObject.push({mesh: mesh, name: name});
return (mesh);
};
@ -87,6 +117,24 @@ class Map
this.arrObject[i].mesh.position.z = ball.position.z;
this.arrObject[i].mesh.position.y = ball.position.y;
}
if (this.arrObject[i].name == "wallLeft")
{
let diff = ball.position.x - this.arrObject[i].mesh.position.x - 0.1;
if (diff > 2)
this.arrObject[i].mesh.material.opacity = 0;
else
this.arrObject[i].mesh.material.opacity = 1 - (diff / 2);
}
if (this.arrObject[i].name == "wallRight")
{
let diff = this.arrObject[i].mesh.position.x - ball.position.x - 0.1;
if (diff > 2)
this.arrObject[i].mesh.material.opacity = 0;
else
this.arrObject[i].mesh.material.opacity = 1 - (diff / 2);
}
}
}
};