- Starting solo game
This commit is contained in:
Kum1ta
2024-08-28 17:30:46 +02:00
parent 6aa715978e
commit 2fdba63def
5 changed files with 265 additions and 18 deletions

View File

@ -6,18 +6,23 @@
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/28 12:07:39 by edbernar #+# #+# */
/* Updated: 2024/08/28 14:07:55 by edbernar ### ########.fr */
/* Updated: 2024/08/28 16:48:12 by edbernar ### ########.fr */
/* */
/* ************************************************************************** */
import * as THREE from 'three';
import { Map } from './soloClass/Map.js'
import { Players } from './soloClass/Players.js'
import { Ball } from './soloClass/Ball.js'
import { stats } from './MultiGame.js';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
let scene = null;
let renderer = null;
let camera = null;
let map = null;
let controls = null;
class SoloGame
{
@ -25,21 +30,25 @@ class SoloGame
{
scene = new THREE.Scene();
renderer = new THREE.WebGLRenderer({antialias: true});
map = new Map(scene);
camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerWidth);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
Map.create(scene);
camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight);
camera.rotation.x = -Math.PI / 2;
renderer.setSize(window.innerWidth, window.innerWidth);
scene.background = new THREE.Color(0xaaaaaa);
renderer.setSize(window.innerWidth, window.innerHeight);
scene.background = new THREE.Color(0x252525);
document.body.appendChild(renderer.domElement);
scene.add(new THREE.AmbientLight(0xffffff, 1));
Players.create(scene);
Ball.create(scene);
controls = new OrbitControls(camera, renderer.domElement);
camera.position.set(0, 11, 0);
renderer.setAnimationLoop(loop)
}
static dispose()
{
if (map)
map.dispose();
map = null;
Map.dispose();
if (renderer)
renderer.dispose();
renderer = null;
@ -60,9 +69,13 @@ class SoloGame
}
};
function loop()
{
stats.begin();
controls.update();
Players.update();
Ball.update();
renderer.render(scene, camera);
stats.end();
}

View File

@ -0,0 +1,105 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Ball.js :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/28 15:58:03 by edbernar #+# #+# */
/* Updated: 2024/08/28 17:30:09 by edbernar ### ########.fr */
/* */
/* ************************************************************************** */
import * as THREE from 'three';
import { wallTop, wallBottom } from './Map.js';
let ball = null;
let speed = 0.3;
// max 0.3 (sinon ca sort);
let dir = -1;
class Ball
{
static create(scene)
{
ball = createBall();
scene.add(ball);
ball.rotateY(0.3);
}
static dispose()
{
ball = null;
}
static update()
{
moveForward(ball, speed, false);
bounceWallTop();
bounceWallTBottom();
}
}
function moveForward(object, speed, bounceTop)
{
const direction = new THREE.Vector3(0, 0, dir);
direction.applyQuaternion(object.quaternion);
object.position.add(direction.multiplyScalar(speed));
}
function createBall()
{
const geometry = new THREE.SphereGeometry(0.3);
const material = new THREE.MeshPhysicalMaterial({color: 0xffffff});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.y += 0.3;
mesh.position.set (0, mesh.position.y, 0);
return (mesh);
}
function bounceWallTop()
{
const origin = new THREE.Vector3(ball.position.x, ball.position.y, ball.position.z);
const direction = new THREE.Vector3(ball.position.x, ball.position.y, ball.position.z - 1);
direction.normalize();
const raycaster = new THREE.Raycaster(origin, direction);
const objects = [ wallTop ];
const intersects = raycaster.intersectObjects(objects);
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()
{
const origin = new THREE.Vector3(ball.position.x, ball.position.y, ball.position.z);
const direction = new THREE.Vector3(ball.position.x, ball.position.y, ball.position.z + 1);
direction.normalize();
const raycaster = new THREE.Raycaster(origin, direction);
const objects = [ wallBottom ];
const intersects = raycaster.intersectObjects(objects);
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;
}
}
}
export { Ball };

View File

