succesfull merge

This commit is contained in:
Atsuyo-INSA 2023-12-18 13:22:20 +01:00
commit 6b0e00d1f9
9 changed files with 208 additions and 134 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 432 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 504 B

View file

@ -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)
{
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)<playerSize/2)
if(player!=null && player.z==this.z && player.id!=this.shooterId && Math.sqrt((player.x-this.x)**2+(player.y-this.y)**2)<playerSize/2)
{
player.takeDamage(1,this.shooterId);
this.deleted=true;
return;
}
squares.forEach((square) => {
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();
}
@ -433,3 +444,38 @@ 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.x<this.in.x+portalSize && player.y>this.in.y && player.y<this.in.y+portalSize)
{
player.z=this.out.z;
player.x=this.out.x;
player.y=this.out.y;
net.update(player);
}
}
}
class Map
{
constructor(portalsOut,z) // portalsIn/Out : portal teleport In/Out; z: idDimension
{
this.portalsOut=portalsOut;
portalsOut.forEach((p) => {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());
}
}

View file

@ -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();

View file

@ -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);
}

View file

@ -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())

View file

@ -13,9 +13,9 @@ class Network{
for (let i = 0; i<data.data.players.length; i++) {
let p = data.data.players[i];
if(p.id==this.playerId)
player=new Player(p.id,p.x,p.y,p.name,p.dir);
player=new Player(p.id,p.x,p.y,0,p.name,p.dir);
else
players.push(new Player(p.id,p.x,p.y,p.name,p.dir));
players.push(new Player(p.id,p.x,p.y,0,p.name,p.dir));
}
break;
@ -24,7 +24,7 @@ class Network{
break;
case "newplayer":
players.push(new Player(data.data.id, data.data.x, data.data.y, data.data.name, data.data.dir));
players.push(new Player(data.data.id, data.data.x, data.data.y, 0, data.data.name, data.data.dir));
break;
case "removePlayer":
@ -32,7 +32,7 @@ class Network{
break;
case "newBullet":
bullets.push(new Bullet(data.data.x,data.data.y,data.data.dx,data.data.dy,data.data.id, this.sound));
bullets.push(new Bullet(data.data.x,data.data.y,data.data.z==undefined?0:data.data.z,data.data.dx,data.data.dy,data.data.id));
break;
case "died":
@ -70,10 +70,9 @@ class Network{
}));
}
newBullet(x,y,dx,dy,parentId)
newBullet(x,y,z,dx,dy,parentId)
{
console.log(parentId)
this.socket.send(JSON.stringify({type:"newBullet",data:{x: x,y: y,dx: dx,dy: dy,id:parentId}}));
this.socket.send(JSON.stringify({type:"newBullet",data:{x: x,y: y,z: z,dx: dx,dy: dy,id:parentId}}));
}
sendMessage(title, content){

View file

@ -1,11 +1,11 @@
let objects = {"squares":[{"x":1162,"y":115,"w":144,"h":125},{"x":120,"y":906,"w":1228,"h":21},{"x":127,"y":0,"w":1225,"h":14},{"x":297,"y":114,"w":78,"h":93},{"x":169,"y":243,"w":62,"h":250},{"x":313,"y":243,"w":62,"h":253},{"x":228,"y":257,"w":96,"h":222},{"x":792,"y":113,"w":176,"h":126},{"x":1017,"y":113,"w":143,"h":81},{"x":1160,"y":241,"w":143,"h":-129},{"x":1065,"y":372,"w":127,"h":124},{"x":697,"y":372,"w":319,"h":126},{"x":697,"y":498,"w":159,"h":111},{"x":697,"y":628,"w":160,"h":158},{"x":1001,"y":628,"w":191,"h":158},{"x":281,"y":629,"w":175,"h":156},{"x":0,"y":0,"w":137,"h":616},{"x":0,"y":615,"w":136,"h":310},{"x":1337,"y":1,"w":130,"h":581},{"x":1337,"y":573,"w":131,"h":356},{"x":586,"y":153,"w":15,"h":24},{"x":651,"y":153,"w":12,"h":24},{"x":700,"y":187,"w":12,"h":22},{"x":1020,"y":194,"w":10,"h":47},{"x":1019,"y":227,"w":56,"h":14},{"x":1101,"y":227,"w":62,"h":13},{"x":654,"y":128,"w":105,"h":17},{"x":748,"y":129,"w":12,"h":49},{"x":748,"y":193,"w":12,"h":47},{"x":654,"y":223,"w":105,"h":16},{"x":521,"y":129,"w":106,"h":14},{"x":521,"y":140,"w":13,"h":38},{"x":521,"y":194,"w":13,"h":46},{"x":521,"y":225,"w":108,"h":16}],"circles":[{"x":552,"y":163,"r":13.601470508735444},{"x":608,"y":190,"r":20.248456731316587},{"x":569,"y":212,"r":11.704699910719626},{"x":680,"y":213,"r":12.041594578792296},{"x":727,"y":164,"r":14.212670403551895}]}
let objects = {"squares":[{"x":1162,"y":115,"z":0,"w":144,"h":125},{"x":120,"y":906,"z":0,"w":1228,"h":21},{"x":127,"y":0,"z":0,"w":1225,"h":14},{"x":297,"y":114,"z":0,"w":78,"h":93},{"x":169,"y":243,"z":0,"w":62,"h":250},{"x":313,"y":243,"z":0,"w":62,"h":253},{"x":228,"y":257,"z":0,"w":96,"h":222},{"x":792,"y":113,"z":0,"w":176,"h":126},{"x":1017,"y":113,"z":0,"w":143,"h":81},{"x":1160,"y":241,"z":0,"w":143,"h":-129},{"x":1065,"y":372,"z":0,"w":127,"h":124},{"x":697,"y":372,"z":0,"w":319,"h":126},{"x":697,"y":498,"z":0,"w":159,"h":111},{"x":697,"y":628,"z":0,"w":160,"h":158},{"x":1001,"y":628,"z":0,"w":191,"h":158},{"x":281,"y":629,"z":0,"w":175,"h":156},{"x":0,"y":0,"z":0,"w":137,"h":616},{"x":0,"y":615,"z":0,"w":136,"h":310},{"x":1337,"y":1,"z":0,"w":130,"h":581},{"x":1337,"y":573,"z":0,"w":131,"h":356},{"x":586,"y":153,"z":0,"w":15,"h":24},{"x":651,"y":153,"z":0,"w":12,"h":24},{"x":700,"y":187,"z":0,"w":12,"h":22},{"x":1020,"y":194,"z":0,"w":10,"h":47},{"x":1019,"y":227,"z":0,"w":56,"h":14},{"x":1101,"y":227,"z":0,"w":62,"h":13},{"x":654,"y":128,"z":0,"w":105,"h":17},{"x":748,"y":129,"z":0,"w":12,"h":49},{"x":748,"y":193,"z":0,"w":12,"h":47},{"x":654,"y":223,"z":0,"w":105,"h":16},{"x":521,"y":129,"z":0,"w":106,"h":14},{"x":521,"y":140,"z":0,"w":13,"h":38},{"x":521,"y":194,"z":0,"w":13,"h":46},{"x":521,"y":225,"z":0,"w":108,"h":16}],"circles":[{"x":552,"y":163,"z":0,"r":13.601470508735444},{"x":608,"y":190,"z":0,"r":20.248456731316587},{"x":569,"y":212,"z":0,"r":11.704699910719626},{"x":680,"y":213,"z":0,"r":12.041594578792296},{"x":727,"y":164,"z":0,"r":14.212670403551895}]}
for (let i=0; i<objects.squares.length;i++) {
let current = objects.squares[i]
squares.push(new Square(current.x, current.y, current.w, current.h));
squares.push(new Square(current.x, current.y, current.z, current.w, current.h));
}
for (let i=0; i<objects.circles.length; i++) {
let current = objects.circles[i]
circles.push(new Circle(current.x, current.y, current.r));
circles.push(new Circle(current.x, current.y, current.z, current.r));
}

View file

@ -6,6 +6,8 @@ const imgPnj2 = new Image();
const map = new Image();
const map_night = new Image();
const map2 = new Image();
const orange_portal = new Image();
const blue_portal = new Image();
imgPlayer.src = "./assets/body.png";
imgBullet.src = "./assets/bullet.png";
imgCar.src = "./assets/car.png";
@ -14,46 +16,45 @@ imgPnj2.src = "./assets/pnj1.png";
map.src = "./assets/map/map_principale.png"
map_night.src = "./assets/map/map_principale_nuit.png"
map2.src = "./assets/map/map_secondaire.png";
orange_portal.src = "./assets/orange_portal.webp";
blue_portal.src = "./assets/blue_portal.webp";
const mapImages = [map,map2];
class Render {
constructor(idCanvas) {
let canvas = document.getElementById(idCanvas);
this.ctx = canvas.getContext("2d");
this.ctx.imageSmoothingEnabled=false;
//this.ReloadAff();
this.ctx.imageSmoothingEnabled=false;//does not lerp pixels
}
RenderPlayer(player,client) {
if(player==null)
RenderPlayer(p,isClient) {
if(p==null)
return;
let x=player.x
let y=player.y
if(x>=2000 && y>=2000 && this.map==1) {
x=player.x-2000
y=player.y-2000
}
let x=p.x
let y=p.y
//this.map==1 && (x<2000 || y<2000)
if((this.map==1 && player.x>=2000 && player.y>=2000) || (this.map==0 && x<2000 && y <2000)) {
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;
}*/
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){
RenderPnj(x, y, z, angle, moving)
{
if(z==player.z)
{
this.ctx.save();
this.ctx.translate(x, y);
this.ctx.rotate(angle);
if((new Date)%1000>=500 || moving==false){
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();
}
}
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() {
let background;
//const fond = new Image();
if(player!=null && player.x >= 2000 && player.y >=2000) {
this.map=1;
} else {
this.map=0;
}
let fond;
if(player.z<=0)
{
let date = new Date();
if(date.getMinutes()%10>=5){
fond = map;
background = map;
}else{
fond = map_night;
background = map_night;
}
/*
if(this.map==0) {
let fond = map;
} else {
let fond = map2;
}
*/
let mapWidth = fond.width;
let mapHeight = fond.height;
else
{
background=mapImages[player.z];
}
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);
});
}
}
}