Hi guys and welcome to part 14. In the last part we added a central mass of stars and stopped the stars colliding with each other. In this part we will bring everything together and get our spiral galaxy to work just like our disk galaxy. We will also fix the camera so we can pan to the edges of the galaxy again.
Warning: Brace yourselves for some serious cut and pasting as we tidy the code up! If you are a lazy person and just want things to work now:
Initialise the dictionary, set the galaxy seed, set galaxy view to true, create the planet data after creating starData and then add starData and starGO to the dictionary. If you don’t understand how to do that, stop being lazy and read on :P.
1. Viewing Solar Systems Again
Open up the Galaxy script. In the CreateGalaxyMethod we create a new dictionary for starsToObjectMap, set the random seed and set galaxyView to true. We will be doing this in our CreateSpiralGalaxy method too. Instead of copying and pasting, let’s create a new method called InitializeGalaxy. Make it void and don’t pass it anything. Cut and paste the three things I just mentioned into this method and then call InitializeGalaxy where they were. Then call it in the appropriate place in CreateSpiralGalaxy too.



Let’s also tidy things up a bit by creating two new void methods called CreateArms and CreateCentre. Cut and paste the correct for loops into these methods. Don’t forget to call them.



We now have an issue in that percent, starsInCentre etc… can’t be passed into the new methods. To solve this we need to declare them in the Galaxy class instead (i.e. where we declare things like numberOfStars). This will make them common to the whole class and not just CreateSpiralGalaxy.
We also need to remove all the int and float types in the CreateSpiralGalaxy method.


We need to do this with starCount as well. It would also be a good idea to add starCount to InitializeGalaxy and set it to 0. Make sure all other “starCount = 0” are removed from the code (use the find tool to help you).


Ok so there’s a lot of tidying up going on in this part but trust me it will be worth it further down the line. Add three new methods called GalaxyMaths, CreateStarData and CreateStarObject.
Make GalaxyMaths void and don’t pass it anything. Make CreateStarData return a Star and pass it starCount (i.e. an int). Make CreateStarObject void and pass it a Star and a Vector3 (starData and cartPosition).



In GalaxyMaths cut and paste our percent, starsInCentre etc… into this method and then call it.


Look how neat and tidy our CreateSpiralGalaxy looks. Only four lines of code :).
How about the other two methods I hear you ask. Well for the CreateStarData we cut and paste the line where we create starData. We also call our CreatePlanetData method before returning starData.

In the CreateStarObject method we cut and paste the line where we create starGO. We also add starData and starGO to starToObjectMap.

Now we call these in the appropriate places in CreateArms and CreateCentre.


Press play and you should be able to view solar systems again :). Sorry for this being a very long winded way to get things working again but like I said these improvements will be worth it in the future if we need to comeback and trouble shoot and cant remember what we did.
2. Fixing the Camera
So at the moment the camera does not pan all the way to the edge of our galaxy. This is because the camera is clamped to the maximumRadius value of the galaxy which we do not use in our spiral galaxy. We do not need to make any changes to our CameraController script however, all we need to do is in our GalaxyMaths method is set the maximum radius equal to our furthest possible star.
But how do we know our furthest possible star? For this we take our distance calculation and sub in numberOfStars/numberOfArms when we calculate a square root.

The camera should now work correctly :).
While we are tweaking the camera it might also be a good time to change it so the camera cannot zoom too far out while in the solar system view. Open up the SolarSystem script. Declare two new floats called solarSystemZoom and storeMaxZoom. Make solarSystemZoom public and set the default value to 100. Keep storeMaxZoom private and set it equal to zero.

At the end of the CreateSolarSystem method, set storeMaxZoom equal to maxZoom then set maxZoom equal to solarSystemZoom.

Now all we do is reset maxZoom in the DestroySolarSystem method.

Now when in the SolarSystem view the camera will not zoom out as much as when we are in the galaxy view.
This concludes this part. In the next part we will look at importing a name list for our stars.
Scripts_for_part_14 (zip file)
I’ve just finished this part and i can’t barely wait to see what’s coming next. You did a really good job and i hope you will continue your tutorials in the coming months. I have just one question about the gameplay of your 4X, will it be turn-based or real-time ? And do you plan to explain how to do both ?
See you soon on next parts!
LikeLike
Hi Aymeric thank you for you’re great feedback 🙂 I put a lot of time into these tutorials and it’s nice to know they are appreciated.
To answer your question, I still haven’t decided. I am leaning towards turn based. Which one would you be most interested in?
LikeLike
Hi,
As a big fan of Master of Orion and Endless Space I appreciate having time to plan my next move especially in a genre like 4X where there’s a lot of information and things to do.
So, i’m totally leaning toward Turn-based, cause i don’t like to rush and i really suck when gameplay is about playing quicker than the AI.
LikeLiked by 1 person
When I go back to galaxy view from solar system view, the galaxy is no longer spiral but is now a regular galaxy. I’ve gone back and copy pasted your scripts over my own but the problem persists.
LikeLike
Hi jake, please email me your code to shadowplaycoding@gmail.com
I will try to help as best I can. Did you change the galaxy button to call th create spiral galaxy method instead of the create normal galaxy method?
LikeLike
Maybe the problem has already been solved, but I think the problem was that he forgot to change the on click method in the Galaxy view button to the new one.
LikeLike