diff --git a/client/src/Projectile.js b/client/src/Projectile.js index 82e9a6b0a6419da1d736d30b27dc8ec455a8020c..67c3ffb2a4566c198e29d36e7026953038370cda 100644 --- a/client/src/Projectile.js +++ b/client/src/Projectile.js @@ -1,26 +1,13 @@ import { Hitbox } from './hitbox.js'; import draw from './draw.js'; +import Entite from './entite.js'; -export class Projectile { - x; - y; +export class Projectile extends Entite { vitesse; - hitbox; constructor(x, y, image) { - this.x = x; - this.y = y; + super(x, y, new Hitbox(122, 68, x, y), image); this.vitesse = 10; - this.image = image; - this.hitbox = new Hitbox(122, 68, this.x, this.y); - } - - getX() { - return this.x; - } - - getY() { - return this.y; } deplacer() { diff --git a/client/src/choixBonus.js b/client/src/choixBonus.js new file mode 100644 index 0000000000000000000000000000000000000000..78c45294c0ea4bbd222c748b4ed8f5df63eb6ff4 --- /dev/null +++ b/client/src/choixBonus.js @@ -0,0 +1,3 @@ +export const bonusNoms = ['vie', 'invincibilite']; +export const bonusImages = ['/images/vie.webp', '/images/vie.png']; +export const bonusTaille = [1, 5]; diff --git a/client/src/main.js b/client/src/main.js index f728a3caf40801323cdb8c79c190ae6610d6b532..2296e893fb237d35214aa459fe658890277b80cc 100644 --- a/client/src/main.js +++ b/client/src/main.js @@ -7,8 +7,8 @@ import draw from './draw.js'; import { Coordinate } from './Coordinate.js'; const socket = io(); -let min =0; -let sec=0; +let min = 0; +let sec = 0; const canvas = document.querySelector('.gameCanvas'); const context = canvas.getContext('2d'); @@ -32,8 +32,6 @@ let LV2Started = false; let canLostLifeAvatar = true; let canLostLifeEnemi = true; - - imageMortier.addEventListener('load', () => { avatar.setImageCanvas(imageMortier, canvas); requestAnimationFrame(render); @@ -44,9 +42,9 @@ imageEnemi.addEventListener('load', () => { }); setInterval(function () { - socket.on("timer",(minute,seconde)=>{ - min=minute; - sec=seconde; + socket.on('timer', (minute, seconde) => { + min = minute; + sec = seconde; }); }, 1000); @@ -89,14 +87,11 @@ function startGame(event) { const canvasResizeObserver = new ResizeObserver(() => resampleCanvas()); canvasResizeObserver.observe(canvas); - function resampleCanvas() { canvas.width = canvas.clientWidth; canvas.height = canvas.clientHeight; } - - document.addEventListener('keydown', event => { let canShoot = true; avatar.changerClick(event); @@ -115,10 +110,8 @@ document.addEventListener('keyup', event => { avatar.changerClick(event); }); - - - let newEnemis = []; +let newBonus = []; function render() { console.log(sec); @@ -128,11 +121,7 @@ function render() { context.font = '40pt New Super Mario Font U'; context.fillStyle = 'blue'; context.fillText(avatar.getScore(), 10, 50); - context.fillText( - 0 + ':' + min + ':' + sec, - canvas.width / 2, - 50 - ); + context.fillText(0 + ':' + min + ':' + sec, canvas.width / 2, 50); for (let i = 0; i < avatar.getVies(); i++) { context.drawImage(imageCoeur, canvas.width - (3 - i) * 50, 0, 50, 50); @@ -156,6 +145,12 @@ function render() { draw(canvas, context, imageEnemi2, enemi.x, enemi.y); } }); + socket.on('bonusArray', data => { + newBonus = data; + }); + newBonus.forEach(bonus => { + draw(canvas, context, bonus.getImage(), bonus.getX(), bonus.getY()); + }); requestAnimationFrame(render); } @@ -210,4 +205,4 @@ document.addEventListener('keyup', event => { event.preventDefault(); }); -console.log(canvas.width); \ No newline at end of file +console.log(canvas.width); diff --git a/server/bonus.js b/server/bonus.js new file mode 100644 index 0000000000000000000000000000000000000000..999d7186bb0245c00a6a18ccbaf5fd08bf3c7607 --- /dev/null +++ b/server/bonus.js @@ -0,0 +1,26 @@ +import Entite from '../client/src/entite.js'; +import { bonusImages } from '../client/src/choixBonus.js'; +export default class Bonus extends Entite { + nom; + taille; + apparition; + + constructor(choix, taille, x, y, image, time) { + super(x, y, new Hitbox(image.width, image.height, x, y), image); + this.choix = choix; + this.taille = taille; + this.apparition = time; + this.image.src = bonusImages[choix]; + } + + estExpire(t) { + return t - this.apparition == 60; + } + + getChoix() { + return this.choix; + } + getTaille() { + return this.taille; + } +} diff --git a/server/index.js b/server/index.js index d1613af6f20f149d4a360758cbfbb2c143407597..0f4d705bc1301b53418b99d39799fdadc85f6786 100644 --- a/server/index.js +++ b/server/index.js @@ -6,6 +6,12 @@ import { Avatar } from '../client/src/avatar.js'; import enemi from './enemis.js'; import { Coordinate } from '../client/src/Coordinate.js'; import timer from './timer.js'; +import Bonus from './bonus.js'; +import { + bonusImages, + bonusNoms, + bonusTaille, +} from '../client/src/choixBonus.js'; const app = express(); @@ -42,6 +48,7 @@ setInterval(function () { const avatars = []; const enemis = []; +const bonusArray = []; let cpt = 0; let canShoot = true; @@ -116,8 +123,20 @@ let spawnIntervalLV2 = setInterval(() => { } }, 800); +let spawnBonusInterval = setInterval(() => { + const choix = Math.floor(Math.random() * bonusNoms.length); + let randomY = Math.floor(Math.random() * canvasSize.height); + let randomX = Math.floor(Math.random() * canvasSize.width); + let img = new Image(); + img.width = 75; + img.height = 75; + const bonus = new Bonus(choix, 1, randomX, randomY, img, t.getTotalTime()); + bonusArray.push(bonus); +}, 15000); + setInterval(() => { io.emit('enemis', enemis); + io.emit('bonusArray', bonusArray); let avatarData = []; avatars.forEach(avatar => { @@ -166,6 +185,20 @@ setInterval(() => { y: avatar.getY(), projectiles: avatar.projectiles, }); + bonusArray.forEach(bonus => { + if (bonus.hitbox.colision(avatar.hitbox)) { + if (bonusNoms[bonus.getChoix()] == 'vie') { + avatar.gagnerVie(); + } else if (bonusNoms[bonus.getChoix()] == 'invincibilite') { + avatar.setStatut('invincibilite'); + avatar.setStatutTime(t.getTotalTime()); + } + bonusArray.splice(bonusArray.indexOf(bonus), 1); + } + if (bonus.estExpire(t.getTotalTime())) { + bonusArray.splice(bonusArray.indexOf(bonus), 1); + } + }); }); io.emit('avatarsData', avatarData); }, 1000 / 60); diff --git a/server/timer.js b/server/timer.js index 05694885d3245fc6b1b39aa4e21ff6302e5addd7..2fb3800dad681f9aa87f6fd6d79d97eb412a3cce 100644 --- a/server/timer.js +++ b/server/timer.js @@ -27,4 +27,8 @@ export default class timer { getHrs() { return this.hrs; } + + getTotalTime() { + return this.hrs * 3600 + this.min * 60 + this.sec; + } }