Hi Guys and welcome to part 23. In the last part we started setting up our star bases by setting up a basic planet colonization system (not the final one). In this part we will continue with our star bases.
At the end of the last episode we created a StarBases script but we didn’t add anything to it. We will now.
What does a star base need? A build cue would be useful, so we will add a public list of type ship which will act as this.
We could also add things like shields or hull health, but for now I will just be sticking with this.
Next we need the constructor so add one called StarBase; don’t pass it anything for now. In the constructor, set the build cue as a new list of type ship.
Now we need two new public void methods, one called AddShipToBuildCue and the other called RemoveShipFromBuildCue. The need to both be passed a Ship.
Add List.Add and List.Remove to the respective methods to add and remove ships from the build cue.
We now have our basic StarBase class setup.
Next we need to open the Planet script and add a public StarBase called starBase. In the Planet constructor set this equal to null as by default a planet will not have a StarBase.
What we need to do now is assign the planet a new StarBase and make an object which can represent our star base in the game. For now we will use a capsule.
In our SolarSystem script add a new public void method called BuildStarBase and pass it a Planet called planetData and a GameObject called planetGO.
In this method set planetData.starBase to a new StarBase.
Next create a new GameObject called starBaseGO and make a capsule just like we make spheres for our planets/stars and cubes for our ships.
Set it’s name to the planet’s name + ” Starbase”. Set its scale to 0.3 in all directions. Then make starBaseGO a child of planetGO and set its local transform to (0.6, 0.6, 0.6). This seems like a good spot for the star base to be in comparison to the planet.
Create a new public void method called CreateStarBase. Pass it a Planet and a GameObject just like BuildStarBase. Cut and paste all the code we just wrote to create the starBaseGO into this method. Call the method from where we just cut the code from.
We need to do this because when we enter the solar system view we don’t want to set a new starBase to the planet everytime. This will wipe our buildCue list every time!
Where we create the planets in the solar system we need to add an if statement with the condition if the planet is colonised AND its starBase is equal to null. We then call our BuildStarBase method.
We then need to add an else if statement below it with the condition if the planet is colonised but the starBase is not null. This time we just call CreateStarBase.
Note: We will be changing this at some point in the future as we will want to have planets which are colonised but do not have a star base until the player builds one on that planet but it will do for now.
We now have a star base in our game! But what do we do now?
Well, we need to start setting up some UI so that when the planet with a star base is clicked on, we have the option of building some ships. We will star this now but will finish it off in the next part of the tutorial.
Change the view in the scene window to 2D by cicking on the button shown below.
Right click the Canvas in the hierarchy and create a new panel (under UI). Name it Planet Panel and make it a similar size and location to the one shown in the picture below. The Planet Panel will eventually be the panel shown when we click on a planet and will show all its information.
Now add a another called Star Base Panel. Make it a similar size as the panel shown below.
Make the Build Ship Button a child of this panel and move it to the location shown below.
This is probably a good time to mention Rect-Transforms. You may notice when a UI element is selected in the scene view some white arrows appear. These show where the shape is anchored to and where they scale from. I usually move these arrows so they make a box around the object. That way they scale and look exactly like they do in the scene window set up. Do this now for all the panels and the Build Ship Button (its probably worth doing it for the galaxy view button as well).
If you want to change the colours of the panels feel free. I will be leaving mine the default colour.
That concludes this part of the tutorial. In the next part we will finish this off so that when we select a planet the panel pops up, and if it has a star base the star base panel is also there. We will also add buttons to close the panels and to open the star base panel from the planet panel.
Scripts_for_part_23 (zip file)
Scripts (Paste Bin)