GrandTabernacleAutoVI/js/render.js

79 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-12-06 09:50:34 +01:00
const imgPlayer = new Image();
const imgBullet = new Image();
imgPlayer.src = "./assets/body.png";
2023-12-06 10:04:39 +01:00
imgBullet.src = "./assets/bullet.png";
2023-11-15 09:09:13 +01:00
class Render {
2023-12-05 15:01:15 +01:00
2023-11-15 09:09:13 +01:00
constructor(id, mapsrc) {
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-11-15 09:09:13 +01:00
this.ReloadAff();
}
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) {
this.ctx.save();
this.ctx.translate(player.x, player.y);
this.ctx.rotate(player.angle);
2023-12-06 09:50:34 +01:00
this.ctx.drawImage(imgPlayer, -playerSize / 2, -playerSize / 2, playerSize, playerSize);
this.ctx.restore();
2023-12-06 10:14:15 +01:00
this.ctx.fillStyle = 'white';
this.ctx.font="10pt arial";
this.ctx.fillText(player.name,player.x-player.name.length*10/3,player.y-playerSize/1.8);
2023-12-06 09:50:34 +01:00
}
RenderBullet(bullet) {
this.ctx.save();
this.ctx.translate(bullet.x, bullet.y);
this.ctx.drawImage(imgBullet, -playerSize / 2, -playerSize / 2, playerSize, playerSize);
2023-11-15 09:09:13 +01:00
this.ctx.restore();
}
ReloadAff() {
/*this.ctx.fillStyle = "red";
this.ctx.fillRect(0,0,this.canvas.width,this.canvas.height);
*/
const fond = new Image();
fond.src = this.mapsrc;
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
}
}