I am making a game in which the player character can gain traits that change how their character behaves in my world model or add functionality to it. For example, the character might gain the "feather" trait that changes how fall damage is calculated or the "strong" trait that means the player can now pick up items that were previously just part of the scenery.
I have been hard coding traits in their relevant classes and switching a boolean to change behavior when they come into play, but it is getting rather unmanageable as my trait list grows.(I am also missing out on a lot of the emergence I was hoping to get) There must be a more scalable and decoupled way of doing this.
How would you code such a feature where code behavior changes or expands at runtime? If you are not inclined to do my work for me, which programming patterns would you use to achieve this kind of behavior?
but once created the pattern doesn't describe how to change the objects at runtime
Given that an entity is pretty much just a list of components, you just need to implement that list with a mutable data structure so that you can add and remove items from the list. – jhocking May 19 '17 at 19:04if entity.has_component(feather) then gravity = world.gravity * 0.1
which deeply encodes the knowledge of "feather" into physics, you want to saygravity = apply_buffs(entity.buffs, world.gravity)
. Which can be modified per-frame by scripts, cool-downs curves, etc. - stuff that is overly complex via just adding/removing components. – Sean Middleditch May 19 '17 at 21:47