Class Structuring Advice

Hi, I’m planning out a game at the moment by thinking about what classes I’m going to need, what will extend what etc. and I wanted some advice on structuring. I will simplify my example a bit for the sake of clarity.

Suppose I have a Library class containing all information and methods for some library. Also I have a Computer class where a Computer can just be On or Off. The Library class stores a bunch of Computer objects. A maximum of 10 Computers can be turned on at once in a Library. If 10 are already on, then the next one fails to turn on.

Now, say I want the player to be able to walk up to a computer and click it on it to toggle it on/off. Should this be handled in the Computer class, Library class, or a different class entirely?

I could always pass the associated Library instance into a Computer as its initialised so the Computer can check how many computers are already on in the library, but this method feels wrong as I feel it should work more independently.

I could also handle it entirely from the Library. That way I can directly check how many Computers are already turned on before turning on the next one, but it feels counter intuitive having the on/off switch of a computer being handled by a completely different class.

Finally I could have a computer handle its own on/off switch, but whenever the computer is activated an event is fired, and the Library immediately turns it back off is there are already 10 activated. This isn’t ideal though as the “TurnedOn” event has already fired, possibly causing problems somewhere else.

There probably isn’t one perfect way to do this. It’s probably very dependent on the rest of the game I guess, but I’m curious how some of you would go about doing it?

1 Like

Whats the logic for having a limit? Whatever part of your games fantasy thats responsible for imposing the limit should probably handle the implementation for it.

E.g. if its limited because the wiring in the library cant handle the power draw, the library should handle it.

Itd also be weird for each computer instance to be coupled to the other computers, and less weird for the library to know about which (and how many) things are in it.

The idea of passing the library to each computer instances init method is even weirder tho. A computer will never be able to exist inisolation or outside a library.

2 Likes