- multiplayer move
This commit is contained in:
Kum1ta
2024-09-16 15:52:38 +02:00
parent cbf665b837
commit 12b33d0ee4
5 changed files with 67 additions and 13 deletions

View File

@ -6,7 +6,7 @@
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/21 10:34:49 by edbernar #+# #+# */
/* Updated: 2024/09/15 14:56:35 by edbernar ### ########.fr */
/* Updated: 2024/09/16 15:51:15 by edbernar ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,10 +15,15 @@ let opponentExist = false;
class Opponent
{
object = null;
speed = 0.1;
speed = 4;
interval = null;
limits = {};
player = null;
limits = {
up : 3,
down: 0.3,
left: -3,
right: 3,
};
last = false;
constructor (object, map)
{
@ -26,7 +31,6 @@ class Opponent
throw Error("Opponent is already init.");
opponentExist = true;
this.object = object;
this.limits = map.limits;
this.object.position.set(0, 0.3, -map.mapLength / 2 + 0.2);
}
@ -37,7 +41,40 @@ class Opponent
update()
{
//en attente du serveur
console.log(this.object.position);
}
movePlayer(content)
{
const thisClass = this;
this.object.position.x = content.pos;
if (content.up && thisClass.object.position.y < thisClass.limits.up)
{
if (this.interval)
clearInterval(this.interval);
thisClass.interval = setInterval(() => {
thisClass.object.position.y += thisClass.speed / 40;
if (thisClass.object.position.y >= thisClass.limits.up)
{
clearInterval(thisClass.interval);
thisClass.interval = null;
}
}, 5);
}
else if (!content.up && thisClass.object.position.y > thisClass.limits.down)
{
if (this.interval)
clearInterval(this.interval);
this.interval = setInterval(() => {
thisClass.object.position.y -= thisClass.speed / 40;
if (thisClass.object.position.y <= thisClass.limits.down)
{
clearInterval(thisClass.interval);
thisClass.interval = null;
thisClass.object.position.y = thisClass.limits.down;
}
}, 5);
}
}
}

View File

@ -6,7 +6,7 @@
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/18 00:30:31 by edbernar #+# #+# */
/* Updated: 2024/09/15 14:57:56 by edbernar ### ########.fr */
/* Updated: 2024/09/16 15:44:46 by edbernar ### ########.fr */
/* */
/* ************************************************************************** */
@ -49,6 +49,7 @@ let pressedButton = [];
class Player
{
isUp = false;
object = null;
camera = null;
speed = 4;
@ -207,6 +208,7 @@ class Player
{
if (pressedButton[i] == 'w' && this.object.position.y < this.limits.up)
{
this.isUp = true;
if (this.interval)
clearInterval(this.interval);
this.interval = setInterval(() => {
@ -222,6 +224,7 @@ class Player
}
if (pressedButton[i] == 's' && this.object.position.y > this.limits.down)
{
this.isUp = false;
if (this.interval)
clearInterval(this.interval);
this.interval = setInterval(() => {

View File

@ -6,11 +6,12 @@
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/18 00:53:53 by edbernar #+# #+# */
/* Updated: 2024/09/15 15:07:29 by edbernar ### ########.fr */
/* Updated: 2024/09/16 15:48:15 by edbernar ### ########.fr */
/* */
/* ************************************************************************** */
import * as THREE from '/static/javascript/three/build/three.module.js'
import { sendRequest } from "/static/javascript/websocket.js";
import { Player } from '/static/javascript/multiOnlineGame/Player.js'
import { Map } from '/static/javascript/multiOnlineGame/Map.js'
import { Ball } from '/static/javascript/multiOnlineGame/Ball.js'
@ -50,6 +51,7 @@ let player = null;
let spotLight = null;
let ambiantLight = null;
let opponent = null;
let interval = null;
class MultiOnlineGamePage
{
@ -107,6 +109,10 @@ class MultiOnlineGamePage
})
renderer.setAnimationLoop(loop)
sendRequest('game', {action: 1});
interval = setInterval(() => {
sendRequest('game', {action: 3, pos: player.object.position.x, up: player.isUp});
}, 1000 / 20);
}
static dispose()
@ -160,8 +166,9 @@ function changeBarColor(bar, color)
function loop()
{
player.update();
opponent.update();
map.update(ball);
renderer.render(scene, player.camera);
}
export { MultiOnlineGamePage };
export { MultiOnlineGamePage, opponent };

View File

@ -6,21 +6,29 @@
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/15 12:00:01 by edbernar #+# #+# */
/* Updated: 2024/09/15 14:23:31 by edbernar ### ########.fr */
/* Updated: 2024/09/16 15:05:01 by edbernar ### ########.fr */
/* */
/* ************************************************************************** */
import { MultiOnlineGamePage, opponent } from "/static/javascript/multiOnlineGame/multiOnlineGamePage.js"
import { WaitingGamePage } from "/static/javascript/waitingGame/main.js"
import { pageRenderer } from '/static/javascript/main.js'
function typeGame(content)
{
console.log("New action game : " + content.action);
if (content.action == 1)
{
if (pageRenderer.actualPage == WaitingGamePage)
WaitingGamePage.showOpponent(content.username);
}
else if (content.action == 3)
{
if (pageRenderer.actualPage == MultiOnlineGamePage)
{
if (content.is_opponent)
opponent.movePlayer(content);
}
}
}
export { typeGame };

View File

@ -6,7 +6,7 @@
/* By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/31 22:17:24 by edbernar #+# #+# */
/* Updated: 2024/09/15 11:55:47 by edbernar ### ########.fr */
/* Updated: 2024/09/16 15:47:51 by edbernar ### ########.fr */
/* */
/* ************************************************************************** */
@ -69,7 +69,6 @@ function launchSocket()
}
else
{
console.log(response);
try {
functionResponse[typeResponse.indexOf(response.type)](response.content);
} catch (error)