Gang of Four Pattern Observer - Jacob1225/pufferfish-minecraft-mod GitHub Wiki

The Observer Pattern (one of the Gang of Four patterns) was used in our mod. This pattern has subscribers that listen and register events caused by a publisher.

In our mod, there is an interaction between the user and the arcade machine. If the user hits the arcade machine block, a container will open up. This interaction causes an event to be triggered and is passed to the machine block class, which will register the event and open the container class. If the user inputs and then insert a token, another event is fired to register to this event. This allows client-side interaction for the game to work. The container class that is built-in Forge allows us to have events triggering in sequence for the user to interact with the block.

Additionally, both the player's spaceship and the invaders were both subscribers and publishers to each other. Once a bullet touches either the player or the invader, an event is triggered.

If a player is hit, the event would be sent downstream to trigger the destruction of the spaceship. This caused a fire image to be displayed indicating the player has died, accompanied by necessary sound effects. This is then followed by a game over screen.

If an invader is hit, the event would be the destruction of the invader. The scoreboard listens to this event and increases, as well as a sound being played indicating an invader is dead. Once all the aliens are destroyed, an event is triggered to end the game as a winning screen.

This pattern is necessary for the space invader game to work as intended from start to end.