-
Notifications
You must be signed in to change notification settings - Fork 3
Game Controller and Persistence
The Game Controller is an extensive, well documented library for maintaining and retrieving static game state for several game features: scenes grouped by level, any specified object location, inventory, items and levers. As well as extension methods for resetting game state per scene or per level.
Because the game controller is used by every scene had has code running every frame it needed to be efficient and a lot of time was spent testing it.
A Public Enum is included in the game controller to keep a record of scenes. Attributes such as level and filename are attached to the scenes. This Enum saved a lot of headache in development as it adds a drop down when selecting things such as the scene to load when activating portals. Previously you had to manually type the file name of the scene to load and we had issues with members mistyping the filename and wondering why their implementation didn't work. Also this meant queries such as retrieving scenes per level could be dynamic: previously a hard coded implementation where the file name for each scene in a level had to be checked was in several places, now the hard coding is only in one place.
To persist object positions per scene you simply drag the objects you want to persist as children of a scene-persistence component (e.g. players, boxes, planks). This works because a recursive method retrieves all child-components at startup. Two Dictionaries (hash tables) are used to maintain both initial and the latest position of these objects. When restoring scenes the 'savedPosition' dictionary is queried and when resetting the 'initialPosition' dictionary is queried.
The RNG element of our game is to randomly generate items on a per level basis but not per scene. To resolve this the generation of items is maintained per scene in an ordered dictionary which is only cleared when levels are reset. This means items are only generated once for a scene so the position of items are maintained when returning to scenes.
The inventory is also global and persisted throughout the entire game (including levels). This was not easy to implement. Initially we kept a store of items the player had picked up and on loading a new scene located the inventory and placed these items into the inventory. This didn't work as the item image was not rendered because Unity requires the game object to be re-created. To fix this persisted items are re-spawned (again) and dropped at the player location and therefore immediately picked up.
It was trivial to maintain the plate location as the object persistence was already setup. However to maintain the plate direction and lever state two additional dictionaries were required. The use of many dictionaries meant different queries could be made for various configurations: Resetting a scene resets all levers in that scene but not in other scenes, while resetting a level resets all levers in all scenes in that level and dying in a scene means only the player state is reset but not the lever state.
Game Overview and Required Features Selection
Mapping Critical Thinking to Game Aspects
Prototype Team Development Log