From 8da97c8e601922f8bc920d23f15380bd8d03d959 Mon Sep 17 00:00:00 2001 From: Kum1ta Date: Sun, 18 Aug 2024 16:19:27 +0200 Subject: [PATCH] Game - Added 2 mods on cameras --- site/game/map.js | 4 +- site/real_game/class/Player.js | 71 ++++++++++++++++++++++++++++++---- site/real_game/main.js | 42 +++++++++++++++++--- 3 files changed, 102 insertions(+), 15 deletions(-) diff --git a/site/game/map.js b/site/game/map.js index ed48400..96b6432 100644 --- a/site/game/map.js +++ b/site/game/map.js @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* map.js :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: hubourge +#+ +:+ +#+ */ +/* By: edbernar { playerExist = false; }) @@ -60,6 +80,21 @@ class Player if (i != this.pressedButton.length) this.pressedButton.splice(i, 1); }); + + document.addEventListener('keypress', (e) => { + if (e.key == 'm') + { + this.cameraFixed = !this.cameraFixed; + if (!this.cameraFixed) + this.setCameraPosition( + this.object.position.x, + this.object.position.y + 0.5, + this.object.position.z + 1 + ); + else + this.setCameraPosition(0, 1, 1) + } + }); } update() @@ -67,13 +102,35 @@ class Player let i; i = 0; + if (this.cameraFixed) + { + this.orbital.target = new THREE.Vector3(this.object.position.x, this.object.position.y, this.object.position.z - 5); + this.orbital.update(); + } + else + this.camera.rotation.set(0, 0, 0); while (i < this.pressedButton.length) { - if (this.pressedButton[i] == 'a') - console.log('A is pressed !'); + if (this.pressedButton[i] == 'w' || this.pressedButton[i] == 's') + { + this.object.position.y += (this.pressedButton[i] == 'w' ? this.speed : -this.speed); + if (!this.cameraFixed) + this.camera.position.y += (this.pressedButton[i] == 'w' ? this.speed : -this.speed); + } + if (this.pressedButton[i] == 'a' || this.pressedButton[i] == 'd') + { + this.object.position.x += (this.pressedButton[i] == 'a' ? -this.speed : this.speed); + if (!this.cameraFixed) + this.camera.position.x += (this.pressedButton[i] == 'a' ? -this.speed : this.speed); + } i++; } } + + setCameraPosition(x, y, z) + { + this.camera.position.set(x, y, z); + } }; diff --git a/site/real_game/main.js b/site/real_game/main.js index 22d0235..449e862 100644 --- a/site/real_game/main.js +++ b/site/real_game/main.js @@ -6,14 +6,44 @@ /* By: edbernar { - player.update(); -}, 100); \ No newline at end of file + return (mesh); +} + +function loop() +{ + // controls.update(); + player.update(); + renderer.render(scene, player.camera); +} + + +const scene = new THREE.Scene(); +const bar = createBarPlayer(0xed56ea); +const renderer = new THREE.WebGLRenderer(); +const player = new Player(bar, renderer); +const spotLight = new THREE.SpotLight(0xffffff, 1, 0, Math.PI / 4); +const helper = new THREE.SpotLightHelper(spotLight); +// const controls = new OrbitControls(player.camera, renderer.domElement); + +scene.add(player.object); +spotLight.target = player.object; +scene.add(spotLight); +scene.add(helper); +scene.background = new THREE.Color(0x1a1a1a); +renderer.setSize(window.innerWidth, window.innerHeight); +document.body.appendChild(renderer.domElement); +player.setCameraPosition(0, 0.5, 1) +renderer.setAnimationLoop(loop)