Presenting, the Star, Planet and Galaxy objects. The idea is to build some simple objects for basics at this point, and then we can iterate through them as we go, adding more and more functionality. So I think this as good as any time to just start the basics.
So we can build stars, planets, species and galaxies. For our purposes we’ll be dealing with one galaxy.
How I’m implementing the Galaxy object is by using as a blend to create, store, and control any interactions (mostly) with all our object. The Galaxy object is being used as mostly a container object and then some functions to query those objects and interact with them. The method I’m trying to use is the MVC; Model / View / Controller. method. I personally haven’t used the method religiously, and built in a lot of display into the data stuff in the past, but I’ll try to keep it pretty clearly separated in my posts going forward. There’s some great benefits to keeping everything separated, including migration to other frameworks while retaining exactly the same sort of functionality.
My take on it is to keep this framework…..erm, framework agnostic. That is, I’m going to build it in pure Lua for now, so any SDK running Lua can use it to create the data model from it, and then it’s up to the user to just use the data in their framework of choice like CoronaSDK or Moai or LÖVE, etc.
The way I envision the usage is that to create a galaxy with a few default variables or a ton of custom variables to affect as many or as little features in the galaxy creation method.
So for example, we’d do something like:
local galaxy = Galaxy:new()
print("Number of civilizations: "..#galaxy.civilizations)
print("Name of civilization 1 "..galaxy.civilization.name)
That’d be pure Lua. Now for some CoronaSDK stuff we could add:
-- Run some tests on the galaxy object
-- This will return a table of all the stars that belong to civilization 1
local civ1stars = galaxy:GetAllStarsBelongingToACvilization(galaxy.civilizations)
-- Pick the colors outside the loop
-- so they'll be the same for the
local r = RAND(1,10)/10
local g = RAND(1,10)/10
local b = RAND(1,10)/10
for i=1,#civ1stars do
local tempCircle = display.newCircle( civ1stars[i].x,civ1stars[i].y,5 )
tempCircle:setFillColor( r,g,b )
print("Star "..civ1stars[i].name.." belongs to the "..galaxy.civilizations.name)
Which would give us something similar to:
Nothing super exciting visually, but behind the scenes there’s a lot of cool stuff going on!
If you’re interested, download the code artificial galaxies 2 here and take a look under the hood.
If you’re not interested in what’s going on behind the scenes, that’s totally cool too! That’s one of the awesome things about OOP, is you can approach it from a “Black Box” mentality; you don’t care how it works or why it does what it does, you just want the Galaxy object created and start to mess around with the data structures it provides. There’s a really awesome feeling of finding a great piece of code that does what you want it to without any tweaking on your part, and that’s what I’m attempting to provide here. 🙂
Next time, we’ll get more into some cool functions for the Galaxy object to do, such as distance from one civilization to another, minimum and maximum distances allowed, and maybe some ships too! Caution: Math ahead! BUT we don’t care, cause the OOP stuff will protect us from all that, you can just say “Move ship from Star A to Star B in increments of X.” That’ll be pretty cool.