Skip to content

Star Background Class

Tom edited this page Oct 17, 2023 · 1 revision

As there were multiple instances where people wanted to create a space-like background for screens, I generalised the animated star background from the space navigation screen into a class that can be used by any screen. This class can be found in the com.csse3200.game.components.backgrounds package.

Understanding the StarBackground Class

The lifecycle of the StarBackground class is roughly as follows:

  • Instantiate a new StarBackground with the number of stars you want in your game.
  • It uses the LibGDX library to create animations for the stars.
  • Stars are instantiated to start at a random location on the game screen.
  • The starShift method can be overridden to position stars according to the game's requirements.
  • The actor's actions are controlled by the act method which updates the animation state times of the stars.
  • The draw method of the actor is used to draw the star animatons on the game's screen.
  • The dispose method is implemented to clear up the resources used once the actor's lifecycle ends.

The background is implemented as a LibGDX actor, so it's usage is the same as any other actor's:

StarBackground starBackground = new StarBackground(100); // Create a starBackground with 100 stars
stage.addActor(animatedBackground); // Add the actor to the stage in the screen

Modifying the star background

In some cases, you may want to modify some aspects of the star background. This is done by creating a child class of the StarBackground and overwriting methods as necessary. Here is an example in which the layout of the stars as well as the background color of the screen is being overwritten for the space screen:

public class NavigationBackground extends StarBackground {

    /**
     * The texture for the space background of the navigation screen.
     */
    private final Texture spaceBackground;

    public NavigationBackground() {
        super(200);
        spaceBackground = new Texture(Gdx.files.internal("images/navigationmap/background.png"));
    }

    @Override
    protected Vector2 starShift(int x, int y){
        // Shift star around regions of no go space (bounding boxes)
        while ((x > Gdx.graphics.getWidth() / 4 && // Planets box
                x < 3 * (Gdx.graphics.getWidth() / 4) &&
                y > 2 * Gdx.graphics.getHeight() / 6 &&
                y < 3 * (Gdx.graphics.getHeight() / 6)) ||
               (x > 2 * (Gdx.graphics.getWidth() / 6) && // Title box
                x < 4 * (Gdx.graphics.getWidth() / 6) &&
                y > 4 * (Gdx.graphics.getHeight() / 6) &&
                y < 5 * (Gdx.graphics.getHeight() / 6)
                )) {
            x = MathUtils.random(0, Gdx.graphics.getWidth());
            y = MathUtils.random(0, Gdx.graphics.getHeight());
        }
        return new Vector2(x, y);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        batch.draw(spaceBackground, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        super.draw(batch, parentAlpha);
    }
}

And then it can be used the same as the default StarBackground:

StarBackground navBackground = new NavigationBackground(420); // Create a starBackground with 420 stars
stage.addActor(navBackground); // Add the actor to the stage in the screen
Clone this wiki locally