Mario Kart Item Post 7
Overview
Item pools and item growing/shrinking rework.
Item Pools
It was soon discovered after the engine rework, that the game will freeze briefly when loading in an item model during runtime. In order to stop this all the items that have models have an item pool which is created when the game scene is loaded. Items can then get an item mesh from an item pool when the item is constructed by passing in an item type enum. Also when an item is destroyed the item mesh is put back into its respective pool by passing in an item type enum.
Item Pools Class Structure
The structure of the item pools consists of a std::map which maps the item type enum to an item pool struct. This struct consists of an int holding the size of the item pool and a std::stack which is the item pool itself. This stack originally held TrackMagnet pointers which is the class that the item meshes instantiate from. It was soon realised, however, that the item’s animation mesh (which is an animation controller pointer) also needed to be loaded into the item pool in order for the game not to freeze. To fix this the item pool stack was changed to hold a pointer to an item mesh struct which held unique pointers to TrackMagnet and AnimationController to hold the regular mesh and animation mesh respectfully.
Creating item pools
When the item pools class is constructed it loops through the item type enum and for every item that needs an item mesh a respective item pool is created. To create an item pool the item type enum and the size pool (gained from the item config JSON) are passed into a create item pool function. This function creates a new item pool struct and sets the pool size within the struct to the size passed in. If this size is greater than 1 then the function returns early as no item pool is needed. Next, the function loops equal to the size of the item pool and each iteration it creates an item mesh struct and adds it to the pool. If the item type is a bomb then an explosion mesh is also made and added to a separate pool. Once finished looping this item pool is made into a pair along with the item type enum and passed into the item pool map.
Retrieving and adding meshes from item pools
The code base’s service locator points to an instantiation of the item pool class in the game scene. When an item is constructed and needs an item mesh it calls the get item mesh function in the item pool class through the service locator. This function takes the item type enum and inserts it as the key of the item pools map to return the respective item pool struct. From this, the top item mesh in the pool stack, in the pool struct is popped off the stack and returned in the function. If the item pool stack is empty then a new item mesh is created and returned (this can freeze the game if this event occurs). When the item is destroyed the item mesh’s variables are reset to their original values and added onto their respective pool by using the item type to find it in the map. For some items, the reset of the item’s rotation will not occur and when that mesh is used again by another item the mesh’s previous rotation is still there. To help prevent this from affecting gameplay the item pool stack was changed to a std::queue so that when items get added to it they won’t be used again until the queue reaches that mesh.
Growing and Shrinking rework
The rendering systems for items and players have been changed where the regular mesh of item and the player no longer render their regular mesh (physics is still applied to this mesh) and the animation mesh is only ever rendered. Because of this the items that scaled the player or themselves (giant mushroom. lightning cloud, and the bomb’s explosion) scaling implementations needed to be re-done. Another group member made a scale animation function in the animation mesh which to a scale to grow/shrink to and duration to do it in. Using these items’ variables and configs were changed to work with this system. A is growing and is shrinking bools were added to these items which are set to true once the grow animation function is called so it doesn’t call it again. A check was also added to the animation mesh which returns true if the scale animation is complete. This is then used to swap between the scaling states. Also in the case of the bomb’s explosion and the giant mushroom and explosions and player’s regular mesh’s scale is set to the animation mesh’s scale each frame. This is done so that their colliders also increase in scale.