3 ms·
> You end up with: > Player.hits(Monster).with(Weapon) I think games are a particularly pathological case for OOP, and as such probably not a good example for
by fenomas 8y ago
> You end up with:
> Player.hits(Monster).with(Weapon)
I think games are a particularly pathological case for OOP, and as such probably not a good example for the article's case. But FWIW, the problem with what you describe (and with OOP for games generally) is that game logic tends to be way too polymorphic for code like that. That is, players don't just Hit() monsters, they also hit items, traps, breakable terrain, etc., and they get hit by monsters, by projectiles, maybe explosions, fall damage, etc.
And the dilemma of doing all that in OOP is, you find yourself with 20 different things that can receive a Hit(), that have little in common otherwise. Some have hit points but others don't, some don't have an armor value, some need to receive knockback but others don't even have a physics body, etc. As a result, do you make Hit() accept 20 different types, with special cases for each? Or do you rejigger your class hierarchy so that those 20 classes all inherit from Hittable, etc? Either could work in this or that case, but neither's much fun.
This is all why ECS (or other aspect-based approaches) are so popular for games - they let you define very general "Hit(src, tgt)" chunks of logic, that don't care what type each object is, but can easily query whether or not they have hit points or a physics body.
But again, I think games are a pathological case here and none of this should necessarily be considered an argument against OOP generally.
- scarface74 8y agoI find the fluent style programming to be much more readable. But in OOP you would define an IHittable interface or use AOP like you suggested.
- fenomas 8y agoYou can define iHittable and get the code to compile, but it doesn't make the actual issue (polymorphism) any easier to tackle. I think eschewing inheritance in favor of aspects is definitely the way to go. (Of course, you can wrap everything in a fluent-style interface either way - that part is orthogonal to all the rest.)
- pas 8y agoECS https://en.wikipedia.org/wiki/Entity%E2%80%93component%E2%80%93system https://en.wikipedia.org/wiki/Entity%E2%80%93component%E2%80... for others like me who had no clue.