Skip to content

Instantly share code, notes, and snippets.

@multivoltage
Created February 7, 2021 21:27
Show Gist options
  • Save multivoltage/fbff8d95041fe3c3bc75084f21535ea1 to your computer and use it in GitHub Desktop.
Save multivoltage/fbff8d95041fe3c3bc75084f21535ea1 to your computer and use it in GitHub Desktop.
//
// Werewolf, it shall have 30 HP and 5 DMG
class Werewolf extends Enemy {
constructor(){
super('Werewolf', 30, 5);
}
}
// Vampire, it shall have 50 HP and 10 DMG
class Vampire extends Enemy {
constructor(){
super('Vampire', 50, 10);
}
}
// AntonioZequila, it shall have 80 HP, 20 DMG and should growl a different sound
class AntonioZequila extends Enemy {
constructor(){
super("AntonioZequila",80,20)
}
growl() {
console.log('Antonio!!!!');
}
}
// Then create an EnemyFactory class that can be used to create random instances of enemies, like the following:
class EnemyFactory {
static create(){
const enemies = [Vampire,AntonioZequila,Werewolf]
const count = enemies.length
const ranges = Array.from(Array(count).keys()).map(i => ((i+1)/count))
const random = Math.random()
let founded = false
let istance
ranges.forEach((r,index) => {
if(!founded && random < r){
founded = true
istance = enemies[index]
}
})
return new istance()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment