Hi guys and welcome to part 17! In the last part we made our name plates face the camera. Lets leave our galaxy/solar systems for now. In this part we will start looking at how we can build ships in our game.

It’s time to write a couple of new scripts. But first lets tidy up our scripts folder a bit. It’s getting pretty crowded. Create 6 new folders inside the scripts folder. Call them Calculations, Camera, Data Handlers, Empires, Space and UI. Then move all the scripts to the assigned folders in the table below.

Folder Scripts
Calculations PositionMaths
Camera CameraController
Data Handlers TextAssetManager
Empires None (yet)
Space Galaxy, Planet, SolarSystem, SpaceObjects, Star
UI GUIManagementScript

Now that everything is more organised add two new scripts to the Empires folder. Call one Fleet and the other Ship. Make them both pure classes by deleting the “: Monobehaviour” next to the class name and the Update and Start methods.

part017pic001
Fleet
part017pic002
Ship

Start with the Ship class. Let’s think about what ships have in other 4X space games:

  • They have a name
  • They have hull hit points
  • They have shield hit points

Ok so now we add these to our ships. Declare a string called shipName, an int called hullPoints and an int called shieldPoints. Make them all public and add getters and setters to them.

Part017Pic003.JPG
Declaring Our Ship Stats

We also need max hull points and max shield points so add two more ints and give them getters but this time set should be protected.

Part017Pic004.JPG
maxHull and maxShields

Now add a constructor method called Ship and make it public. Pass it shipName, maxHull and maxShields.

Part017Pic005.JPG
Ship Constructor

Set the values equal to each other (set shieldPoints to maxShields and hullPoints to maxHull as a new ship will always start with full hull and shields).

Part017Pic006.JPG
Constructor Complete

So now our Ship class is set up let’s switch over to our Fleet class.

What is a fleet? Essentially all it is is a List of ships. You should know the drill by now… when adding a List, add the line “using System.Collections.Generic” at the top of the script. Then declare a new public list of type Ship called ships. Also declare a string for the fleet name with getter and setter.

Part017Pic007.JPG
Declaring Fleet Stats

Just like our Ship script add a public constructor method called Fleet. Pass it a name and a Ship.

Part017Pic008.JPG
Fleet Constructor

Now we need to set fleetName equal to name and ships equal to a new List of type Ship. We then need to add the passed ship to that List.

Part017Pic009.JPG
Constructor Complete

Let’s also add two new void methods called AddShip and RemoveShip. Make them both public and pass them a Ship.

part017pic010
Setting up the Methods

In AddShip add the Ship to ships. In RemoveShip remove the Ship from ships.

part017pic011
Methods Complete

We have now got a basic data structure for our ships. This seems like a good place to stop, so in the next part we will add a button to add ships to the game and a GameObject to represent our ships.

Scripts_for_part_17 (zip file)

Part 18 Building Ships 2