Compare commits

...

2 commits

Author SHA1 Message Date
Baptiste
87d078a34d correction son en parametre => plus propre 2023-12-07 18:53:44 +01:00
Baptiste
97fac63998 ajout du drift 2023-12-07 18:53:34 +01:00
6 changed files with 41 additions and 19 deletions

BIN
assets/sounds/drift.mp3 Normal file

Binary file not shown.

View file

@ -7,9 +7,6 @@ const bulletSpeed=playerSpeed*2;
const halfSqrtTwo=0.70710678118; const halfSqrtTwo=0.70710678118;
const defaulthealth=10.; const defaulthealth=10.;
let sound = new Sound("./assets/sounds/");
sound.loadSounds();
class Player class Player
{ {
constructor (id,x,y,name, dir) constructor (id,x,y,name, dir)
@ -114,7 +111,7 @@ class Player
class Bullet class Bullet
{ {
constructor (x,y,dx,dy,id) constructor (x,y,dx,dy,id, sound)
{ {
sound.shoot(); sound.shoot();
this.x=x; this.x=x;
@ -198,8 +195,9 @@ class Circle
class Car class Car
{ {
constructor(Renderer, type, spawn) constructor(Renderer, type, spawn, sound)
{ {
this.sound=sound
this.type=type // 0 circule vers le haut this.type=type // 0 circule vers le haut
// 1 circule vers le bas // 1 circule vers le bas
@ -215,6 +213,8 @@ class Car
this.angle=-Math.PI/2; this.angle=-Math.PI/2;
} }
this.drift=0;
this.spawn=spawn this.spawn=spawn
this.tick=0; this.tick=0;
@ -227,7 +227,12 @@ class Car
{ {
let cx=this.x-carSize/2; let cx=this.x-carSize/2;
let cy=this.y-carSize/2; let cy=this.y-carSize/2;
return (cx<=x && x<=cx+carSize && cy<=y && y<=cy+carSize); let collide = (cx<=x && x<=cx+carSize && cy<=y && y<=cy+carSize);
if(collide) {
this.sound.drift()
this.drift=300;
}
return collide
} }
Update() Update()
@ -252,7 +257,12 @@ class Car
this.y=this.y+this.tick this.y=this.y+this.tick
break; break;
} }
this.Renderer.RenderCar(this.x,this.y, this.angle) if(this.drift > 0) {
this.Renderer.RenderCar(this.x,this.y, this.angle+90)
this.drift--
} else {
this.Renderer.RenderCar(this.x,this.y, this.angle)
}
} }
pseudoaleatoire() { pseudoaleatoire() {

View file

@ -11,13 +11,15 @@ function CookiePseudo() {
Renderer = new Render("canvas", "./assets/map/map7_recadr.png"); Renderer = new Render("canvas", "./assets/map/map7_recadr.png");
LB = new LeaderBoard("canvas"); LB = new LeaderBoard("canvas");
cars = [new Car(Renderer, 0, 0), let sound = new Sound("./assets/sounds/");
new Car(Renderer, 0, 7), sound.loadSounds();
new Car(Renderer, 1, 7), cars = [new Car(Renderer, 0, 0, sound),
new Car(Renderer, 1, 13), new Car(Renderer, 0, 7, sound),
new Car(Renderer, 1, 14), new Car(Renderer, 1, 7, sound),
new Car(Renderer, 0, 15)] new Car(Renderer, 1, 13, sound),
let Net = new Network("ws://129.151.227.50:8080?name="+CookiePseudo(), Renderer); new Car(Renderer, 1, 14, sound),
new Car(Renderer, 0, 15, sound)]
let Net = new Network("ws://129.151.227.50:8080?name="+CookiePseudo(), sound);
let playerId = null; //id of client player let playerId = null; //id of client player
@ -25,7 +27,7 @@ let players = [];
let bullets = []; let bullets = [];
let player = null; let player = null;
Inp = new Input("canvas", Net,Renderer); Inp = new Input("canvas", Net,Renderer, sound);
playerId=Net.playerId; playerId=Net.playerId;
player=Net.clientPlayer; player=Net.clientPlayer;

View file

@ -1,5 +1,5 @@
class Input { class Input {
constructor(id, net,renderer) { constructor(id, net,renderer, sound) {
this.keysDown = new Set() this.keysDown = new Set()
this.dir = 0; this.dir = 0;
this.player=null; this.player=null;
@ -23,7 +23,7 @@ class Input {
let dx = this.mouseX-this.player.x; let dx = this.mouseX-this.player.x;
let dy = this.mouseY-this.player.y; let dy = this.mouseY-this.player.y;
let norm = Math.sqrt(dx*dx+dy*dy); let norm = Math.sqrt(dx*dx+dy*dy);
let b = new Bullet(this.player.x,this.player.y,dx/norm,dy/norm,this.player.id); let b = new Bullet(this.player.x,this.player.y,dx/norm,dy/norm,this.player.id, sound);
this.bullets.push(b); this.bullets.push(b);
this.renderer.addBullet(b); this.renderer.addBullet(b);

View file

@ -1,5 +1,5 @@
class Network{ class Network{
constructor(adress){ constructor(adress, bulletsound){
this.adress = adress; this.adress = adress;
this.connected = false; this.connected = false;
@ -10,6 +10,7 @@ class Network{
this.playersToUpdate = []; this.playersToUpdate = [];
this.bulletsToAdd = []; this.bulletsToAdd = [];
this.deathToAdd = []; this.deathToAdd = [];
this.sound=bulletsound;
} }
message(data){ message(data){
@ -36,7 +37,8 @@ class Network{
this.playersToRemove.push(data.data.id); this.playersToRemove.push(data.data.id);
break; break;
case "newBullet": case "newBullet":
this.bulletsToAdd.push(new Bullet(data.data.x,data.data.y,data.data.dx,data.data.dy,data.data.id)); console.log(this.sound)
this.bulletsToAdd.push(new Bullet(data.data.x,data.data.y,data.data.dx,data.data.dy,data.data.id, this.sound));
break; break;
case "died": case "died":
console.log("player",data.data.id,"was killed by",data.data.killerId); console.log("player",data.data.id,"was killed by",data.data.killerId);

View file

@ -7,6 +7,8 @@ class Sound{
console.log(this.assetsUrl + "shoot.mp3") console.log(this.assetsUrl + "shoot.mp3")
this.shootSound = new Audio(this.assetsUrl + "shoot.mp3"); this.shootSound = new Audio(this.assetsUrl + "shoot.mp3");
this.shootSound.type = "audio/mp3"; this.shootSound.type = "audio/mp3";
this.driftsound = new Audio(this.assetsUrl + "drift.mp3");
this.driftsound.type = "audio/mp3";
} }
shoot(){ shoot(){
@ -14,4 +16,10 @@ class Sound{
this.shootSound.currentTime=0; this.shootSound.currentTime=0;
this.shootSound.play() this.shootSound.play()
} }
drift(){
this.driftsound.pause()
this.driftsound.currentTime=0;
this.driftsound.play()
}
} }