Olivia's Fireflies background

LibGDX post #3: Drawing the fireflies

August 02, 2016

This was a tough one for me.

I knew enough about Java that it made sense to create a Firefly class. Something that would let me describe a firefly, maybe declare its position, size, etc — but also so I have a way to create multiple instances of my firefly.

Firefly.java

With that, here's my Firefly.java class below. But first, a few notes.



package player;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;

public class Firefly extends Image {

    private float x = MathUtils.random(20, Gdx.graphics.getWidth() -40);
    private float  y = MathUtils.random(200, Gdx.graphics.getHeight() - 40);
    private float width = 50;
    private float height = 50;


    public Firefly(Skin skin, String drawableName) {
        super(skin, drawableName);
        this.setBounds(x, y, width, height);
    }

} // firefly

PlayForest.java

This is the main game screen, where the user attempts to drag (capture) the fireflies into a jar. Some notes about this code:



package scenes;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.DragAndDrop;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.sixfiveagency.oliviasfireflies.GameMain;

import Helpers.GameInfo;
import player.Firefly;

public class PlayForest implements Screen  {

    private GameMain game;
    private OrthographicCamera mainCamera;
    private Viewport gameViewport;
    private Sprite background;
    private Stage stage;
    private int numberCounter = 0;


    public PlayForest(GameMain game) {

        this.game = game;

        float aspectRatio = (float) Gdx.graphics.getHeight() / (float) Gdx.graphics.getWidth();
        mainCamera = new OrthographicCamera();
        mainCamera.setToOrtho(false, GameInfo.GAME_HEIGHT * aspectRatio, GameInfo.GAME_HEIGHT);
        mainCamera.position.set(GameInfo.GAME_WIDTH / 2f, GameInfo.GAME_HEIGHT / 2f, 0);
        gameViewport = new StretchViewport(GameInfo.GAME_WIDTH, GameInfo.GAME_HEIGHT, mainCamera);
        background = new Sprite(new Texture("Backgrounds/forest2.jpg"));
        background.setSize(GameInfo.GAME_WIDTH, GameInfo.GAME_HEIGHT);

    }



    public void show () {

        stage = new Stage();
        Gdx.input.setInputProcessor(stage);

        // variable that decides the number of fireflies to appear
        float fireflyCount = MathUtils.random(12, 19);

        // JAR CREATED HERE
        final Skin skin = new Skin();
        skin.add("default", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
        skin.add("jar", new Texture("jar.png"));

        Image validTargetImage = new Image(skin, "jar");
        validTargetImage.setBounds(10, 10, 140, 200);
        stage.addActor(validTargetImage);

        DragAndDrop dragAndDrop = new DragAndDrop();

        Texture texture = new Texture(
        Gdx.files.internal("4-firefly1-left-glow.png"));


        // ********************************************
        // iterate through the number of fireflies
        // we want to draw out using fireflyCount
        // ********************************************
        for (int fireflyIndex = 0; fireflyIndex < fireflyCount; fireflyIndex++) {
            // YELLOW FIREFLY HERE
            String fireflyName = "firefly" + fireflyIndex;
            skin.add(fireflyName, texture);
            final Firefly ff = new Firefly(skin, fireflyName);

            System.out.println("Fireflies objects:" + fireflyIndex);

            stage.addActor(ff);

            // MAKE EACH FIREFLY DRAGGABLE, and SET LARGER SIZE as you drag
            dragAndDrop.addSource(new DragAndDrop.Source(ff) {
                public DragAndDrop.Payload dragStart (InputEvent event, float x, float y, int pointer) {
                    DragAndDrop.Payload payload = new DragAndDrop.Payload();

                    payload.setObject("Firefly captured");
                    payload.setDragActor(ff);

                    getActor().setSize(80, 80);
                    return payload;
                }

                // IF YOU DON'T DROP FIREFLY ON TARGET, MAKE SURE IT STAYS ON STAGE
                // AND GOES BACK TO THE NORMAL SIZE
                @Override
                public void dragStop(InputEvent event, float x, float y, int pointer, DragAndDrop.Payload payload, DragAndDrop.Target target) {
                    if(target == null)
                    stage.addActor(ff);
                    getActor().setSize(50, 50);
                }
            });
        } // ***** END OF FOR LOOP ***********

        // THIS IS FOR THE TARGET ... THE JAR
        // IT TURNS YELLOW WHEN A FIREFLY GETS DRAGGED ON IT
        dragAndDrop.addTarget(new DragAndDrop.Target(validTargetImage) {
            public boolean drag (DragAndDrop.Source source, DragAndDrop.Payload payload, float x, float y, int pointer) {
            getActor().setColor(Color.YELLOW);
            return true;rn            }

            public void reset (DragAndDrop.Source source, DragAndDrop.Payload payload) {
            getActor().setColor(Color.WHITE);
        }

        // INCREMENTS A VARIABLE FOR EACH SUCCESSFUL CAPTURE
        public void drop (DragAndDrop.Source source, DragAndDrop.Payload payload, float x, float y, int pointer) {
            System.out.println("Accepted: " + payload.getObject() + " " + x + ", " + y);
            addNumber();
        }
        });

    }

    // METHOD FOR INCREMENTING VARIABLE
    public void addNumber() {
    numberCounter++;
    System.out.println("Count is now " + numberCounter);

    if (numberCounter == 10) {
    game.setScreen(new Start(game));
    }
    }

    @Overridern    public void render(float delta) {

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    game.getBatch().begin();
    game.getBatch().draw(background, 0, 0,GameInfo.GAME_WIDTH, GameInfo.GAME_HEIGHT);
    game.getBatch().end();

    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();

    }

    @Override
    public void resize(int width, int height) {
        gameViewport.update(width, height, true);
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
            background.getTexture().dispose();
            stage.dispose();
    }


} // PlayForest

The man. The myth. The legend. The man. The myth. The legend.

It's fair to say I lost quite a few evenings over the course of a week, as I couldn't figure out how to drag all of my fireflies. Creating them was easy, but making them all draggable? That was an entirely different issue.

So I brought in the big guns.

Unlike myself, Steve Rufle is an experienced programmer. Even though he's never touched a line of Android code, let alone spent any time in LibGDX — in a matter of 30 minutes he already had the first of two possible solutions. Some of that joyous code is found above.  So a little shout-out credit to my hero this week ... a few of Steve's links:

Below: Here's what PlayForest looks like, after all the fireflies populated the screen.

Random fireflies