3 ms·
I've run into this exact issue in my roguelike [1]. I don't think I have a fully baked solution yet, but the direction I'm going [2] is a combination of the Obs
by munificent 6y ago
I've run into this exact issue in my roguelike [1]. I don't think I have a fully baked solution yet, but the direction I'm going [2] is a combination of the Observer pattern and caching.
For derived stats, it's not enough to just always recalculate its value from scratch every time you access it. At least in my game, I also want to tell the user when the value of a derived stat changes. If you equip a piece of armor that raises your strength, I want the game to tell you "You feel stronger!".
So each derived stat stores its current value and then has a refresh() function to recalculate itself. The stat is refreshed at the end of each turn. It recalculates the value and then compares its previously cached one. If the value changed, it logs the result and then updates the cache.
[1]: https://github.com/munificent/hauberk https://github.com/munificent/hauberk
[2]: https://github.com/munificent/hauberk/blob/master/lib/src/engine/hero/stat.dart https://github.com/munificent/hauberk/blob/master/lib/src/en...