Posted May 25, 2023 by lucaseverettjones
Architecture is the art of designing the spaces we inhabit. A poorly designed building is unpleasant and confusing for the people who must work in it. Architecture is also a part of code, and poorly designed code is much the same as a poorly designed building. Without planning your code when you start, it will become messy and difficult to expand or change.
For the first feature, we only need to worry about the player and the tablet. However, I decided to design the architecture for the full game to start with. That way, we know what scripts we need and don't have to scramble to plan it out while we make it.
I started by planning one script per function we would need. I split the player controller into three pieces: stats, actions, and inputs. The stats would handle things like health and stamina, while the actions would define all the things the player could do, like running and jumping. Seperating these from the inputs meant we could reuse these scripts for the AIs, swapping out inputs for the algorithm that controlled the AI. The inputs, of course, simply meant checking if keys like W and Spacebar were pressed.
The main feature of the game is the gradual unlocking of movement abilities like running and dashing by destroying tablets - to start, the player can only walk with WASD. Inputs is also the place where it is checked to see if an action is avaliable. A seperate RuleContainer script would be used to store which actions are allowed, and a TabletContoller script would be attached to the tablet to enable its corresponding action when destroyed.
In addition, some other miscellaneous scripts were needed. A UIController would display health and stamina on the UI, while a SceneController loaded new scenes and quit the game when needed. To save the game, a dedicated script would be needed. Some tablets were planned that affected physics rather than just player actions, and these would be best done seperately. We needed a SoundController to play music and sounds. Lastly, we would need something to play particle effects and non-character animations such as opening doors.
This produced a great many scripts, many of which only did one thing. While every script is meant to be very specific in what it does, these were too simple. Several groups of these simple, single-use scripts were closely related, so I combined them together. The TabletController was meant to be attached to each tablet, while the RuleContainer was meant to have a single instance that everything referenced, so these needed to stay seperate. However, the save file, UI, and SceneController scripts were combined into one given their close relation in managing the game. The ActionContainer and StatContainer were also combined into a single, EntityController script.
In the end, our architecture ended up looking like this:
Scripts:
EntityContainer
PlayerController
RuleContainer
TabletController
AIController
UIController
PhysicsController
FXController
SoundController
The most important thing when architecting the code of a game is to know what your code needs to do. After that, it's best to start by planning every function as its own script, and if that produces too many small scripts. combine similar ones. Only an incredibly simple project can be done reasonably without some attention to code architecture.