- Added request when w or s pressed
This commit is contained in:
edbernar
2024-08-07 16:22:47 +02:00
parent 2291266bd1
commit aa01b67037
9 changed files with 26 additions and 14 deletions

29
site/game/controls.js vendored
View File

@ -3,24 +3,29 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* controls.js :+: :+: :+: */ /* controls.js :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: hubourge <hubourge@student.42.fr> +#+ +:+ +#+ */ /* By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/07 15:20:55 by hubourge #+# #+# */ /* Created: 2024/08/07 15:20:55 by hubourge #+# #+# */
/* Updated: 2024/08/07 15:58:16 by hubourge ### ########.fr */ /* Updated: 2024/08/07 16:19:53 by edbernar ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
import { sendRequest } from "./websocket.js";
import * as THREE from 'three'; import * as THREE from 'three';
class Moves { class Moves {
wPress = false; wPress = false;
sPress = false; sPress = false;
constructor() {}; constructor() {};
} }
class MoveObject { class MoveObject {
#moves = null; #moves = null;
#object = null; #object = null;
#speed = 0.1;
constructor(object) constructor(object)
{ {
let key = ['w', 's']; let key = ['w', 's'];
@ -39,12 +44,6 @@ class MoveObject {
document.addEventListener("keydown", (event) => { document.addEventListener("keydown", (event) => {
for (let i = 0; i < key.length; i++) 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]) if (event.key == key[i])
{ {
(movesValueDown[i])(); (movesValueDown[i])();
@ -64,7 +63,19 @@ class MoveObject {
}); });
}; };
update() {}; update()
{
if (this.moves.wPress)
{
this.object.position.z -= this.#speed;
sendRequest("playerMove", {x: this.object.position.x, y: this.object.position.y, z: this.object.position.z});
}
if (this.moves.sPress)
{
this.object.position.z += this.#speed;
sendRequest("playerMove", {x: this.object.position.x, y: this.object.position.y, z: this.object.position.z});
}
};
} }
export { MoveObject }; export { MoveObject };

View File

@ -6,7 +6,7 @@
/* By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ */ /* By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/30 13:50:46 by edbernar #+# #+# */ /* Created: 2024/07/30 13:50:46 by edbernar #+# #+# */
/* Updated: 2024/07/30 13:50:47 by edbernar ### ########.fr */ /* Updated: 2024/08/07 16:20:11 by edbernar ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* main.js :+: :+: :+: */ /* main.js :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: hubourge <hubourge@student.42.fr> +#+ +:+ +#+ */ /* By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/30 13:50:49 by edbernar #+# #+# */ /* Created: 2024/07/30 13:50:49 by edbernar #+# #+# */
/* Updated: 2024/08/07 15:54:56 by hubourge ### ########.fr */ /* Updated: 2024/08/07 16:21:01 by edbernar ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -78,6 +78,7 @@ function animate() {
ball.position.x = Math.sin(Date.now() * 0.001) * 2; ball.position.x = Math.sin(Date.now() * 0.001) * 2;
ball.position.z = Math.cos(Date.now() * 0.001) * 2; ball.position.z = Math.cos(Date.now() * 0.001) * 2;
renderer.render(scene, camera); renderer.render(scene, camera);
controlBall.update();
stats.end(); stats.end();
} }

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* map.js :+: :+: :+: */ /* map.js :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: hubourge <hubourge@student.42.fr> +#+ +:+ +#+ */ /* By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/30 13:50:51 by edbernar #+# #+# */ /* Created: 2024/07/30 13:50:51 by edbernar #+# #+# */
/* Updated: 2024/08/07 15:34:12 by hubourge ### ########.fr */ /* Updated: 2024/08/07 16:20:08 by edbernar ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */