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.
|Space||Galaxy, Planet, SolarSystem, SpaceObjects, Star|
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.
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.
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.
Now add a constructor method called Ship and make it public. Pass it shipName, maxHull and maxShields.
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).
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.
Just like our Ship script add a public constructor method called Fleet. Pass it a name and a Ship.
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.
Let’s also add two new void methods called AddShip and RemoveShip. Make them both public and pass them a Ship.
In AddShip add the Ship to ships. In RemoveShip remove the Ship from ships.
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)