Game
- Updating ball moves
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
/* By: hubourge <hubourge@student.42.fr> +#+ +:+ +#+ */
|
/* By: hubourge <hubourge@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/08/20 14:52:55 by hubourge #+# #+# */
|
/* Created: 2024/08/20 14:52:55 by hubourge #+# #+# */
|
||||||
/* Updated: 2024/08/20 16:34:25 by hubourge ### ########.fr */
|
/* Updated: 2024/08/20 17:24:15 by hubourge ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -18,15 +18,21 @@ class Map
|
|||||||
{
|
{
|
||||||
scene = null;
|
scene = null;
|
||||||
arrObject = [];
|
arrObject = [];
|
||||||
|
centerPos = {x:-1,y:-1,z:-1}
|
||||||
|
|
||||||
constructor(scene, length)
|
constructor(scene, length)
|
||||||
{
|
{
|
||||||
this.scene = scene;
|
this.scene = scene;
|
||||||
scene.add(this.#createPlanes(7.5, length, -(Math.PI / 2), "plane1"));
|
scene.add(this.#createPlanes(7.5, length, -(Math.PI / 2), "planeBottom", true));
|
||||||
// scene.add(this.#createWalls(0, 0, 0, "wallLeft"));
|
scene.add(this.#createPlanes(7.5, length, (Math.PI / 2), "planeTop", false));
|
||||||
|
scene.add(this.#createWall(-3.5, 0.15, -length/2, "wallLeft"));
|
||||||
|
scene.add(this.#createWall(3.5, 0.15, -length/2, "wallRight"));
|
||||||
|
this.centerPos.x = 0;
|
||||||
|
this.centerPos.y = 0.15;
|
||||||
|
this.centerPos.z = -length/2;
|
||||||
};
|
};
|
||||||
|
|
||||||
#createPlanes(x, y, rot, name) // passer un materiel
|
#createPlanes(x, y, rot, name, isBottom) // passer un materiel
|
||||||
{
|
{
|
||||||
for (let i = 0; i < this.arrObject.length; i++)
|
for (let i = 0; i < this.arrObject.length; i++)
|
||||||
{
|
{
|
||||||
@ -38,21 +44,30 @@ class Map
|
|||||||
const mesh = new THREE.Mesh(geometry, material);
|
const mesh = new THREE.Mesh(geometry, material);
|
||||||
|
|
||||||
mesh.rotateX(rot);
|
mesh.rotateX(rot);
|
||||||
mesh.position.set(0, 0.15, -6);
|
if (isBottom)
|
||||||
|
mesh.position.set(0, 0.15, -6);
|
||||||
|
else
|
||||||
|
mesh.position.set(0, 3.05, -6);
|
||||||
this.arrObject.push({mesh: mesh, name: name});
|
this.arrObject.push({mesh: mesh, name: name});
|
||||||
|
mesh.receiveShadow = true;
|
||||||
return (mesh);
|
return (mesh);
|
||||||
};
|
};
|
||||||
|
|
||||||
// #createWalls(x, y, z, name)
|
#createWall(x, y, z, name)
|
||||||
// {
|
{
|
||||||
// const geometry = new THREE.BoxGeometry(20, 20, 20);
|
for (let i = 0; i < this.arrObject.length; i++)
|
||||||
// const material = new THREE.MeshPhysicalMaterial();
|
{
|
||||||
// const mesh = new THREE.Mesh(geometry, material);
|
if (this.arrObject[i].name == name)
|
||||||
|
throw Error("Name already exist.");
|
||||||
|
}
|
||||||
|
const geometry = new THREE.BoxGeometry(0.05, 1, 1.25);
|
||||||
|
const material = new THREE.MeshPhysicalMaterial();
|
||||||
|
const mesh = new THREE.Mesh(geometry, material);
|
||||||
|
|
||||||
// mesh.position.set(x, y, z);
|
mesh.position.set(x, y, z);
|
||||||
// this.arrObject.push({mesh: mesh, name: name});
|
this.arrObject.push({mesh: mesh, name: name});
|
||||||
// return (mesh);
|
return (mesh);
|
||||||
// };
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export { Map };
|
export { Map };
|
@ -6,7 +6,7 @@
|
|||||||
/* By: hubourge <hubourge@student.42.fr> +#+ +:+ +#+ */
|
/* By: hubourge <hubourge@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/08/18 00:53:53 by edbernar #+# #+# */
|
/* Created: 2024/08/18 00:53:53 by edbernar #+# #+# */
|
||||||
/* Updated: 2024/08/20 16:11:26 by hubourge ### ########.fr */
|
/* Updated: 2024/08/20 17:28:36 by hubourge ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,6 +14,7 @@ import * as THREE from 'three';
|
|||||||
import { Player } from './class/Player'
|
import { Player } from './class/Player'
|
||||||
import { Map } from './class/Map'
|
import { Map } from './class/Map'
|
||||||
import { OrbitControls } from 'three/examples/jsm/Addons.js';
|
import { OrbitControls } from 'three/examples/jsm/Addons.js';
|
||||||
|
import { update } from 'three/examples/jsm/libs/tween.module.js';
|
||||||
|
|
||||||
function createBarPlayer(color)
|
function createBarPlayer(color)
|
||||||
{
|
{
|
||||||
@ -29,6 +30,9 @@ function loop()
|
|||||||
{
|
{
|
||||||
player.update();
|
player.update();
|
||||||
renderer.render(scene, player.camera);
|
renderer.render(scene, player.camera);
|
||||||
|
|
||||||
|
// ===== test ball =====
|
||||||
|
updateBall();
|
||||||
}
|
}
|
||||||
|
|
||||||
function createMap()
|
function createMap()
|
||||||
@ -50,6 +54,40 @@ const player = new Player(bar);
|
|||||||
const spotLight = new THREE.SpotLight(0xffffff, 10000, 0, Math.PI / 4);
|
const spotLight = new THREE.SpotLight(0xffffff, 10000, 0, Math.PI / 4);
|
||||||
const ambiantLight = new THREE.AmbientLight(0xffffff, 1);
|
const ambiantLight = new THREE.AmbientLight(0xffffff, 1);
|
||||||
|
|
||||||
|
// ===== test ball =====
|
||||||
|
const geometryBall = new THREE.SphereGeometry(0.15, 32, 32);
|
||||||
|
const materialBall = new THREE.MeshPhysicalMaterial({color: 0xff0000});
|
||||||
|
const ball = new THREE.Mesh(geometryBall, materialBall);
|
||||||
|
ball.position.x = map.centerPos.x;
|
||||||
|
ball.position.y = map.centerPos.y + 0.15;
|
||||||
|
ball.position.z = map.centerPos.z;
|
||||||
|
ball.receiveShadow = true;
|
||||||
|
ball.castShadow = true;
|
||||||
|
scene.add(ball);
|
||||||
|
|
||||||
|
function updateBall()
|
||||||
|
{
|
||||||
|
// pressedButton = [];
|
||||||
|
let i = 0;
|
||||||
|
let interval = null;
|
||||||
|
let speed = 0.01;
|
||||||
|
const limits = {
|
||||||
|
up : 3,
|
||||||
|
down: 0.2,
|
||||||
|
left: -3,
|
||||||
|
right: 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('keypress', (e) => {
|
||||||
|
if (e.key == '9')
|
||||||
|
{
|
||||||
|
ball.position.z += speed;
|
||||||
|
console.log(e.key);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// =====================
|
||||||
|
|
||||||
scene.add(player.object);
|
scene.add(player.object);
|
||||||
scene.add(ambiantLight);
|
scene.add(ambiantLight);
|
||||||
spotLight.position.set(0, 100, 0);
|
spotLight.position.set(0, 100, 0);
|
||||||
|
Reference in New Issue
Block a user