From f6cbebe532b2dd2db859dabc8d02d79fe4442977 Mon Sep 17 00:00:00 2001 From: Atsuyo-INSA Date: Tue, 12 Dec 2023 13:53:55 +0100 Subject: [PATCH] bientot ok ? --- public_html/js/game.js | 4 +- public_html/js/global.js | 34 +++++++---- public_html/js/input.js | 11 +--- public_html/js/network.js | 12 ++-- public_html/js/objects.js | 6 +- public_html/js/render.js | 123 +++++++++++++++++--------------------- 6 files changed, 93 insertions(+), 97 deletions(-) diff --git a/public_html/js/game.js b/public_html/js/game.js index fcc2403..7f64be7 100644 --- a/public_html/js/game.js +++ b/public_html/js/game.js @@ -11,10 +11,10 @@ function game() { cars.forEach((c) => { c.Update(); - if(c.collide(player.x,player.y)) + if(c.collide(player.x,player.y,player.z)) { net.died(player.id,-1); - player.x=-50; + player.z=-1; player.deaths++; player.health=10; } diff --git a/public_html/js/global.js b/public_html/js/global.js index 56bcbd6..dbde358 100644 --- a/public_html/js/global.js +++ b/public_html/js/global.js @@ -9,17 +9,17 @@ let inp = new Input("canvas"); let bullets = []; let circles = []; let squares = []; -let PNJS = [new PNJ(500, 100), - new PNJ(700, 100), - new PNJ(500, 600), - new PNJ(200, 700)]; +let PNJS = [new PNJ(500, 100,0), + new PNJ(700, 100,0), + new PNJ(500, 600,0), + new PNJ(200, 700,0)]; -let cars = [new Car(renderer, 0, 0), - new Car(renderer, 0, 7), - new Car(renderer, 1, 7), - new Car(renderer, 1, 13), - new Car(renderer, 1, 14), - new Car(renderer, 0, 15)]; +let cars = [new Car(0, 0), + new Car(1, 7), + new Car(1, 13), + new Car(1, 14), + new Car(0, 7), + new Car(0, 15)]; function updatePlayer(data) @@ -28,6 +28,7 @@ function updatePlayer(data) { player.x=data.x; player.y=data.y; + player.z=data.z; } else { @@ -37,6 +38,9 @@ function updatePlayer(data) { players[i].x=data.x; players[i].y=data.y; + if(data.z==undefined) + data.z=0; + players[i].z=data.z; players[i].dir=data.dir; players[i].visibleDir=data.visibleDir; break; @@ -47,10 +51,18 @@ function updatePlayer(data) function addPlayer(data) { - let np = new Player(data.id, data.x, data.y, data.name, data.dir); + let np = new Player(data.id, data.x, data.y, 0, data.name, data.dir); players.push(np); } + + + + + + + + function removePlayer(id) { for(let i=0;i=2000 && player.y>=2000) { - mouseX+=2000; - mouseY+=2000; - } let dx = mouseX-player.x; let dy = mouseY-player.y; let norm = Math.sqrt(dx*dx+dy*dy); - let b = new Bullet(player.x,player.y,dx/norm,dy/norm,player.id); + let b = new Bullet(player.x,player.y,player.z,dx/norm,dy/norm,player.id); bullets.push(b); - net.newBullet(b.x,b.y,b.dx,b.dy,b.parentId); + net.newBullet(b.x,b.y,b.z,b.dx,b.dy,b.parentId); }); window.addEventListener("keydown", (e)=>{ //blocks the action of the key (cf. Killian) - if(["Space","ArrowUp","ArrowDown","ArrowLeft","ArrowRight"].indexOf(e.code) > -1) { + if(["Space","ArrowUp","ArrowDown","ArrowLeft","ArrowRight"].includes(e.code)) { e.preventDefault(); } this.keysDown.add(e.key.toLowerCase()) diff --git a/public_html/js/network.js b/public_html/js/network.js index 0faf7df..0cd871f 100644 --- a/public_html/js/network.js +++ b/public_html/js/network.js @@ -12,9 +12,9 @@ class Network{ for (let i = 0; i=2000 && y>=2000 && this.map==1) { - x=player.x-2000 - y=player.y-2000 - } - - //this.map==1 && (x<2000 || y<2000) - if((this.map==1 && player.x>=2000 && player.y>=2000) || (this.map==0 && x<2000 && y <2000)) { + let x=p.x + let y=p.y + + if(p.z==player.z) + { this.ctx.save(); this.ctx.translate(x, y); - this.ctx.rotate(player.angle); + this.ctx.rotate(p.angle); this.ctx.drawImage(imgPlayer, -playerSize / 2, -playerSize / 2, playerSize, playerSize); this.ctx.restore(); this.ctx.fillStyle = 'white'; this.ctx.font="10pt arial"; - this.ctx.fillText(player.name,x-player.name.length*10/3,y-playerSize/1.8); - if(client) { + this.ctx.fillText(p.name,x-p.name.length*10/3,y-playerSize/1.8); + if(isClient) { this.ctx.fillStyle = 'red'; this.ctx.fillRect(x-playerSize/2-5, y-playerSize/2, playerSize+10, 5); this.ctx.fillStyle = '#7AFF33'; - this.ctx.fillRect(x-playerSize/2-5, y-playerSize/2, player.health*(playerSize+10)/defaulthealth, 5); + this.ctx.fillRect(x-playerSize/2-5, y-playerSize/2, p.health*(playerSize+10)/defaulthealth, 5); } } } - RenderCar(x,y,angle) { - if(this.map==0) { + RenderCar(x,y,z,angle) { + if(z==player.z) { this.ctx.save(); this.ctx.translate(x, y); this.ctx.rotate(angle); @@ -62,73 +58,66 @@ class Render { } } - RenderBullet(x,y) { - /* - if(this.map==1) { - x = x-2000; - y = y-2000; - }*/ - this.ctx.save(); - this.ctx.translate(x, y); - this.ctx.drawImage(imgBullet, -20 / 2, -20 / 2, 20, 20); - this.ctx.restore(); + RenderBullet(x,y,z) { + if(z==player.z) + { + this.ctx.save(); + this.ctx.translate(x, y); + this.ctx.drawImage(imgBullet, -20 / 2, -20 / 2, 20, 20); + this.ctx.restore(); + } } - RenderPnj(x, y, angle, moving){ - - this.ctx.save(); - this.ctx.translate(x, y); - this.ctx.rotate(angle); - if((new Date)%1000>=500 || moving==false){ - this.ctx.drawImage(imgPnj, -30 / 2, -30 / 2, 30, 30); - }else{ - this.ctx.drawImage(imgPnj2, -30 / 2, -30 / 2, 30, 30); - } - - this.ctx.restore(); + RenderPnj(x, y, z, angle, moving) + { + if(z==player.z) + { + this.ctx.save(); + this.ctx.translate(x, y); + this.ctx.rotate(angle); + if(moving == false || (new Date().getMilliseconds())%1000>=500){ + this.ctx.drawImage(imgPnj, -30 / 2, -30 / 2, 30, 30); + }else{ + this.ctx.drawImage(imgPnj2, -30 / 2, -30 / 2, 30, 30); + } + this.ctx.restore(); + } } ReloadAff() { - - //const fond = new Image(); - if(player!=null && player.x >= 2000 && player.y >=2000) { - this.map=1; - } else { - this.map=0; + let background; + + if(player.z==0) + { + let date = new Date(); + if(date.getMinutes()%10>=5){ + background = map; + }else{ + background = map_night; + } + } + else + { + background=map2; } - let fond; - let date = new Date(); - - if(date.getMinutes()%10>=5){ - fond = map; - }else{ - fond = map_night; - } - /* - if(this.map==0) { - let fond = map; - } else { - let fond = map2; - } - */ - let mapWidth = fond.width; - let mapHeight = fond.height; + let mapWidth = background.width; + let mapHeight = background.height; this.ctx.canvas.width = mapWidth; this.ctx.canvas.height = mapHeight; - this.ctx.drawImage(fond, 0, 0, mapWidth, mapHeight); + this.ctx.drawImage(background, 0, 0, mapWidth, mapHeight); this.RenderPlayer(player,true); cars.forEach((car) => { - this.RenderCar(car.x,car.y,car.angle+(car.drift>0?2.1:0)); + this.RenderCar(car.x,car.y,car.z,car.angle+(car.drift>0?2.1:0)); }); players.forEach((player) => { this.RenderPlayer(player,false); }) bullets.forEach((bullet) => { - this.RenderBullet(bullet.x,bullet.y); + this.RenderBullet(bullet.x,bullet.y,bullet.z); }); PNJS.forEach((pnj)=>{ - this.RenderPnj(pnj.x, pnj.y, (pnj.dir-1)*Math.PI/4, pnj.dir!=0); + this.RenderPnj(pnj.x, pnj.y, pnj.z, (pnj.dir-1)*Math.PI/4, pnj.dir!=0); }) } } \ No newline at end of file