Gane
- Continuing solo game
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/08/28 12:07:39 by edbernar #+# #+# */
|
/* Created: 2024/08/28 12:07:39 by edbernar #+# #+# */
|
||||||
/* Updated: 2024/08/28 16:48:12 by edbernar ### ########.fr */
|
/* Updated: 2024/08/29 00:19:01 by edbernar ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -33,7 +33,7 @@ class SoloGame
|
|||||||
renderer.shadowMap.enabled = true;
|
renderer.shadowMap.enabled = true;
|
||||||
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
|
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
|
||||||
Map.create(scene);
|
Map.create(scene);
|
||||||
camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight);
|
camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight);
|
||||||
camera.rotation.x = -Math.PI / 2;
|
camera.rotation.x = -Math.PI / 2;
|
||||||
renderer.setSize(window.innerWidth, window.innerHeight);
|
renderer.setSize(window.innerWidth, window.innerHeight);
|
||||||
scene.background = new THREE.Color(0x252525);
|
scene.background = new THREE.Color(0x252525);
|
||||||
@ -42,8 +42,12 @@ class SoloGame
|
|||||||
Ball.create(scene);
|
Ball.create(scene);
|
||||||
|
|
||||||
controls = new OrbitControls(camera, renderer.domElement);
|
controls = new OrbitControls(camera, renderer.domElement);
|
||||||
camera.position.set(0, 11, 0);
|
camera.position.set(0, 20, 0);
|
||||||
renderer.setAnimationLoop(loop)
|
renderer.setAnimationLoop(loop);
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
Ball.moveBall();
|
||||||
|
}, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
static dispose()
|
static dispose()
|
||||||
@ -75,7 +79,6 @@ function loop()
|
|||||||
stats.begin();
|
stats.begin();
|
||||||
controls.update();
|
controls.update();
|
||||||
Players.update();
|
Players.update();
|
||||||
Ball.update();
|
|
||||||
renderer.render(scene, camera);
|
renderer.render(scene, camera);
|
||||||
stats.end();
|
stats.end();
|
||||||
}
|
}
|
||||||
|
@ -6,17 +6,18 @@
|
|||||||
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/08/28 15:58:03 by edbernar #+# #+# */
|
/* Created: 2024/08/28 15:58:03 by edbernar #+# #+# */
|
||||||
/* Updated: 2024/08/28 17:30:09 by edbernar ### ########.fr */
|
/* Updated: 2024/08/29 00:44:26 by edbernar ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
import * as THREE from 'three';
|
import * as THREE from 'three';
|
||||||
import { wallTop, wallBottom } from './Map.js';
|
import { wallTop, wallBottom } from './Map.js';
|
||||||
|
import { player1, player2 } from './Players.js';
|
||||||
|
|
||||||
let ball = null;
|
let ball = null;
|
||||||
let speed = 0.3;
|
let speed = 0.15;
|
||||||
// max 0.3 (sinon ca sort);
|
|
||||||
let dir = -1;
|
let dir = -1;
|
||||||
|
let interval = null;
|
||||||
|
|
||||||
class Ball
|
class Ball
|
||||||
{
|
{
|
||||||
@ -26,8 +27,8 @@ class Ball
|
|||||||
ball = createBall();
|
ball = createBall();
|
||||||
|
|
||||||
scene.add(ball);
|
scene.add(ball);
|
||||||
ball.rotateY(0.3);
|
ball.rotateY(0.8);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static dispose()
|
static dispose()
|
||||||
@ -35,19 +36,36 @@ class Ball
|
|||||||
ball = null;
|
ball = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
static update()
|
static moveBall()
|
||||||
{
|
{
|
||||||
moveForward(ball, speed, false);
|
createInterval();
|
||||||
bounceWallTop();
|
}
|
||||||
bounceWallTBottom();
|
|
||||||
|
|
||||||
|
static stopBall()
|
||||||
|
{
|
||||||
|
if (interval)
|
||||||
|
clearInterval(interval);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function moveForward(object, speed, bounceTop)
|
function createInterval()
|
||||||
|
{
|
||||||
|
interval = setInterval(() => {
|
||||||
|
console.log(ball.position);
|
||||||
|
moveForward();
|
||||||
|
bounceWallTop();
|
||||||
|
bounceWallTBottom();
|
||||||
|
bouncePlayer1();
|
||||||
|
}, 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
function moveForward()
|
||||||
{
|
{
|
||||||
const direction = new THREE.Vector3(0, 0, dir);
|
const direction = new THREE.Vector3(0, 0, dir);
|
||||||
direction.applyQuaternion(object.quaternion);
|
direction.applyQuaternion(ball.quaternion);
|
||||||
object.position.add(direction.multiplyScalar(speed));
|
|
||||||
|
ball.position.add(direction.multiplyScalar(speed));
|
||||||
}
|
}
|
||||||
|
|
||||||
function createBall()
|
function createBall()
|
||||||
@ -57,7 +75,8 @@ function createBall()
|
|||||||
const mesh = new THREE.Mesh(geometry, material);
|
const mesh = new THREE.Mesh(geometry, material);
|
||||||
|
|
||||||
mesh.position.y += 0.3;
|
mesh.position.y += 0.3;
|
||||||
|
mesh.castShadow = true;
|
||||||
|
mesh.receiveShadow = true;
|
||||||
mesh.position.set (0, mesh.position.y, 0);
|
mesh.position.set (0, mesh.position.y, 0);
|
||||||
return (mesh);
|
return (mesh);
|
||||||
}
|
}
|
||||||
@ -74,11 +93,8 @@ function bounceWallTop()
|
|||||||
|
|
||||||
if (intersects.length > 0)
|
if (intersects.length > 0)
|
||||||
{
|
{
|
||||||
console.log("Distance du rayon à l'objet : ", intersects[0].distance);
|
|
||||||
if (intersects[0].distance <= 0.5)
|
if (intersects[0].distance <= 0.5)
|
||||||
{
|
|
||||||
ball.rotation.y = Math.PI - ball.rotation.y
|
ball.rotation.y = Math.PI - ball.rotation.y
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,11 +110,25 @@ function bounceWallTBottom()
|
|||||||
|
|
||||||
if (intersects.length > 0)
|
if (intersects.length > 0)
|
||||||
{
|
{
|
||||||
console.log("Distance du rayon à l'objet : ", intersects[0].distance);
|
if (intersects[0].distance <= 0.4)
|
||||||
if (intersects[0].distance <= 0.5)
|
ball.rotation.y = Math.PI - ball.rotation.y;
|
||||||
{
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function bouncePlayer1()
|
||||||
|
{
|
||||||
|
const origin = new THREE.Vector3(ball.position.x, ball.position.y, ball.position.z);
|
||||||
|
const direction = new THREE.Vector3(ball.position.x - 1, ball.position.y, ball.position.z);
|
||||||
|
|
||||||
|
direction.normalize();
|
||||||
|
const raycaster = new THREE.Raycaster(origin, direction);
|
||||||
|
const objects = [ player1 ];
|
||||||
|
const intersects = raycaster.intersectObjects(objects);
|
||||||
|
|
||||||
|
if (intersects.length > 0)
|
||||||
|
{
|
||||||
|
if (intersects[0].distance <= 0.4)
|
||||||
ball.rotation.y = Math.PI - ball.rotation.y;
|
ball.rotation.y = Math.PI - ball.rotation.y;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/08/28 15:12:25 by edbernar #+# #+# */
|
/* Created: 2024/08/28 15:12:25 by edbernar #+# #+# */
|
||||||
/* Updated: 2024/08/28 16:29:28 by edbernar ### ########.fr */
|
/* Updated: 2024/08/29 00:25:50 by edbernar ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -40,17 +40,18 @@ class Players
|
|||||||
|
|
||||||
static update()
|
static update()
|
||||||
{
|
{
|
||||||
let i = 0;
|
const limits = 4.55;
|
||||||
|
let i = 0;
|
||||||
|
|
||||||
while (i < pressedButton.length)
|
while (i < pressedButton.length)
|
||||||
{
|
{
|
||||||
if (pressedButton[i] == 'w' && player1.position.z > -5.05)
|
if (pressedButton[i] == 'w' && player1.position.z > -limits)
|
||||||
player1.position.z -= speed;
|
player1.position.z -= speed;
|
||||||
else if (pressedButton[i] == 's' && player1.position.z < 5.05)
|
else if (pressedButton[i] == 's' && player1.position.z < limits)
|
||||||
player1.position.z += speed;
|
player1.position.z += speed;
|
||||||
else if (pressedButton[i] == 'ArrowUp' && player2.position.z > -5.05)
|
else if (pressedButton[i] == 'ArrowUp' && player2.position.z > -limits)
|
||||||
player2.position.z -= speed;
|
player2.position.z -= speed;
|
||||||
else if (pressedButton[i] == 'ArrowDown' && player2.position.z < 5.05)
|
else if (pressedButton[i] == 'ArrowDown' && player2.position.z < limits)
|
||||||
player2.position.z += speed;
|
player2.position.z += speed;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
@ -59,14 +60,16 @@ class Players
|
|||||||
|
|
||||||
function newBarPlayer(nbPlayer)
|
function newBarPlayer(nbPlayer)
|
||||||
{
|
{
|
||||||
const geometry = new THREE.BoxGeometry(0.1, 0.2, 2);
|
const geometry = new THREE.BoxGeometry(0.3, 0.4, 2.5);
|
||||||
const material = new THREE.MeshPhysicalMaterial({color: 0xffffff});
|
const material = new THREE.MeshPhysicalMaterial({color: 0xffffff});
|
||||||
const mesh = new THREE.Mesh(geometry, material);
|
const mesh = new THREE.Mesh(geometry, material);
|
||||||
|
|
||||||
|
mesh.castShadow = true;
|
||||||
|
mesh.receiveShadow = true;
|
||||||
if (nbPlayer == 1)
|
if (nbPlayer == 1)
|
||||||
mesh.position.set(-12, 0.2, 0);
|
mesh.position.set(-12, 0.4, 0);
|
||||||
else
|
else
|
||||||
mesh.position.set(12, 0.2, 0);
|
mesh.position.set(12, 0.4, 0);
|
||||||
return (mesh);
|
return (mesh);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,4 +95,4 @@ function remKeyInArr(e)
|
|||||||
pressedButton.splice(i, 1);
|
pressedButton.splice(i, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
export { Players };
|
export { Players, player1, player2 };
|
Reference in New Issue
Block a user