vendredi 17 mars 2017

How to implement mutable functionality at runtime?

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. There must be a more scalable and decoupled way of doing this.

(The perfect scenario I want is a way that I can just program the class variations and get them switched at runtime in some way while keeping the context and references of the previous object, but any other way to suitably fulfill the requirements bellow is a good answer.)

How would you implement such a feature where methods change and new functionality is gained and removed at runtime?(Please provide an hypothetical example on the given scenario) If you are not inclined to do my work for me, which programming patterns would you use to achieve this kind of behavior?

P.S.:Please feel free to suggest a better title. I suck at those. I will edit this out once a better one has been found.

Aucun commentaire:

Enregistrer un commentaire