Hey guys, first post. I’m developing a game using LibGDX and it is going quite well!
I took Java in college, but my previous game dev experience is with C/C++ (and a library called Allegro).
My game is similar to the star stage of Spore, but it is just a wide open star field. Stars are generated randomly as the player moves into new space using a predictive algorithm so that the same stars are always generated.
The problem I have is with this tremendously ugly piece of code that is central to my game:
StarMap_block[][] blocks;
Stage stage;
public void generateBlocks() {
short center_x = gameData.player_x;
short center_y = gameData.player_y;
short one = (short)(1);
stage.clear(); // scene2d stage, clear them out and add in the new stars
blocks = new StarMap_block[][] {
{
new StarMap_block((short)(center_x-one),(short)(center_y+one), stage),
new StarMap_block(center_x,(short)(center_y+one), stage),
new StarMap_block((short)(center_x+one),(short)(center_y+one), stage)
},
{
new StarMap_block((short)(center_x-one),center_y, stage),
new StarMap_block(center_x,center_y, stage),
new StarMap_block((short)(center_x+one),center_y, stage)
},
{
new StarMap_block((short)(center_x-one),(short)(center_y-one), stage),
new StarMap_block((short)(center_x),(short)(center_y-one), stage),
new StarMap_block((short)(center_x+one),(short)(center_y-one), stage)
}
};
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
blocks[i][j].adjust(j * 480, i * 480);
}
}
}
The 9 blocks make up a 1440x1440 pixel “active map”. Each StarMap_block contains a node list of stars, which are added to the stage.
This function is called each time the camera moves out of the current active blocks.
What is a better approach to accomplish this? Is this leaking memory? Is this actually not a bad way to do what I need to? Do I honestly need to constantly cast shorts like that in Java?
I’m at a point where I need to add more into this code, but am looking for some feedback before I shoot myself in the foot.
Thanks for any help.
[edit]
I posted a little demo video if anyone is interested in checking it out:
WLm_PD201Lk