Skip to content

Instantly share code, notes, and snippets.

@theScottyJam
Last active July 19, 2022 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theScottyJam/f8637d6a3d96624c29330856f916d96d to your computer and use it in GitHub Desktop.
Save theScottyJam/f8637d6a3d96624c29330856f916d96d to your computer and use it in GitHub Desktop.
Article: Composition Alone Can't Replace Inheritance - helper functions
const monsterBehaviors = {
attack() {
playHurtAnimation()
},
die({ monsterId, dropLoot }) {
userInventory.add(dropLoot())
removeFromUi(monsterId)
},
}
export class Slime { ... }
export class Skeleton { ... }
export class Clown {
#id = Math.random()
dropLoot() {
return [new Key()]
}
attack() {
monsterBehaviors.attack()
if (itsTime()) {
showCutScene()
}
}
die() {
return monsterBehaviors.die({
monsterId: this.#id,
dropLoot: () => this.dropLoot(),
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment