Say I have a User object that has an "xp" value. Now say that I have an XPLevelsModel which contains an array of XPLevels that define how many points are required for that level (among other things):
class User
{
xp : int;
}
class XPLevelsModel
{
levels : XPLevel;
}
class XPLevel
{
...
xpRequired : int;
name : String;
...
}
Okay so now say I have a number of methods such as "getCurrentLevel()" "getPercentProgressThroughLevel()" etc. Where should those methods live?
I could put them on the User object:
class User
{
constructor(levelsModel:XPLevelsModel) : {}
xp : int;
getCurrentLevel() : int {}
getPercentProgressThroughLevel() : Number {}
...
}
but as we add more data such as "lives", "health" etc etc User will start to grow with lots of functions (not very good for SoC)
I could try to group these things up into components such as "XPComponent" which is an object on User:
class User
{
xp : UserXPComponent;
}
class UserXPComponent
{
constructor(levelsModel:XPLevelsModel) {}
points : int;
getCurrentLevel() : int {}
getPercentProgressThroughLevel() : Number {}
...
}
but the problem is accessing this data from the rest of the game now means user.xp.getCurrentLevel(), which breaks the law of Demeter, plus its ugly, hard to test and then what happens if you go more levels deep?
There is one more option I thought of, to have something like:
class User
{
xp : int
}
class UserXPHelpers
{
constructor(levelsModel:XPLevelsModel) {}
getCurrentLevel(user:User) : int {}
getPercentProgressThroughLevel(user:User) : Number {}
}
Here we have separated out the logic and the data and pass the user through the methods. The only thing is, does this not break encapsulation? As all those methods rely upon a user to be passed to them, shouldn't they live with the user?
If this IS the correct solution, is "Helpers" the correct name for this?
I hope you can help a problem that has challenged me time and time again and have only now been able to articulate it into a query.
Mike