GrandTabernacleAutoVI/public_html/js/render.js

133 lines
3 KiB
JavaScript
Raw Normal View History

2023-12-06 09:50:34 +01:00
const imgPlayer = new Image();
const imgBullet = new Image();
2023-12-07 09:51:55 +01:00
const imgCar = new Image();
2023-12-06 09:50:34 +01:00
imgPlayer.src = "./assets/body.png";
2023-12-06 10:04:39 +01:00
imgBullet.src = "./assets/bullet.png";
2023-12-07 09:51:55 +01:00
imgCar.src = "./assets/car.png";
2023-11-15 09:09:13 +01:00
class Render {
2023-12-05 15:01:15 +01:00
2023-12-07 20:01:44 +01:00
constructor(id, mapsrc, map2src) {
2023-11-15 09:09:13 +01:00
this.canvas = document.getElementById(id);
this.ctx = canvas.getContext("2d");
this.players = [];
2023-12-06 09:50:34 +01:00
this.bullets = [];
this.mapsrc=mapsrc;
2023-12-07 22:31:50 +01:00
this.map2src=map2src;
2023-11-15 09:09:13 +01:00
this.ReloadAff();
2023-12-07 09:35:35 +01:00
this.playerid=0;
2023-12-07 22:31:50 +01:00
this.map=0
2023-12-07 09:35:35 +01:00
}
SetPlayerId(id) {
this.playerid=id
2023-11-15 09:09:13 +01:00
}
AddPlayer(player) {
2023-12-05 15:01:15 +01:00
this.players.push(player);
2023-11-15 09:09:13 +01:00
}
2023-12-06 09:50:34 +01:00
addBullet(bullet) {
this.bullets.push(bullet);
}
remBullet(bullet) {
2023-12-06 14:02:35 +01:00
for(let i=0;i<this.bullets.length;i++) {
if(bullet==this.bullets[i]) {
2023-12-06 09:50:34 +01:00
this.bullets.splice(i,1);
2023-12-06 14:02:35 +01:00
}
}
2023-12-06 09:50:34 +01:00
}
2023-11-29 15:53:35 +01:00
RemPlayer(id) {
2023-12-05 15:01:15 +01:00
for(let i=0;i<this.players.length;i++)
{
if(this.players[i].id==id)
{
this.players.splice(i,1);
break;
}
}
2023-11-29 15:53:35 +01:00
}
2023-11-15 09:09:13 +01:00
RenderPlayer(player) {
2023-12-07 22:31:50 +01:00
let x=player.x
let y=player.y
if(x>=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)) {
this.ctx.save();
this.ctx.translate(x, y);
this.ctx.rotate(player.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-name.length*10/3,y-playerSize/1.8);
if(player.id==this.playerid) {
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);
}
}
2023-12-06 09:50:34 +01:00
}
2023-12-07 16:29:34 +01:00
RenderCar(x,y,angle) {
2023-12-07 22:31:50 +01:00
if(this.map==0) {
this.ctx.save();
this.ctx.translate(x, y);
this.ctx.rotate(angle);
this.ctx.drawImage(imgCar, -carSize*1513/750 / 2, -carSize / 2, carSize*1513/750, carSize);
this.ctx.restore();
}
2023-12-07 09:51:55 +01:00
}
2023-12-06 09:50:34 +01:00
RenderBullet(bullet) {
2023-12-07 22:31:50 +01:00
let x = bullet.x
let y = bullet.y
if(this.map==1) {
x = x-2000
y = y-2000
}
2023-12-06 09:50:34 +01:00
this.ctx.save();
2023-12-07 22:31:50 +01:00
this.ctx.translate(x, y);
2023-12-07 13:14:08 +01:00
this.ctx.drawImage(imgBullet, -20 / 2, -20 / 2, 20, 20);
2023-11-15 09:09:13 +01:00
this.ctx.restore();
}
ReloadAff() {
const fond = new Image();
2023-12-07 22:31:50 +01:00
this.players.forEach((p) => {
if(p.id==this.playerid) {
//console.log(p)
if(p.x >= 2000 && p.y >=2000) {
this.map=1
} else {
this.map=0
}
}
})
if(this.map==0) {
fond.src = this.mapsrc;
} else {
fond.src = this.map2src;
}
2023-12-06 19:51:25 +01:00
mapWidth = fond.width;
mapHeith = fond.height;
this.ctx.canvas.width = mapWidth;
this.ctx.canvas.height = mapHeith;
2023-11-15 09:09:13 +01:00
this.ctx.drawImage(fond, 0, 0, mapWidth, mapHeith);
this.players.forEach((player) => {
this.RenderPlayer(player);
})
2023-12-06 09:50:34 +01:00
this.bullets.forEach((bullet) => {
this.RenderBullet(bullet);
});
2023-11-15 09:09:13 +01:00
}
}