When to split a class, when to keep it all as one.

I have 2 classes in my game, the PlayStateController (InputHandler basically) and the MapController. At first, they were pretty well encapsulated, but as my project has advanced, they have all become so intertwined together that they almost seem like they belong in the same class file now. Many of their major functions are extremely dependent on using getters/setters to pass information to each other.

Here’s an example, in my PlayStateController, I handle my “floating” camera by detecting the player’s X and Y on the screen and sending a set statement to the MapController:

	public void update(MapController mapCtrl, GameContainer gc, StateBasedGame sbg, PlayState play) throws SlickException{
		if(!(controlLockout)){randomEncounter(sbg);}
	
		if(!(Math.abs(mapCtrl.getMapY()) >= (Math.abs(mapCtrl.getMapHeight()*32)-(gc.getHeight()/3)-10))){
			if(playerY > (gc.getHeight()/3)-140){
				if(playerY > (gc.getHeight()/3)-120){
					mapCtrl.setMapY(mapCtrl.getMapY()-2);
					playerY -= 2;
				}else{
					mapCtrl.setMapY(mapCtrl.getMapY()-1);
					playerY -= 1;
				}
			}
		}

…and then, within the MapController I find myself doing this, using a getter that in itself relies on other classes (in this case the PlayStateController class) to gather data to send it to whatever is requesting it:

EDIT: Clarification, the second code example isnt actually a getStatement, it’s something used internally in the MapController to process mathmatics to figure out how to render only the section of the ma visible on screen… :slight_smile:

	//Top left corner to start drawing the map. 
	public int mapDrawStartX(PlayStateController ctrl){return (mapX+((Math.round(ctrl.getPlayerX()-mapX+16)/32-16)*32));}
	public int mapDrawStartY(PlayStateController ctrl){return (mapY+((Math.round(ctrl.getPlayerY()-mapY+16)/32-16)*32));}
	
	//Bottom right corner to end drawing the map.
	public int mapTileStartX(PlayStateController ctrl){return (Math.round(ctrl.getPlayerX()-mapX+16)/32-16);}
	public int mapTileStartY(PlayStateController ctrl){return (Math.round(ctrl.getPlayerY()-mapY+16)/32-16);}

So my question is, are these signs I should just merge the 2 classes together? They talk to each other so much they’re both basically useless without the other.

Yeah it happens, especially if you’re ‘developing’ your design at the same time as developing the code. Sometimes it’s good to take a step back and do your own review of your design (which it sounds like you have), often I’ll do this when I’ve completed some new feature or change.

It sounds like you’ve essentially created two http://c2.com/cgi/wiki?GodClass :wink:

Rather than debating splitting or joining your two main classes, you might want to consider splitting out the ‘model’ code from the controllers and make your project more MVC http://en.wikipedia.org/wiki/Model–view–controller

Lots of small, simple classes with clearly defined responsibilities are easier to maintain and test.

You mention a camera, that might be a good candidate for a model class that is controlled by the PlayStateController. A map model might also be worth considering.

Just some random thoughts, hope it helps.

  • stride

Always stay away from cyclic dependencies like hell and generally keep dependencies low.
A map controller (whatever that is exactly) shouldn’t need to talk the input handler (which is confusingly named PlayStateHandler).
Arguments of the update method look rather suspicious, means too many and too general/global.
Do not merge but identify clearly separated game tasks and define your classes following that.