From 5f5b164ccb2d42b43e2b4ce3b2f2fe1a8edd754f Mon Sep 17 00:00:00 2001 From: Hugo Bourgeon Date: Wed, 7 Aug 2024 15:59:42 +0200 Subject: [PATCH] Game - Added event on key W and S --- site/game/controls.js | 70 +++++++++++++++++++++++++++++++++++++++++++ site/game/main.js | 14 +++++---- site/game/map.js | 28 +++++++++++++---- 3 files changed, 101 insertions(+), 11 deletions(-) create mode 100644 site/game/controls.js diff --git a/site/game/controls.js b/site/game/controls.js new file mode 100644 index 0000000..de664f4 --- /dev/null +++ b/site/game/controls.js @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* controls.js :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hubourge +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/07 15:20:55 by hubourge #+# #+# */ +/* Updated: 2024/08/07 15:58:16 by hubourge ### ########.fr */ +/* */ +/* ************************************************************************** */ + +import * as THREE from 'three'; + +class Moves { + wPress = false; + sPress = false; + constructor() {}; +} + +class MoveObject { + #moves = null; + #object = null; + constructor(object) + { + let key = ['w', 's']; + let movesValueDown = [ + () => { this.moves.wPress = true; }, + () => { this.moves.sPress = true; } + ]; + let movesValueUp = [ + () => { this.moves.wPress = false; }, + () => { this.moves.sPress = false; } + ]; + + + this.moves = new Moves(); + this.object = object; + document.addEventListener("keydown", (event) => { + for (let i = 0; i < key.length; i++) + { + if (event.key == '-') + { + console.log(this.moves.wPress); + console.log(this.moves.sPress); + return ; + } + if (event.key == key[i]) + { + (movesValueDown[i])(); + return ; + } + } + }); + document.addEventListener("keyup", (event) => { + for (let i = 0; i < key.length; i++) + { + if (event.key == key[i]) + { + (movesValueUp[i])(); + return ; + } + } + }); + }; + + update() {}; +} + +export { MoveObject }; \ No newline at end of file diff --git a/site/game/main.js b/site/game/main.js index c6c7b36..ef43954 100644 --- a/site/game/main.js +++ b/site/game/main.js @@ -3,14 +3,15 @@ /* ::: :::::::: */ /* main.js :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: edbernar +#+ +:+ +#+ */ +/* By: hubourge +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/30 13:50:49 by edbernar #+# #+# */ -/* Updated: 2024/08/07 15:29:04 by edbernar ### ########.fr */ +/* Updated: 2024/08/07 15:54:56 by hubourge ### ########.fr */ /* */ /* ************************************************************************** */ import { sendRequest } from './websocket.js'; +import { MoveObject } from './controls.js'; import * as THREE from 'three'; import Stats from 'stats.js'; import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'; @@ -49,6 +50,8 @@ const ball = new THREE.Mesh(geometryBall, materialBall); ball.position.y = 0.1; ball.castShadow = true; scene.add(ball); +const controlBall = new MoveObject(ball); + let spotLight = createSpotLight(0xffffff, ball, scene); @@ -57,9 +60,10 @@ let lightPoint = createLightPoint(scene); createMap(scene); -// const controls = new OrbitControls(camera, renderer.domElement); -// camera.position.set(0, 1, 5); -// controls.update(); +const controls = new OrbitControls(camera, renderer.domElement); +camera.position.set(0, 1, 5); +controls.update(); + function animate() { diff --git a/site/game/map.js b/site/game/map.js index 7bc71a7..eaf2888 100644 --- a/site/game/map.js +++ b/site/game/map.js @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* map.js :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: edbernar +#+ +:+ +#+ */ +/* By: hubourge +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/30 13:50:51 by edbernar #+# #+# */ -/* Updated: 2024/07/30 13:50:51 by edbernar ### ########.fr */ +/* Updated: 2024/08/07 15:34:12 by hubourge ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,9 +16,13 @@ function createMap(scene) { let wallUp; let wallDown; let ground; + let boxLeft; + let boxRight; - wallUp = createWall(scene, 0, 0.20, -2.5, 0.5); - wallDown = createWall(scene, 0, 0.20, 2.3, 0.1); + wallUp = createWall(scene, 0, 0.25, -2.3, 0.1); + wallDown = createWall(scene, 0, 0.25, 2.3, 0.1); + boxLeft = createBox(scene, 0, 0.25, 0); + // boxRight = createBox(scene, 0, 0.25, 0); ground = createGround(scene); } @@ -46,6 +50,18 @@ function createGround(scene) { return plane; } +function createBox(scene, x, y, z) +{ + const geometryBox = new THREE.BoxGeometry(1, 0.5, 0.1); + const materialBox = new THREE.MeshLambertMaterial({ + color: 0xff0000, + }); + const box = new THREE.Mesh(geometryBox, materialBox); + box.position.set(x, y, z); + box.rotateY(Math.PI / 2); + box.receiveShadow = true; + scene.add(box); + return box; +} - -export { createMap }; \ No newline at end of file +export { createMap };