So this latest incarnation incorporates some cool stuff. In the Galaxy object, I’ve built functionality so you can query the Galaxy to return only stars that are owned by a particular civilization. The usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
local data= { -- The width and height of the -- galaxy in units. Pixels, light years, -- hey it's up to you, -- Number of civilizations -- minimum distance from another civilization width = screenW, height = screenH, civilizations = 1, min_dist_from_civilizations = 200, max_dist_from_civilizations = 400, } local galaxy = Galaxy:new(data) starModel = galaxy:GetAllStarsBelongingToACivilization(galaxy.civilizations[1]) |
That will return a table with all the star objects that belong to civilization 1 to the table ‘starModel’. Name it whatever you want, but in trying to keep my former promise of using the MVC paradigm, I’ll call it starModel. Remember the term OBJECTS. So we have the data for them, but we still need a display.
Here’s where I’ll break into my CoronaSDK background. We’ll create a bunch of display objects to represent the stars and the ship. I just have one ship for testing purposes below. The ship is the white hexagon flying from it’s starting point (the green star) to the destination (the red star).
I built some convenience code and attributes into the code for the Ship object. For the Ship object (which I think I included in the code from before, but we never used…) I added a ChangeHeading function, and a MoveForward function. These both use the mathlib library to do some heavy lifting in calculating radians to degrees and then implementing a function to move at an incremental speed to their destinations.
Check it:
1 2 3 |
shipModel[1]:ChangeHeading(starModel[3]) shipModel[1]:MoveForward() shipView[1].x,shipView[1].y = shipModel[1].x,shipModel[1].y |
We call the function every 250ms, so we tell the ship to get the heading of the starModel[3] object. Then, we tell the object to move forward along that heading. So, our screen shows the ship grooving along the path from the source star to the destination star…then it just kinda diddles around the destination star. We can build some functionality to have the ship ‘Dock” at the destination star if it gets within a certain distance, and perhaps have the star reference which ship are docked at it? We already have the Ship object with a builtin ‘DockedAt’ variable we can point to the destination star too.
See? Groovy things are a-happening! Download it here.
PS Aliens: