This week I have continued to add more core mechanics for the game. I have started to notice that I am adding the mechanics in order that the player may use them. As last week I worked on selecting units and buildngs, then this week I have worked further on building buildings, training units and all resources cost that corresponds to the unit/building type.
The first thing out of these three areas to add was resources, as every unit/building will have some resource cost. A section at the top of the screen for the ui was created that could represent resources and a basis for implementation of some menu later on. Resources in Enemy Confederation are Food, Wood, Gold and Iron. A serparate script was created that controlled all resource management with each resource having a get/add to functions then a master update resources which changes it on the UI everytime resources change.
A basic building mechanic already existed, it just required to actually cost resources. As the same building script is applied to all buildings, the cost couldn't be defined as it would make it the same for every building. Therefore I needed someway to store the data for these costs. I first considered an xml files that held the information and then parsing through it to extract it, however I discovered scriptable objects. This is a different class that scripts can derive from instead of MonoBehaviour and allow you to create data files using them. I created a scriptable object called BuildingType that contained four variables. Wood Cost, Iron Cost, BuildTime and MaxPopulationIncrease. I can then get the script to "create asset menu". This allows me in the Unity editor to create an asset of type "BuildingType". So I created four BuildingType objects. Two for the Town Centre and House already made, two for two new buildings (Barracks and Archery Range).
Once these were created it just needed to be implemented on each building so they can get a reference to their building type. Then once an object has been instanced and not placed in the Build script, when the player clicks it will check they have the required resources. If they do it will place, if they don't it will cancel the building.
Some buildings such as the town centre are going to be able to train units. Creating a similar scriptable object to building I created one for units. This meant I could update the Town Centre canvas to enable this option in the same style that the worker has for building buildings.
This image also shows a small addition of making the worker go to the cloest point to actually build the building. Once the worker is in the building position it chose, the building will start a coroutine that checks when the build time is complete using the build time variable in the scriptable object. To inform the player that the building is actually being built the building needed a progression percentage. All buttons that buildings have to train units had to be grouped under an options panel that was enabled only when the building was built. Further checks had to be added as the player could decide to send multiple workers to build a building, therefore the building script got a list of workers that had been assigned to work on it, and multiplied the progress increase by how many workers there are (Note, currently multiple workers stand on the same place if sent to build a building, this will change). Another check was added to make sure the progress stopped if the worker moved away and stopped building. A final check had to be made if a worker was told to go to a building that wasn't yet built, it will continue building it. An interesting bug occured when shift clicking to create multiple buildings. The unit will start travelling to the first when it is clicked, and then move to the second. It will then build in code like it is meant to, but as there is only one canvas for each building, it wasn't being updated with the correct details for each building when it was selected. Meaning the first building would visually show it had been built, but not actually be in code.
Once a building has been placed units need to be trained and placed around the building. Using the corner points of the building, a unit will spawn a set distance away from the building all around it. When a unit is placed a check is made against the A* grid to make sure the area is able to place a unit. Then if all the areas get full the player will need to move units before creating more.
There are a few small things before this section can be completed. Units themselves are still instant in creating and have no training progression text, and units can be queued in a building so a foreseen problem will be having the player create 5 units, and all 5 create within the time one does. When the workers travel to a building, they actually only travel to the closest corner, so if they are inline witha building, they actually travel to a further point. Then the final bug to fix are having multiple workers build on one spot. These need to be changed in the next week