- Continuing solo game
This commit is contained in:
Kum1ta
2024-08-29 00:45:13 +02:00
parent 250d7dd6db
commit dc0b8fb950
3 changed files with 71 additions and 35 deletions

View File

@ -6,7 +6,7 @@
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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.type = THREE.PCFSoftShadowMap;
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;
renderer.setSize(window.innerWidth, window.innerHeight);
scene.background = new THREE.Color(0x252525);
@ -42,8 +42,12 @@ class SoloGame
Ball.create(scene);
controls = new OrbitControls(camera, renderer.domElement);
camera.position.set(0, 11, 0);
renderer.setAnimationLoop(loop)
camera.position.set(0, 20, 0);
renderer.setAnimationLoop(loop);
setTimeout(() => {
Ball.moveBall();
}, 1000);
}
static dispose()
@ -75,7 +79,6 @@ function loop()
stats.begin();
controls.update();
Players.update();
Ball.update();
renderer.render(scene, camera);
stats.end();
}

View File

@ -6,17 +6,18 @@
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 { wallTop, wallBottom } from './Map.js';
import { player1, player2 } from './Players.js';
let ball = null;
let speed = 0.3;
// max 0.3 (sinon ca sort);
let speed = 0.15;
let dir = -1;
let interval = null;
class Ball
{
@ -26,7 +27,7 @@ class Ball
ball = createBall();
scene.add(ball);
ball.rotateY(0.3);
ball.rotateY(0.8);
}
@ -35,19 +36,36 @@ class Ball
ball = null;
}
static update()
static moveBall()
{
moveForward(ball, speed, false);
bounceWallTop();
bounceWallTBottom();
createInterval();
}
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);
direction.applyQuaternion(object.quaternion);
object.position.add(direction.multiplyScalar(speed));
direction.applyQuaternion(ball.quaternion);
ball.position.add(direction.multiplyScalar(speed));
}
function createBall()
@ -57,7 +75,8 @@ function createBall()
const mesh = new THREE.Mesh(geometry, material);
mesh.position.y += 0.3;
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.position.set (0, mesh.position.y, 0);
return (mesh);
}
@ -74,13 +93,10 @@ function bounceWallTop()
if (intersects.length > 0)
{
console.log("Distance du rayon à l'objet : ", intersects[0].distance);
if (intersects[0].distance <= 0.5)
{
ball.rotation.y = Math.PI - ball.rotation.y
}
}
}
function bounceWallTBottom()
{
@ -94,12 +110,26 @@ function bounceWallTBottom()
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.4)
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;
}
}
export { Ball };

View File

@ -6,7 +6,7 @@
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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()
{
const limits = 4.55;
let i = 0;
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;
else if (pressedButton[i] == 's' && player1.position.z < 5.05)
else if (pressedButton[i] == 's' && player1.position.z < limits)
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;
else if (pressedButton[i] == 'ArrowDown' && player2.position.z < 5.05)
else if (pressedButton[i] == 'ArrowDown' && player2.position.z < limits)
player2.position.z += speed;
i++;
}
@ -59,14 +60,16 @@ class Players
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 mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
if (nbPlayer == 1)
mesh.position.set(-12, 0.2, 0);
mesh.position.set(-12, 0.4, 0);
else
mesh.position.set(12, 0.2, 0);
mesh.position.set(12, 0.4, 0);
return (mesh);
}
@ -92,4 +95,4 @@ function remKeyInArr(e)
pressedButton.splice(i, 1);
}
export { Players };
export { Players, player1, player2 };