Last active
July 19, 2022 14:44
-
-
Save theScottyJam/f8637d6a3d96624c29330856f916d96d to your computer and use it in GitHub Desktop.
Article: Composition Alone Can't Replace Inheritance - helper functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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