3 ms·
> The vast majority of essential code is not operating on just one object – it is actually implementing cross-cutting concerns. Example: when class Player hits(
by ttty 8y ago
> The vast majority of essential code is not operating on just one object – it is actually implementing cross-cutting concerns. Example: when class Player hits() a class Monster, where exactly do we modify data? Monster's hp has to decrease by Player's attackPower, Player's xps increase by Monster's level if Monster got killed. Does it happen in Player.hits(Monster m) or Monster.isHitBy(Player p). What if there's a class Weapon involved? Do we pass it as an argument to isHitBy or does Player has a currentWeapon() getter?
You have a function that has access to these objects. Based on that it will update the player and monster.
Function onAttack(from as player, to as monster, weapon)
player.addXp(weapon.xp)
monster.addHealth(-weapon.damage)
Probably you'll want to have immutable data instead of mutating this.
(I'm on mobile so I can't put too much code)
The reason you do this is to decouple code. You don't want the player to be aware of the monster, at least not in this scenario.