diff --git a/public_html/assets/blue_portal.webp b/public_html/assets/blue_portal.webp new file mode 100644 index 0000000..d66d314 Binary files /dev/null and b/public_html/assets/blue_portal.webp differ diff --git a/public_html/assets/orange_portal.webp b/public_html/assets/orange_portal.webp new file mode 100644 index 0000000..dfdd842 Binary files /dev/null and b/public_html/assets/orange_portal.webp differ diff --git a/public_html/js/class.js b/public_html/js/class.js index 531895e..e88d46e 100644 --- a/public_html/js/class.js +++ b/public_html/js/class.js @@ -5,15 +5,17 @@ const PNJSpeed=.02; const bulletSpeed=playerSpeed*2; const halfSqrtTwo=0.70710678118; const defaulthealth=10; +const portalSize=40; +const affPortal = true; class Player { - constructor (id,x,y,name,dir) + constructor (id,x,y,z,name,dir) { this.name=name; this.x=x; this.y=y; - //this.z=z;//correspond to the map. Ex: 0=>main; 1=>arena + this.z=z;//correspond to the map. Ex: 0=>main; 1=>arena this.id=id; this.visibleDir=1; this.dir=dir;//0=stand Still @@ -72,7 +74,7 @@ class Player this.y += dy*dt; squares.forEach(square => { - if(square.collide(this.x,this.y)) + if(square.collide(this.x,this.y,this.z)) { this.x-=dx*dt; this.y-=dy*dt; @@ -82,7 +84,7 @@ class Player }); circles.forEach(circle => { - if(circle.collide(this.x,this.y)) + if(circle.collide(this.x,this.y,this.z)) { this.x-=dx*dt; this.y-=dy*dt; @@ -100,11 +102,13 @@ class Player class Bullet { - constructor (x,y,dx,dy,id) + constructor (x,y,z,dx,dy,id) { - bulletSound.play(); + if(z==player.z) + bulletSound.play(); this.x=x; this.y=y; + this.z=z; this.dx=dx; this.dy=dy; this.deleted=false; @@ -125,21 +129,21 @@ class Bullet if(this.deleted) return; - if(player!=null && player.id!=this.shooterId && Math.sqrt((player.x-this.x)**2+(player.y-this.y)**2) { - if(square.collide(this.x,this.y)) + if(square.collide(this.x,this.y,this.z)) { this.deleted=true; return; } }); circles.forEach((circle) => { - if(circle.collide(this.x,this.y)) + if(circle.collide(this.x,this.y,this.z)) { this.deleted=true; return; @@ -151,32 +155,34 @@ class Bullet class Square { - constructor(x,y,width,heigth) + constructor(x,y,z,width,heigth) { this.x=x; this.y=y; + this.z=z; this.w=width; this.h=heigth; } - collide(x,y) + collide(x,y,z) { - return this.x<=x && x<=this.x+this.w && this.y<=y && y<=this.y+this.h; + return this.z==z && this.x<=x && x<=this.x+this.w && this.y<=y && y<=this.y+this.h; } } class Circle { - constructor(x,y,radius) + constructor(x,y,z,radius) { this.x=x; this.y=y; + this.z=z; this.r=radius; } - collide(x,y) + collide(x,y,z) { - return (x-this.x)**2+(y-this.y)**2 <= this.r**2; + return this.z==z && (x-this.x)**2+(y-this.y)**2 <= this.r**2; } } @@ -186,7 +192,7 @@ class Car { this.type=type; // 0 circule vers le haut // 1 circule vers le bas - + this.z=0; if(this.type == 1) { //vers le bas this.x=1247; this.y=-40; @@ -206,8 +212,11 @@ class Car this.tick=0; } - collide(x,y) + collide(x,y,z) { + if(this.z!=z) { + return false; + } let cx=this.x-carSize/2; let cy=this.y-carSize/2; let collide = (cx<=x && x<=cx+carSize && cy<=y && y<=cy+carSize); @@ -372,41 +381,42 @@ class Car } class PNJ{ - constructor(x, y){ + constructor(x, y, z){ this.x=x; this.y=y; + this.z=z; this.dir=1; } - checkCollisions(x,y){ - let colliding = false; + checkCollisions(){ squares.forEach((square) => { - if(square.collide(x,y)) + if(square.collide(this.x,this.y,this.z)) { - colliding = true; - return; + return true; } }); circles.forEach((circle) => { - if(circle.collide(x,y)) + if(circle.collide(this.x,this.y,this.z)) { - colliding=true; - return; + return true; } }); - return colliding; + return false; } changeDirection(){ - let newDir = this.dir; - while(newDir == this.dir){ - newDir = Math.floor(Math.random()*8); + let newDir = Math.floor(Math.random()*8); + if(newDir == this.dir){ + newDir = (this.dir+3)%4+1; } this.dir = newDir; } update(dt) { + if(this.z!=player.z){ + return; + } let dx,dy; switch(this.dir) { @@ -420,10 +430,11 @@ class PNJ{ case 8: dx=-halfSqrtTwo*PNJSpeed;dy=-halfSqrtTwo*PNJSpeed;break; default: dx=0;dy=0;break; } - if(!this.checkCollisions(this.x + dx*dt, this.y + dy*dt)){ - this.x += dx*dt; - this.y += dy*dt; - }else{ + this.x+=dx*dt; + this.y+=dy*dt; + if(this.checkCollisions()){ + this.x -= dx*dt; + this.y -= dy*dt; this.changeDirection(); } @@ -432,4 +443,39 @@ class PNJ{ } } +} + +class Portal +{ + constructor(xIn,yIn,zIn,xOut,yOut,zOut) + { + this.in={x:xIn,y:yIn,z:zIn}; + this.out={x:xOut,y:yOut,z:zOut}; + } + + update() + { + if(player.z==this.in.z && player.x>this.in.x && player.xthis.in.y && player.y {if(p.in.z!=z){console.log("WRONG PORTAL DEFINITION FOR MAP",z,", portal :",p);}}); + this.z=z; + } + + update() + { + this.portalsOut.forEach((p)=>p.update()); + } } \ No newline at end of file diff --git a/public_html/js/game.js b/public_html/js/game.js index 3e57bf3..b871655 100644 --- a/public_html/js/game.js +++ b/public_html/js/game.js @@ -10,12 +10,13 @@ function game() { } cars.forEach((c) => { - c.Update(dt); - if(c.collide(player.x,player.y)) + c.Update(); + if(c.collide(player.x,player.y,player.z)) { net.died(player.id,-1); - player.x=-50; + player.z=-1; player.deaths++; + players[0].kill++; player.health=10; } }); @@ -24,6 +25,8 @@ function game() { pnj.update(dt); }) + maps.forEach((m) => {m.update();}); + updateBullets(dt); renderer.ReloadAff(); LB.ReloadAff(); @@ -36,4 +39,4 @@ function game() { net.connect(); //connect to server, create a player, and retrieve all players info -setInterval(game, 16); \ No newline at end of file +setInterval(game, 16); diff --git a/public_html/js/global.js b/public_html/js/global.js index 011c89d..b3ae65f 100644 --- a/public_html/js/global.js +++ b/public_html/js/global.js @@ -10,17 +10,30 @@ let phone = new Phone(); 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(0, 0), - new Car(0, 7), - new Car(1, 3), + new Car(1, 7), new Car(1, 13), new Car(1, 14), + new Car(0, 7), new Car(0, 15)]; +let portals = [new Portal(250,457,0,500,500,0),//O + new Portal(344,758,0,500,500,0), // SO + new Portal(1190,211,0,500,500,0),// NE + new Portal(862,213,0,500,500,0), // N + new Portal(1126,472,0,500,500,0),// E + new Portal(1076,768,0,500,500,0),// SE + new Portal(721,767,0,500,500,0), // S + new Portal(970,476,0,500,500,0)];//Mid + +let maps = [new Map(portals.slice(0),0)]; + +players.push(new Player(-1,-50,-50,-1,"VOITURES",0)); function updatePlayer(data) { @@ -28,6 +41,7 @@ function updatePlayer(data) { player.x=data.x; player.y=data.y; + player.z=data.z; } else { @@ -37,6 +51,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,7 +64,7 @@ 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); } diff --git a/public_html/js/input.js b/public_html/js/input.js index 6dd538f..e9528a8 100644 --- a/public_html/js/input.js +++ b/public_html/js/input.js @@ -14,26 +14,21 @@ class Input { let mouseX = (e.clientX - bounds.x)*this.canvas.width/bounds.width; let mouseY = (e.clientY - bounds.y)*this.canvas.height/bounds.height; - - if(player.x>=2000 && player.y>=2000) { - mouseX+=2000; - mouseY+=2000; - } + //console.log(Math.round(mouseX),",",Math.round(mouseY)); //for debug 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.shooterId); + net.newBullet(b.x,b.y,b.z,b.dx,b.dy,b.shooterId); }); window.addEventListener("keydown", (e)=>{ //blocks the action of the key (cf. Killian) - if(["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()) @@ -102,4 +97,4 @@ class Input { return Math.atan2(this.mouseY - playerY, this.mouseX - playerX); } */ -} \ No newline at end of file +} diff --git a/public_html/js/network.js b/public_html/js/network.js index 3b7e60e..ae4ff3f 100644 --- a/public_html/js/network.js +++ b/public_html/js/network.js @@ -13,9 +13,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,61 +63,65 @@ 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); - } + 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(); + } + } - this.ctx.restore(); + RenderPortal(x,y,orange) + { + this.ctx.save(); + this.ctx.translate(x,y); + if(orange) + this.ctx.drawImage(orange_portal,0,0,portalSize,portalSize); + else + this.ctx.drawImage(blue_portal,0,0,portalSize,portalSize); + 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=mapImages[player.z]; } - 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) => { @@ -126,10 +131,19 @@ class Render { 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); }) + if(affPortal) + { + portals.forEach((portal) => { + if(portal.in.z==player.z) + this.RenderPortal(portal.in.x,portal.in.y,true); + if(portal.out.z==player.z) + this.RenderPortal(portal.out.x,portal.out.y,false); + }); + } } -} \ No newline at end of file +}