Hey, so I’m making a game and have already completed the majority of the engine. It is developed in an object-oriented fashion. The game works well, but I have one issue that I’m afraid will begin to clutter up my code before too long.
My Player class is 1,499 lines long!
I’ll admit that there are a few reasons for the lines being this long. The Player inherits a lot of different interfaces. I tend to skip a lot of lines for readability purposes. For every action the player does, he refers to an Action object within an array which forces me to make many different classes implementing the Action interface who’s definition is encapsulated within the Player.
Yeah, so the last item I mentioned is probably responsible for 700 of the 1,499 lines in the Player.java file. Basically, every action the Player does is delegated by one action in an array of actions. When I want to start another action, I call upon the method setAction(int action). Something like this happens…
/**
* Action gets assigned
*/
public void setAction(int action)
{
// Leaves our current action by calling the exit method. Potentially cleans up some leftovers. I might get rid of this method
//since it has proven to be pretty useless so far.
actions[actionPointer].exit();
// Assigns the action pointer and calls on its enter method for housekeeping purposes. Action gets ready to be run.
actionPointer = action;
actions[actionPointer].enter();
}
/**
* Action gets run
*/
public void update()
{
actions[actionPointer].run();
}
This paradigm is really nice because the Player’s behavior is isolated to one Action object, and the Action object can determine when the player needs to switch to another action. If you are in the Standing action and are on the ground and pressing up, then the player will go to the Jumping action. If the player is Jumping, then the check to see if the user is pressing the Up key will never happen. That check is only in the Standing action. Many local variables can be stored within each Action object because they only ever related to that action.
This paradigm is also bad because each action takes up many lines of code since each new Action must have a definition that is written in the Player class.
Well, I don’t have TOO much of a problem with this since each Action is easy to maintain, but there are also a lot of variables that simply belong to the Player class that determine if the Player is allowed to do something or not. If the Player is close to a ladder, the ladder will invoke the method consider(Climbable climbable) from the player which alerts the player that a Climbable is nearby. The player will proceed to climb the ladder if the local boolean canClimb is true. canClimb is only true if the current Action that is running says it’s true. This code is starting to become tangled…
If anyone has experience preventing this pattern from becoming a disaster, I would appreciate it if you could give me some advice. As things currently stand, I am perfectly capable of proceeding. I am just afraid that 1,499 lines of code is unreasonable for a single class. Like I said before, most of the fluff comes from having to write out new class definitions for new Action object. I would also like to mention that it is too late for me to adopt the functional paradigm with entities/components because my game engine is rather OO. I would have to scrap my entire project, excluding some resources, in order to adopt that paradigm.