Search code examples
oopdesign-patternsarchitecturegame-engine

Game Architecture: Class leaking into almost all files


We currently have the problem that our main class GameController is being pulled into every backend file in our game. We're wondering what are common solutions to this problem.

Here's a bit more about the game architecture. The game is a board game, so since ~90-95% of the time there isn't anything happening, the game is set up more like a rest API. It waits for a user prompt, when received, the msg is distributed to the respective components of the game and the proper logic is executed. No large update loops, just executes logic when prompted.

The problem is that as this msg cascades through the system, GameController acts more like a relay point between the systems. It's how the nodes communicate to each other so that all game components are updated properly. The problem is that it's created this system where all new/old classes contain a pointer to parentGame, so GameController is everywhere.

Are there any simple architectural solutions to avoid having every class contain a pointer to parentGame? Is this necessarily a bad thing?

Some example code:

class GameController {
    bank: Bank
    action: Action
    ...
}
class Bank {
    parentGame: GameController

    constructor(game: GameController) {
        this.parentGame = game
    }
}
class Action {
    parentGame: GameController

    constructor(game: GameController) {
        this.parentGame = game
    }
}

Solution

  • I don't believe it is necessarily a bad thing, but you might be able to clean it up a bit by wrapping it in some kind of eventing mechanism in the middle - in other words, pub/sub might give you some of the decoupling that you want.

    It doesn't have to be anything that communicates via an external messaging service - that just introduces unnecessary overhead. The Observer Pattern comes to mind for me.