Let’s Make a 4X Space Game with Unity – Part 23 Star Bases 2

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.

Part023Pic001
buildCue

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.

Part023Pic002.JPG
Constructor

Now we need two new public void methods, one called AddShipToBuildCue and the other called RemoveShipFromBuildCue. The need to both be passed a Ship.

Part023Pic003.JPG
Methods

Add List.Add and List.Remove to the respective methods to add and remove ships from the build cue.

Part023Pic004.JPG
Methods Complete

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.

Part023Pic005.JPG
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.

Part023Pic006.JPG
BuildStarBase

In this method set planetData.starBase to a new StarBase.

Part023Pic007.JPG
Creating 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. 

Part023Pic008
starBaseGO

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.

Part023Pic011
Creating the GameObject

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.

Part023Pic012
New Method to Create the GameObject

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.

Part023Pic009.JPG
Build the Star Base

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.

Part023Pic010.JPG
Create the Star Base

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.

Part023Pic023.JPG
Star Base in the Game

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.

Part023Pic013.JPG
2D Button

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.

Part023Pic015
Creating the Panel
Part023Pic014
Location

Now add a another called Star Base Panel. Make it a similar size as the panel shown below.

Part023Pic016
Location

Make the Build Ship Button a child of this panel and move it to the location shown below.

Part023Pic018
Build Ship Button
Part023Pic019.JPG
Moving the Button

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).

Part023Pic020
Build Ship Button
Part023Pic022.JPG
Star Base Panel
Part023Pic021
Planet Panel

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)

Part 24 Star Bases 3

Advertisements

1 Comment »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s