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

ajouts objets + héritage entités

parent a38969eb
No related branches found
No related tags found
No related merge requests found
client/public/images/vie.png

90 KiB

client/public/images/vie.webp

87.7 KiB

import { Projectile } from './Projectile.js';
import Entite from './entite.js';
import { Hitbox } from './hitbox.js';
export class Avatar {
y;
x;
export class Avatar extends Entite {
nom;
vies;
click;
vitesse;
image;
canvas;
projectiles;
score;
hitbox;
inertie;
momentumX;
momentumY;
statut;
constructor(nom, vitesse) {
super(0, 0, new Hitbox(68, 145, 0, 0), null);
this.nom = nom;
this.score = 0;
this.y = 0;
......@@ -56,7 +55,7 @@ export class Avatar {
}
setImageCanvas(image, canvas) {
this.image = image;
super.image = image;
this.canvas = canvas;
}
......@@ -72,16 +71,16 @@ export class Avatar {
return this.nom;
}
getX() {
return this.x;
getClick() {
return this.click;
}
getY() {
return this.y;
setStatut(status) {
this.statut = status;
}
getClick() {
return this.click;
getStatus() {
return this.status;
}
colision(x, y, image) {
......@@ -152,13 +151,13 @@ export class Avatar {
}
initAvatar() {
super.hitbox = new Hitbox(68, 145, this.x, this.y);
super.y = 0;
super.x = 0;
this.score = 0;
this.y = 0;
this.x = 0;
this.vies = 3;
this.click = [];
this.projectiles = [];
this.hitbox = new Hitbox(68, 145, this.x, this.y);
}
enVie() {
......
import Entite from './entite.js';
import { Hitbox } from './hitbox.js';
export default class Bonus extends Entite {
nom;
taille;
apparition;
constructor(choix, taille, x, y, image, time) {
super(x, y, new Hitbox(image.width / 2, image.height, x, y), image);
this.choix = choix;
this.taille = taille;
this.apparition = time;
}
estExpire(t) {
return t - this.apparition >= 60;
}
getChoix() {
return this.choix;
}
getTaille() {
return this.taille;
}
}
export const bonusNoms = ['vie', 'invincibilite'];
export const bonusImages = ['/images/vie.webp', '/images/vie.png'];
export const bonusTaille = [1, 5];
......@@ -5,7 +5,7 @@ export default function draw(canvas, context, image, x, y) {
x >= 0 &&
y >= 0
) {
context.drawImage(image, x, y);
context.drawImage(image, x, y, image.width, image.height);
return true;
}
}
import Entite from './entite.js';
import { Hitbox } from './hitbox.js';
export default class enemi {
y;
x;
export default class enemi extends Entite {
vy;
vx;
vies;
hitbox;
image;
amplitude;
direction;
positionInitialeY;
difficulté;
constructor(x, y, image, difficulté) {
super(x, y, new Hitbox(image.width / 2, image.height, x, y), image);
this.y = y;
this.x = x;
this.image = image;
this.difficulté = difficulté;
this.vx = 8;
this.vy = 0;
......@@ -23,7 +20,6 @@ export default class enemi {
this.amplitude = 20;
this.direction = 1;
this.positionInitialeY = y;
this.hitbox = new Hitbox(image.width / 2, image.height, this.x, this.y);
}
colision(x, y, image) {
......@@ -39,14 +35,6 @@ export default class enemi {
return this.vies;
}
getX() {
return this.x;
}
getY() {
return this.y;
}
getDifficulte() {
return this.difficulté;
}
......
export default class Entite {
y;
x;
hitbox;
image;
constructor(x, y, hitbox, image) {
this.x = x;
this.y = y;
this.hitbox = hitbox;
this.image = image;
}
getX() {
return this.x;
}
getY() {
return this.y;
}
getImage() {
return this.image;
}
getHitbox() {
return this.hitbox;
}
}
......@@ -4,6 +4,8 @@ import { io } from 'socket.io-client';
import timer from './timer.js';
import setHtml from './setHtml.js';
import draw from './draw.js';
import Bonus from './bonus.js';
import { bonusNoms, bonusImages, bonusTaille } from './choixBonus.js';
const socket = io();
let t = new timer();
......@@ -11,6 +13,7 @@ let t = new timer();
const canvas = document.querySelector('.gameCanvas');
const context = canvas.getContext('2d');
const enemis = [];
const bonusArray = [];
const avatar = new Avatar('julien', 1);
const imageMortier = new Image();
const imageProjectile = new Image();
......@@ -101,11 +104,29 @@ document.addEventListener('keyup', event => {
avatar.changerClick(event);
});
setInterval(genererBonus, 1000);
function genererBonus() {
if (gameStarted) {
const choix = Math.floor(Math.random() * bonusNoms.length);
let randomY = Math.floor(Math.random() * canvas.height);
let randomX = Math.floor(Math.random() * canvas.width);
let img = new Image();
img.src = bonusImages[choix];
img.width = 100;
img.height = 100;
const bonus = new Bonus(choix, 1, randomX, randomY, img, t.getTotalTime());
bonusArray.push(bonus);
}
}
setInterval(() => {
avatar.deplacer();
avatar.projectiles.forEach(projectile => projectile.deplacer());
enemis.forEach(enemi => {
if (enemi.hitbox.colision(avatar.hitbox)) {
if (
enemi.hitbox.colision(avatar.hitbox) &&
avatar.getStatus() != 'invincibilite'
) {
if (canLostLifeAvatar) {
avatar.decrementScore(5);
enemis.splice(enemis.indexOf(enemi), 1);
......@@ -139,6 +160,19 @@ setInterval(() => {
}
}
});
bonusArray.forEach(bonus => {
if (bonus.hitbox.colision(avatar.hitbox)) {
if (bonusNoms[bonus.getChoix()] == 'vie') {
avatar.gagnerVie();
} else if (bonusNoms[bonus.getChoix()] == 'invincibilite') {
console.log('INVICIBILITE');
avatar.setStatut('invincibilite');
}
bonusArray.splice(bonusArray.indexOf(bonus), 1);
} else if (bonus.estExpire(t.getTotalTime())) {
bonusArray.splice(bonusArray.indexOf(bonus), 1);
}
});
});
}, 1000 / 60);
......@@ -186,6 +220,20 @@ function render() {
enemi.setVy(4);
});
}
bonusArray.forEach(bonus => {
console.log(bonus);
if (
bonus.x <= canvas.width - bonus.image.width &&
bonus.y <= canvas.height &&
bonus.x >= 0 &&
bonus.y >= 0
) {
draw(canvas, context, bonus.getImage(), bonus.getX(), bonus.getY());
} else {
bonusArray.splice(bonusArray.indexOf(bonus), 1);
}
});
enemis.forEach(enemi => {
console.log(enemi.getDifficulte());
if (
......
......@@ -16,6 +16,10 @@ export default class timer {
}
}
getTotalTime() {
return 3600 * this.getHrs() + 60 * this.getMin() + this.getSec;
}
getSec() {
return this.sec;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment