Javascript – Efficient and readable data structure for star system

Hello and thanks for reading.

As per title I’m trying to figure out an efficient yet readable data structure for a star system.

Basically it’s a tree structure:
quadrants > galaxies > systems > planets

Here’s what I’ve come up with up to now:

var quadrants = [

   { name: 'XX1C Quadrant', galaxies: [0,1,5,6], spritesheet: 'xx1c' },
   { name: 'Bob Quadrant', galaxies: [0,1,5,6], spritesheet: 'bob'},
   { name: 'MN2 Quadrant', galaxies: [0,1,5,6], spritesheet: 'mn2'},

];

var galaxies = [

   { name: 'Ara Perileos',   systems: [0,1,5,6], spritesheet: 'ara' },
   { name: 'Alatheia White', systems: [0,1,5,6], spritesheet: 'alatheia'},
   { name: 'Pavo Eridanus',  systems: [0,1,5,6], spritesheet: 'pavo'},

];

/* Systems */

var OPEN_CLUSTER     = 0,
    STAR_SYSTEM      = 1,
    PLANETARY_SYSTEM = 2,
    SOLAR_SYSTEM     = 3;

var systems = [

    {
        name: 'Eta Corvi System',
        description: 'lorem ipsum',
        type: OPEN_CLUSTER,
        bodies: [0,1,2]
    },
    {
        name: 'Nusukan',
        description: 'lorem ipsum',
        type: PLANETARY_SYSTEM,
        bodies: [0,1,2]
    },
    {
        name: 'Outeria 369 System',
        description: 'lorem ipsum',
        type: SOLAR_SYSTEM,
        bodies: [4,5,6]
    }
]

var bodies = [
    {
        name: 'Althea',
        resources: [{
            name: 'cobalt', qty: 500
        }]
    },
    {
       name: 'Zapto',
        resources: [{
            name: 'cobalt', qty: 500
        }]
    },
    {
        name: 'Flip',
        resources: [{
            name: 'cobalt', qty: 500
        }]
    }
]

I think it’s all right, but there starts to be a problem when I start to think about what part of the galaxy I can assign to the player (and save) as KNOWN.

I would like it to be “iteratable” and so that I can quickly assign a quadrant, galaxy, system or bodies to the player object.

I thought about simply adding a boolean to the objects to identify the items as “discovered”, but it means I’d have to save all the dataset.

Furthermore when I access the starmap I will scan the player’s known galaxy items and go and fetch the resources with reference to the complete dataset.

For example something doing something like:

load(galaxy[player.galaxy[i]].spritesheet);

But then again I would like to console log easily and find out if everything has been assigned correctly.

Right now I have to do this:

console.log(galaxies[quadrants[0].galaxies[1]].name);

Thanks!


Source: oop

Leave a Reply