From f0817769a4b9137cbc3e7e8ade163da3c29bb955 Mon Sep 17 00:00:00 2001 From: Atsuyo-INSA Date: Fri, 8 Dec 2023 18:02:42 +0100 Subject: [PATCH] no more crash? --- public_html/game.html | 3 +- public_html/index.html | 2 +- public_html/js/class.js | 23 ++--- public_html/js/game.js | 177 +++++++-------------------------------- public_html/js/global.js | 4 +- public_html/js/render.js | 47 +++++------ 6 files changed, 69 insertions(+), 187 deletions(-) diff --git a/public_html/game.html b/public_html/game.html index f18497d..a858bf7 100644 --- a/public_html/game.html +++ b/public_html/game.html @@ -3,11 +3,12 @@ - + + diff --git a/public_html/index.html b/public_html/index.html index 4f58ba8..edb8748 100644 --- a/public_html/index.html +++ b/public_html/index.html @@ -40,4 +40,4 @@ window.addEventListener('keyup', (event) => { document.cookie = "pseudo=" + pseudo.value }) - \ No newline at end of file + diff --git a/public_html/js/class.js b/public_html/js/class.js index ca8f6a6..dea9e23 100644 --- a/public_html/js/class.js +++ b/public_html/js/class.js @@ -12,17 +12,18 @@ class Player this.name=name; this.x=x; this.y=y; + //this.z=z;//correspond to the map. Ex: 0=>main; 1=>arena this.id=id; this.visibleDir=1; - this.dir=dir;//0=standStill - //1=North - //2=North-East - //3=East - //4=South-East - //5=South - //6=South-West - //7=West - //8=North-West + this.dir=dir;//0=stand Still + //1=North + //2=North-East + //3=East + //4=South-East + //5=South + //6=South-West + //7=West + //8=North-West this.ammo=10; this.health=10; this.kill=0; @@ -69,7 +70,7 @@ class Player this.y += dy*dt; squares.forEach(square => { - if(square.collide(this)) + if(square.collide(this.x,this.y)) { this.x-=dx*dt; this.y-=dy*dt; @@ -79,7 +80,7 @@ class Player }); circles.forEach(circle => { - if(circle.collide(this)) + if(circle.collide(this.x,this.y)) { this.x-=dx*dt; this.y-=dy*dt; diff --git a/public_html/js/game.js b/public_html/js/game.js index 8f796e0..47a43d3 100644 --- a/public_html/js/game.js +++ b/public_html/js/game.js @@ -1,174 +1,55 @@ -let cars = [new Car(Renderer, 0, 0, sound), - new Car(Renderer, 0, 7, sound), - new Car(Renderer, 1, 7, sound), - new Car(Renderer, 1, 13, sound), - new Car(Renderer, 1, 14, sound), - new Car(Renderer, 0, 15, sound)]; +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 dt = 1; - -function update() -{ - if(player.y>470 && player.y<480 && player.x>237 && player.x<310) { - player.x=2510 - player.y=2714 - } else if (player.x>2588 && player.x<2687 && player.y>2129 && player.y<2180) { - player.x=270 - player.y=250 - } - let playerToUpdate = Net.getPlayersToUpdate(); - for (let i = 0;i { - console.log("New player: ",p.id); - players.push(p); - Renderer.AddPlayer(p); - }); -} - -function remPlayers() -{ - let playerToRemove = Net.getPlayersToRemove(); - for(let i=0;i { - bullets.push(b); - Renderer.addBullet(b); - }); - -} +let dt = 1.; function updateBullets(dt) { for(let i = bullets.length-1;i>=0;i--) { bullets[i].update(dt); - bullets[i].checkCollisions(player,squares,circles,Net); + bullets[i].checkCollisions(squares,circles); if(bullets[i].deleted) { - Renderer.remBullet(bullets[i]); bullets.splice(i,1); } } } -function updateKills() -{ - let deaths = Net.getDeathToAdd(); - deaths.forEach((object) => { - let dead = object.id; - let killer = object.killerId; - if(player.id==killer) - player.kill++; - for(let i=0;i { - Renderer.AddPlayer(p) - }); - players.push(new Player(-1,-50,-50,"VOITURE",0,null)); - LB.ReloadAff(players, player); - } - } - else - { - update(); - addPlayers(); - remPlayers(); - addBullets(); - updateKills(); - player.update(squares, circles, dt); - for (var i = players.length - 1; i >= 0; i--) { - players[i].update(squares, circles, dt); - } - Renderer.ReloadAff(); - cars.forEach((c) => { - c.Update(); - if(c.collide(player.x,player.y)) - { - Net.died(player.id,-1); - player.x=-50; - player.deaths++; - player.health=10; - } - }); - updateBullets(dt); - LB.ReloadAff(players, player); - } + if(net.playerId==null) + return; + player.update(squares, circles, dt); + for (let i = 0;i { + c.Update(); + if(c.collide(player.x,player.y)) + { + net.died(player.id,-1); + player.x=-50; + player.deaths++; + player.health=10; + } + }); + updateBullets(dt); + LB.ReloadAff(); + let newTime = new Date(); dt=newTime - currentTime; currentTime=newTime; } -Net.connect(); //connect to server, create a player, and retrieve all players info +net.connect(); //connect to server, create a player, and retrieve all players info setInterval(game); \ No newline at end of file diff --git a/public_html/js/global.js b/public_html/js/global.js index 9d3063d..baa91db 100644 --- a/public_html/js/global.js +++ b/public_html/js/global.js @@ -1,4 +1,4 @@ -function CookiePseudo() { +function CookiePseudo() {//Code from OpenClassroom nom = "pseudo="; var liste = document.cookie.split (';'); for (var i = 0; i < liste.length; i++) { @@ -19,6 +19,8 @@ let net = new Network("ws://129.151.227.50:8080?name="+CookiePseudo()); let inp = new Input("canvas"); let date = new Date(); let bullets = []; +let circles = []; +let squares = []; function updatePlayer(data) { diff --git a/public_html/js/render.js b/public_html/js/render.js index 8e182c3..e3d5218 100644 --- a/public_html/js/render.js +++ b/public_html/js/render.js @@ -11,13 +11,12 @@ map2.src = "./assets/map_secondaire.png"; class Render { constructor(idCanvas) { - this.canvas = document.getElementById(idCanvas); + let canvas = document.getElementById(idCanvas); this.ctx = canvas.getContext("2d"); - this.ReloadAff(); - this.map=0 + //this.ReloadAff(); } - RenderPlayer(player) { + RenderPlayer(player,client) { let x=player.x let y=player.y if(x>=2000 && y>=2000 && this.map==1) { @@ -34,8 +33,8 @@ class Render { this.ctx.restore(); this.ctx.fillStyle = 'white'; this.ctx.font="10pt arial"; - this.ctx.fillText(player.name,x-name.length*10/3,y-playerSize/1.8); - if(player.id==this.playerid) { + this.ctx.fillText(player.name,x-player.name.length*10/3,y-playerSize/1.8); + if(client) { this.ctx.fillStyle = 'red'; this.ctx.fillRect(x-playerSize/2-5, y-playerSize/2, playerSize+10, 5); this.ctx.fillStyle = '#7AFF33'; @@ -54,13 +53,12 @@ class Render { } } - RenderBullet(bullet) { - let x = bullet.x - let y = bullet.y + RenderBullet(x,y) { + /* if(this.map==1) { - x = x-2000 - y = y-2000 - } + x = x-2000; + y = y-2000; + }*/ this.ctx.save(); this.ctx.translate(x, y); this.ctx.drawImage(imgBullet, -20 / 2, -20 / 2, 20, 20); @@ -68,8 +66,8 @@ class Render { } ReloadAff() { - /* - const fond = new Image(); + + //const fond = new Image(); if(player.x >= 2000 && player.y >=2000) { this.map=1; } else { @@ -77,21 +75,20 @@ class Render { } if(this.map==0) { - fond.src = this.mapsrc; + let fond = map; } else { - fond.src = this.map2src; + let fond = map2; } - */ - mapWidth = fond.width; - mapHeith = fond.height; - this.ctx.canvas.width = mapWidth; - this.ctx.canvas.height = mapHeith; + + this.ctx.canvas.width = fond.width; + this.ctx.canvas.height = fond.height; this.ctx.drawImage(fond, 0, 0, mapWidth, mapHeith); - this.players.forEach((player) => { - this.RenderPlayer(player); + this.RenderPlayer(player,true); + players.forEach((player) => { + this.RenderPlayer(player,false); }) - this.bullets.forEach((bullet) => { - this.RenderBullet(bullet); + bullets.forEach((bullet) => { + this.RenderBullet(bullet.x,bullet.y); }); } } \ No newline at end of file