diff --git a/client/src/avatar.js b/client/src/avatar.js index e33fb3643ff2bf59d57f64410625a2b2f21a8a48..938ae232d96e0e32e5b0542e317ef07e044f5244 100644 --- a/client/src/avatar.js +++ b/client/src/avatar.js @@ -23,10 +23,10 @@ export class Avatar { this.x = 0; this.vies = 3; this.click = []; - this.vitesse = 5; + this.vitesse = 1; this.projectiles = []; this.hitbox = new Hitbox(68, 145, this.x, this.y); - this.inertie = 0.95; + this.inertie = 0.9; this.momentumX = 0; this.momentumY = 0; this.canvasSize = canvasSize; @@ -104,29 +104,51 @@ export class Avatar { deplacer() { if (this.click[37]) { - //if (this.x > 0) { - this.x -= this.vitesse; - this.hitbox.x -= this.vitesse; - //} + if (this.x > 0) { + this.x -= this.vitesse; + this.hitbox.x -= this.vitesse; + this.momentumX -= this.vitesse; + } } if (this.click[39]) { - //if (this.x < this.canvas.width - this.image.width) { - this.x += this.vitesse; - this.hitbox.x += this.vitesse; - //} + if (this.x < this.canvasSize.width - 68) { + this.x += this.vitesse; + this.hitbox.x += this.vitesse; + this.momentumX += this.vitesse; + } } if (this.click[38]) { - //if (this.y > 0) { - this.y -= this.vitesse; - this.hitbox.y -= this.vitesse; - //} + if (this.y > 0) { + this.y -= this.vitesse; + this.hitbox.y -= this.vitesse; + this.momentumY -= this.vitesse; + } } if (this.click[40]) { - //if (this.y < this.canvas.height - this.image.height) { - this.y += this.vitesse; - this.hitbox.y += this.vitesse; - // } + if (this.y < this.canvasSize.height - 145) { + this.y += this.vitesse; + this.hitbox.y += this.vitesse; + this.momentumY += this.vitesse; + } } + if ( + (this.x >= 0 && this.momentumX <= 0) || + (this.x <= this.canvasSize.width - 68 && this.momentumX >= 0) + ) { + this.x += this.momentumX; + this.hitbox.x = this.x; + } + + if ( + (this.y >= 0 && this.momentumY <= 0) || + (this.y <= this.canvasSize.height - 145 && this.momentumY >= 0) + ) { + this.y += this.momentumY; + this.hitbox.y = this.y; + } + + this.momentumX *= this.inertie; + this.momentumY *= this.inertie; } perdreVie() { diff --git a/server/index.js b/server/index.js index 8507a2d8092890bde1c0f7f318913c2de8824161..a6eeb67c38a0a5fe93ac758840fb879b99662fea 100644 --- a/server/index.js +++ b/server/index.js @@ -9,14 +9,13 @@ import timer from './timer.js'; const app = express(); -let canvasSize=new Coordinate(1920,1261); +let canvasSize = new Coordinate(1920, 1261); let canLostLifeAvatar = true; let canLostLifeEnemi = true; let t = new timer(); - const httpServer = http.createServer(app); const fileOptions = { root: process.cwd() }; addWebpackMiddleware(app); @@ -36,13 +35,11 @@ httpServer.listen(port, () => { console.log(`Server running at http://localhost:${port}/`); }); - setInterval(function () { t.addTime(); - io.emit("timer",t.getMin(),t.getSec()); + io.emit('timer', t.getMin(), t.getSec()); }, 1000); - const avatars = []; const enemis = []; @@ -94,9 +91,8 @@ io.on('connection', socket => { }); let spawnIntervalLV1 = setInterval(() => { - - if(t.getMin()>1){ - LVL2start=true; + if (t.getMin() > 1) { + LVL2start = true; } let randomY = Math.random() * (canvasSize.height - 0) + 0; @@ -113,7 +109,7 @@ let spawnIntervalLV2 = setInterval(() => { do { randomY = Math.random() * (canvasSize.height - 0) + 0; } while (randomY > canvasSize.height - 100); - const newEnemy = new enemi(canvasSize.width-100 , randomY, 1, 2); + const newEnemy = new enemi(canvasSize.width - 100, randomY, 1, 2); enemis.push(newEnemy); } }, 800); @@ -123,9 +119,9 @@ setInterval(() => { let avatarData = []; avatars.forEach(avatar => { + avatar.canvasSize = canvasSize; enemis.forEach(enemi => { if (LVL2start) { - enemi.setVx(10); enemi.setVy(4); }