@ -6,32 +6,66 @@
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/28 12:23:48 by edbernar #+# #+# */
/* Updated: 2024/08/28 14:01:24 by edbernar ### ########.fr */
/* Updated: 2024/08/28 17:01:17 by edbernar ### ########.fr */
/* */
/* ************************************************************************** */
import * as THREE from 'three';
const width = 25;
const height = 12.5;
let spotLight = null;
let wallTop = null;
let wallBottom = null;
class Map
{
constructor(scene)
static create(scene)
{
createGround(scene);
wallBottom = createWall(false);
wallTop = createWall(true);
spotLight = new THREE.SpotLight({color: 0xffffff});
spotLight.castShadow = true;
spotLight.position.y = 10;
spotLight.intensity = 200;
scene.add(spotLight);
scene.add(wallTop);
scene.add(wallBottom);
scene.add(new THREE.AmbientLight(0xffffff, 0.5));
}
dispose()
static dispose()
{
if (spotLight)
spotLight.dispose();
spotLight = null;
}
}
function createGround(scene)
{
const geometry = new THREE.PlaneGeometry(window.innerWidth / 100, window.innerHeight / 100);
const material = new THREE.MeshPhysicalMaterial();
const geometry = new THREE.PlaneGeometry(width, height);
const material = new THREE.MeshPhysicalMaterial({color: 0x222222});
const mesh = new THREE.Mesh(geometry, material);
mesh.rotateX(-Math.PI / 2);
mesh.position.set(0, 0, 0);
scene.add(mesh);
}
export { Map };
function createWall(onTop)
{
const geometry = new THREE.BoxGeometry(width, 0.7, 0.2);
const material = new THREE.MeshPhysicalMaterial({color: 0x333333});
const mesh = new THREE.Mesh(geometry, material);
if (onTop)
mesh.position.z = -6.15;
else
mesh.position.z = 6.15;
mesh.position.y += 0.35;
return (mesh);
}
export { Map, wallBottom, wallTop };

View File

@ -0,0 +1,95 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Players.js :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/28 15:12:25 by edbernar #+# #+# */
/* Updated: 2024/08/28 16:29:28 by edbernar ### ########.fr */
/* */
/* ************************************************************************** */
import * as THREE from 'three';
const speed = 0.25;
let player1 = null;
let player2 = null;
let pressedButton = [];
class Players
{
static create(scene)
{
player1 = newBarPlayer(1);
player2 = newBarPlayer(2);
scene.add(player1);
scene.add(player2);
document.addEventListener('keydown', addKeyInArr);
document.addEventListener('keyup', remKeyInArr);
}
static dispose()
{
document.removeEventListener('keydown', addKeyInArr);
document.removeEventListener('keyup', remKeyInArr);
player1 = null;
player2 = null;
}
static update()
{
let i = 0;
while (i < pressedButton.length)
{
if (pressedButton[i] == 'w' && player1.position.z > -5.05)
player1.position.z -= speed;
else if (pressedButton[i] == 's' && player1.position.z < 5.05)
player1.position.z += speed;
else if (pressedButton[i] == 'ArrowUp' && player2.position.z > -5.05)
player2.position.z -= speed;
else if (pressedButton[i] == 'ArrowDown' && player2.position.z < 5.05)
player2.position.z += speed;
i++;
}
}
}
function newBarPlayer(nbPlayer)
{
const geometry = new THREE.BoxGeometry(0.1, 0.2, 2);
const material = new THREE.MeshPhysicalMaterial({color: 0xffffff});
const mesh = new THREE.Mesh(geometry, material);
if (nbPlayer == 1)
mesh.position.set(-12, 0.2, 0);
else
mesh.position.set(12, 0.2, 0);
return (mesh);
}
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);
}
export { Players };

View File

@ -6,7 +6,7 @@
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/28 12:05:53 by edbernar #+# #+# */
/* Updated: 2024/08/28 12:20:25 by edbernar ### ########.fr */
/* Updated: 2024/08/28 14:30:05 by edbernar ### ########.fr */
/* */
/* ************************************************************************** */