-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrawler.js
128 lines (107 loc) · 3.75 KB
/
Brawler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import Game from "./Game.js";
export default class Brawler {
static availableClasses = ['NINJA', 'ZOMBIE', 'CAPTAIN'];
static availableAttacks = [
{
type:"punch",
name: "Coup de poing",
damage: 1,
},
{
type: "kick",
name: "Coup de pied",
damage: 2,
},
{
type: "katana",
name: "Coup de katana",
damage: 5,
},
{
type: 'buff',
name: "Sort de Boost de dégats",
damage: 0,
isBuff: true,
active: false,
used: false,
},
{
type: 'shielded',
name: "Coups de pieds*",
damage: 1,
}
];
class = null;
healPoints = 0;
attacks = [];
constructor(name) {
this.name = name;
this.chooseClass();
//chose random heal points between 1 and 100
this.healPoints = Math.floor(Math.random() * 100) + 1;
}
chooseClass() {
// choose a random class
this.class = Brawler.availableClasses[Math.floor(Math.random() * Brawler.availableClasses.length)];
switch (this.class) {
case 'NINJA':
this.attacks = [
Brawler.availableAttacks.find(attack => attack.type === 'punch'),
Brawler.availableAttacks.find(attack => attack.type === 'kick'),
Brawler.availableAttacks.find(attack => attack.type === 'katana'),
]
break;
case 'ZOMBIE':
this.attacks = [
Brawler.availableAttacks.find(attack => attack.type === 'punch'),
Brawler.availableAttacks.find(attack => attack.type === 'kick'),
Brawler.availableAttacks.find(attack => attack.type === 'buff'),
]
break;
case 'CAPTAIN':
this.attacks = [
Brawler.availableAttacks.find(attack => attack.type === 'shielded'),
]
break;
default:
}
}
sendAttack(brawler) {
// select random attack
const attack = this.getRandomAttack();
if (Game.debug) {
console.log({attack})
}
// if the attack is buff, set it to active
if (attack.type === 'buff') {
this.attacks.find(a => a.type === "buff").active = true;
}
brawler.receiveAttack(attack, this);
}
getRandomAttack() {
// Récupération des attaques valides
const validAttacks = this.attacks.filter(attack => !attack.used && !attack.active);
// Si il n'y a pas d'attaques valides, on retourne null
if (validAttacks.length === 0) {
throw new Error('No valid attack');
}
// Sélection aléatoire d'une attaque valide
const randomIndex = Math.floor(Math.random() * validAttacks.length);
return validAttacks[randomIndex];
}
receiveAttack(attack, attacker) {
const absorbedDamage = this.class === 'CAPTAIN' ? 1 : 0;
let damage = attack.damage;
let buffedDamage = false;
let buff = attacker.attacks.find(a => a.type === "buff");
if (buff?.active && !buff?.used && !attack.isBuff) {
buff.used = true;
damage *= 3;
buffedDamage = true;
}
damage = Math.max(damage - absorbedDamage, 0);
console.log(`${attacker.name} attacks ${this.name} with ${attack.name} (${damage} damage${buffedDamage ? ' buffed' : ''})`);
this.healPoints -= damage; // if the damage is less than 0, set it to 0
this.healPoints = this.healPoints < 0 ? 0 : this.healPoints;
}
}