Compare commits
2 commits
a4c8e32a26
...
87d078a34d
Author | SHA1 | Date | |
---|---|---|---|
|
87d078a34d | ||
|
97fac63998 |
6 changed files with 41 additions and 19 deletions
BIN
assets/sounds/drift.mp3
Normal file
BIN
assets/sounds/drift.mp3
Normal file
Binary file not shown.
22
js/class.js
22
js/class.js
|
@ -7,9 +7,6 @@ const bulletSpeed=playerSpeed*2;
|
|||
const halfSqrtTwo=0.70710678118;
|
||||
const defaulthealth=10.;
|
||||
|
||||
let sound = new Sound("./assets/sounds/");
|
||||
sound.loadSounds();
|
||||
|
||||
class Player
|
||||
{
|
||||
constructor (id,x,y,name, dir)
|
||||
|
@ -114,7 +111,7 @@ class Player
|
|||
|
||||
class Bullet
|
||||
{
|
||||
constructor (x,y,dx,dy,id)
|
||||
constructor (x,y,dx,dy,id, sound)
|
||||
{
|
||||
sound.shoot();
|
||||
this.x=x;
|
||||
|
@ -198,8 +195,9 @@ class Circle
|
|||
|
||||
class Car
|
||||
{
|
||||
constructor(Renderer, type, spawn)
|
||||
constructor(Renderer, type, spawn, sound)
|
||||
{
|
||||
this.sound=sound
|
||||
this.type=type // 0 circule vers le haut
|
||||
// 1 circule vers le bas
|
||||
|
||||
|
@ -215,6 +213,8 @@ class Car
|
|||
this.angle=-Math.PI/2;
|
||||
}
|
||||
|
||||
this.drift=0;
|
||||
|
||||
this.spawn=spawn
|
||||
|
||||
this.tick=0;
|
||||
|
@ -227,7 +227,12 @@ class Car
|
|||
{
|
||||
let cx=this.x-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()
|
||||
|
@ -252,8 +257,13 @@ class Car
|
|||
this.y=this.y+this.tick
|
||||
break;
|
||||
}
|
||||
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() {
|
||||
return Math.floor(Date.now()/1000)%10
|
||||
|
|
18
js/game.js
18
js/game.js
|
@ -11,13 +11,15 @@ function CookiePseudo() {
|
|||
|
||||
Renderer = new Render("canvas", "./assets/map/map7_recadr.png");
|
||||
LB = new LeaderBoard("canvas");
|
||||
cars = [new Car(Renderer, 0, 0),
|
||||
new Car(Renderer, 0, 7),
|
||||
new Car(Renderer, 1, 7),
|
||||
new Car(Renderer, 1, 13),
|
||||
new Car(Renderer, 1, 14),
|
||||
new Car(Renderer, 0, 15)]
|
||||
let Net = new Network("ws://129.151.227.50:8080?name="+CookiePseudo(), Renderer);
|
||||
let sound = new Sound("./assets/sounds/");
|
||||
sound.loadSounds();
|
||||
cars = [new Car(Renderer, 0, 0, sound),
|
||||
new Car(Renderer, 0, 7, sound),
|
||||
new Car(Renderer, 1, 7, sound),
|
||||
new Car(Renderer, 1, 13, sound),
|
||||
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
|
||||
|
@ -25,7 +27,7 @@ let players = [];
|
|||
let bullets = [];
|
||||
let player = null;
|
||||
|
||||
Inp = new Input("canvas", Net,Renderer);
|
||||
Inp = new Input("canvas", Net,Renderer, sound);
|
||||
|
||||
playerId=Net.playerId;
|
||||
player=Net.clientPlayer;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
class Input {
|
||||
constructor(id, net,renderer) {
|
||||
constructor(id, net,renderer, sound) {
|
||||
this.keysDown = new Set()
|
||||
this.dir = 0;
|
||||
this.player=null;
|
||||
|
@ -23,7 +23,7 @@ class Input {
|
|||
let dx = this.mouseX-this.player.x;
|
||||
let dy = this.mouseY-this.player.y;
|
||||
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.renderer.addBullet(b);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
class Network{
|
||||
constructor(adress){
|
||||
constructor(adress, bulletsound){
|
||||
this.adress = adress;
|
||||
this.connected = false;
|
||||
|
||||
|
@ -10,6 +10,7 @@ class Network{
|
|||
this.playersToUpdate = [];
|
||||
this.bulletsToAdd = [];
|
||||
this.deathToAdd = [];
|
||||
this.sound=bulletsound;
|
||||
}
|
||||
|
||||
message(data){
|
||||
|
@ -36,7 +37,8 @@ class Network{
|
|||
this.playersToRemove.push(data.data.id);
|
||||
break;
|
||||
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;
|
||||
case "died":
|
||||
console.log("player",data.data.id,"was killed by",data.data.killerId);
|
||||
|
|
|
@ -7,6 +7,8 @@ class Sound{
|
|||
console.log(this.assetsUrl + "shoot.mp3")
|
||||
this.shootSound = new Audio(this.assetsUrl + "shoot.mp3");
|
||||
this.shootSound.type = "audio/mp3";
|
||||
this.driftsound = new Audio(this.assetsUrl + "drift.mp3");
|
||||
this.driftsound.type = "audio/mp3";
|
||||
}
|
||||
|
||||
shoot(){
|
||||
|
@ -14,4 +16,10 @@ class Sound{
|
|||
this.shootSound.currentTime=0;
|
||||
this.shootSound.play()
|
||||
}
|
||||
|
||||
drift(){
|
||||
this.driftsound.pause()
|
||||
this.driftsound.currentTime=0;
|
||||
this.driftsound.play()
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue