Skip to content
Snippets Groups Projects
Commit f1128361 authored by Mathis Decoster's avatar Mathis Decoster :apple:
Browse files

finission entite + ajout bonus

parent b2634dfd
No related branches found
No related tags found
No related merge requests found
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() {
......
export const bonusNoms = ['vie', 'invincibilite'];
export const bonusImages = ['/images/vie.webp', '/images/vie.png'];
export const bonusTaille = [1, 5];
......@@ -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);
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;
}
}
......@@ -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);
......@@ -27,4 +27,8 @@ export default class timer {
getHrs() {
return this.hrs;
}
getTotalTime() {
return this.hrs * 3600 + this.min * 60 + this.sec;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment