Game
- Added event on key W and S
This commit is contained in:
70
site/game/controls.js
vendored
Normal file
70
site/game/controls.js
vendored
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* controls.js :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: hubourge <hubourge@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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 };
|
@ -3,14 +3,15 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* main.js :+: :+: :+: */
|
/* main.js :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ */
|
/* By: hubourge <hubourge@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:29:04 by edbernar ### ########.fr */
|
/* Updated: 2024/08/07 15:54:56 by hubourge ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
import { sendRequest } from './websocket.js';
|
import { sendRequest } from './websocket.js';
|
||||||
|
import { MoveObject } from './controls.js';
|
||||||
import * as THREE from 'three';
|
import * as THREE from 'three';
|
||||||
import Stats from 'stats.js';
|
import Stats from 'stats.js';
|
||||||
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.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.position.y = 0.1;
|
||||||
ball.castShadow = true;
|
ball.castShadow = true;
|
||||||
scene.add(ball);
|
scene.add(ball);
|
||||||
|
const controlBall = new MoveObject(ball);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
let spotLight = createSpotLight(0xffffff, ball, scene);
|
let spotLight = createSpotLight(0xffffff, ball, scene);
|
||||||
@ -57,9 +60,10 @@ let lightPoint = createLightPoint(scene);
|
|||||||
createMap(scene);
|
createMap(scene);
|
||||||
|
|
||||||
|
|
||||||
// const controls = new OrbitControls(camera, renderer.domElement);
|
const controls = new OrbitControls(camera, renderer.domElement);
|
||||||
// camera.position.set(0, 1, 5);
|
camera.position.set(0, 1, 5);
|
||||||
// controls.update();
|
controls.update();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function animate() {
|
function animate() {
|
||||||
|
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* map.js :+: :+: :+: */
|
/* map.js :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ */
|
/* By: hubourge <hubourge@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/07/30 13:50:51 by edbernar #+# #+# */
|
/* 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 wallUp;
|
||||||
let wallDown;
|
let wallDown;
|
||||||
let ground;
|
let ground;
|
||||||
|
let boxLeft;
|
||||||
|
let boxRight;
|
||||||
|
|
||||||
wallUp = createWall(scene, 0, 0.20, -2.5, 0.5);
|
wallUp = createWall(scene, 0, 0.25, -2.3, 0.1);
|
||||||
wallDown = createWall(scene, 0, 0.20, 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);
|
ground = createGround(scene);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,6 +50,18 @@ function createGround(scene) {
|
|||||||
return plane;
|
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 };
|
export { createMap };
|
Reference in New Issue
Block a user