There is just a little bit of code that needs to be changed in this chapter, we are going to create a controller of the game which when the player touches it, missiles will fire from the plane. In this chapter we will just show the word “Hi” on the game screen if the player pressed the controller button, in the next chapter we will create the missile factory class to manage the player missile.

The first file we are going to edit is the MainActivity class where we are going to create the controller object which consists of two part, the base and the knob. It is the position of the knob which will be used as the control area to fire the missile.

First we will create the controller object.

private void resetGameController() {
	controllerRegion = atlas.findRegion("controller");
        controller_knobRegion = atlas.findRegion("controller_knob");
    }

Next we will render the object in the draw game method.

batch.draw(controllerRegion, viewportWidth/2 - controllerRegion.getRegionWidth()/2, controller_knobRegion.getRegionHeight()/5);
batch.draw(controller_knobRegion, viewportWidth/2 - controller_knobRegion.getRegionWidth()/2, 0);

Finally, we will pass extra parameters into the constructor of the DetectTouch object which will be used to call the string builder object as well as to determine the area of the game controller.

touchadaptor = new DetectTouch(player, viewportHeight, viewportWidth, controller_knobRegion, this);

This is the revised file from the MainActivity class.

package com.gamingdirectional.lonemoon;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FitViewport;

public class MainActivity extends ApplicationAdapter  {

	SpriteBatch batch;
	OrthographicCamera camera;
	float viewportWidth;
	float viewportHeight;
	TextureRegion bgRegion;
	TextureRegion planeRegion;
	Vector2 planePosition;
	BitmapFont font;
	StringBuilder score = new StringBuilder();
	Integer point;
	TextureAtlas atlas;
	Vector2 touchPosition;
	Vector3 cameraposition = new Vector3();
	DetectTouch touchadaptor;
	Player player;
	World world;
	static final float STEP_TIME = 1f/60f;
	float accumulator = 0;
	Box2DDebugRenderer debugRenderer;
	final static int BOX2D_TOGAME_SCALE = 1;
	private FitViewport viewport;
	private Stage bj;
    private TextureRegion controllerRegion;
    private TextureRegion controller_knobRegion;

    public MainActivity(int width, int height) {
		viewportWidth = width;
		viewportHeight = height;
	}
	
	@Override
	public void create () {
		resetGame();
	}

	@Override
	public void render () {
		Gdx.gl.glClearColor(1, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		updateGame();
		drawGame();
	}

	private void drawGame() {

		updateCamera();
		batch.setProjectionMatrix(camera.combined);
		batch.begin();
		batch.draw(bgRegion, 0, 0);
        batch.draw(controllerRegion, viewportWidth/2 - controllerRegion.getRegionWidth()/2, controller_knobRegion.getRegionHeight()/5);
        batch.draw(controller_knobRegion, viewportWidth/2 - controller_knobRegion.getRegionWidth()/2, 0);
		batch.draw((TextureRegion) player.getPlayerBody().getUserData(), player.getPlayerBody().getPosition().x-planeRegion.getRegionWidth()/2, player.getPlayerBody().getPosition().y-planeRegion.getRegionHeight()/2);
		font.draw(batch, score, viewportWidth/6-font.getScaleX()/2, viewportHeight - font.getScaleY()/4);
		batch.end();
		debugRenderer.render(world, camera.combined);
	}

	private void updateCamera() {
		cameraposition.set(player.getPlayerBody().getPosition().x, player.getPlayerBody().getPosition().y,0);
		//camera.unproject(cameraposition);
		camera.update();
	}

	private void updateGame() {
		updatePosition();
		updateScore();
	}

	// step time control
	private void updatePosition() {

		float delta = Gdx.graphics.getDeltaTime();

		accumulator += Math.min(delta, 0.25f);

		if (accumulator >= STEP_TIME) {
			accumulator -= STEP_TIME;
			world.step(STEP_TIME, 6, 2);
		}
	}

	private void updateScore() {
		//Float f = player.getPlayerBody().getPosition().x;
		//score.replace(0, 1, f.toString());
	}


	/* resetting game objects, background, font, camera, and create sprite batch object */

	private void resetGame() {
		batch = new SpriteBatch();
		atlas = new TextureAtlas(Gdx.files.internal("lonemoon.txt"));
		debugRenderer = new Box2DDebugRenderer();
		resetScore();
		resetFont();
		resetPysWorld();
		resetGameController();
		resetPlane();
		resetCamera();
		resetBackground();
        Gdx.input.setInputProcessor(touchadaptor);
	}

	private void resetPysWorld() {
		world = new World(new Vector2(0, 0), false);
	}

	private void resetScore() {
		point = 0;
	}

	private void resetFont() {

		font = new BitmapFont(Gdx.files.internal("freefont.fnt"),
				Gdx.files.internal("freefont.png"), false);
	}

	private void resetCamera() {
		camera = new OrthographicCamera();
		camera.setToOrtho(false, viewportWidth, viewportHeight);
		viewport = new FitViewport(viewportWidth, viewportHeight, camera);
	}

	private void resetPlane() {
		planeRegion = atlas.findRegion("plane");//new TextureRegion(plane, plane.getWidth(), plane.getHeight());
		planePosition= new Vector2();
		touchPosition=new Vector2();
		planePosition.set((viewportWidth/2-planeRegion.getRegionWidth()/2)/BOX2D_TOGAME_SCALE, (viewportHeight/2 - planeRegion.getRegionHeight()/2)/BOX2D_TOGAME_SCALE);
		player = new Player(planePosition, world, planeRegion);
		touchadaptor = new DetectTouch(player, viewportHeight, viewportWidth, controller_knobRegion, this);
	}

	private void resetBackground() {
		bgRegion = atlas.findRegion("background0");//new TextureRegion(background, background.getWidth(), background.getHeight());
	}

	private void resetGameController() {
	    controllerRegion = atlas.findRegion("controller");
        controller_knobRegion = atlas.findRegion("controller_knob");
    }

	@Override
	public void dispose () {
		batch.dispose();
		font.dispose();
		atlas.dispose();
		world.dispose();
	}

}

Next, all we need to do is to determine whether a user has clicked on the controller area or not, if he does then we will print out the “Hi” message on the top of the screen.

 /* check to see whether the user has touched the controller knob */
        if(touchPosition.x > viewportwidth/2 - controller_knobRegion.getRegionWidth()/2 &&  touchPosition.x < viewportwidth/2 + controller_knobRegion.getRegionWidth()/2 && touchPosition.y < controller_knobRegion.getRegionHeight())
            activity.score.append("hi");

Below is the fully revised DetectTouch class file.

package com.gamingdirectional.lonemoon;

import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import java.util.ArrayList;

public class DetectTouch extends InputAdapter{

    private final float viewportwidth;
    private final MainActivity activity;
    Vector2 touchPosition;
    float viewportHeight;
    Player player;
    ArrayList<Boolean> direction;
    final static int BOX2D_TOGAME_SCALE = 1;
    TextureRegion controller_knobRegion;
    StringBuilder score;

    public DetectTouch(Player player, float viewportHeight, float viewportwidth, TextureRegion controller_knobRegion, MainActivity activity) {

        touchPosition = new Vector2();
        this.viewportHeight = viewportHeight;
        this.viewportwidth = viewportwidth;
        direction = new ArrayList<Boolean>();
        direction.add(0, true);
        direction.add(1, true);
        this.player  = player;
        this.controller_knobRegion = controller_knobRegion;
        this.activity = activity;
    }


    private void applyForce() {
        player.applyForceOnBody(direction);
    }

    public boolean touchUp (int x, int y, int pointer, int button) {
        touchPosition.set((float)x/BOX2D_TOGAME_SCALE, (viewportHeight-(float)y)/BOX2D_TOGAME_SCALE);
        calculateposition();
        applyForce();

        /* check to see whether the user has touched the controller knob */
        if(touchPosition.x > viewportwidth/2 - controller_knobRegion.getRegionWidth()/2 &&  touchPosition.x < viewportwidth/2 + controller_knobRegion.getRegionWidth()/2 && touchPosition.y < controller_knobRegion.getRegionHeight())
            activity.score.append("hi");

        return true; // return true to indicate the event was handled
    }

    public boolean touchDown (int x, int y, int pointer, int button) {

        return true;
    }

    public void calculateposition() {
        if(touchPosition.x > player.getPlayerBody().getPosition().x && touchPosition.y > player.getPlayerBody().getPosition().y) {
            direction.set(0, true);
            direction.set(1, true);
        } else if(touchPosition.x < player.getPlayerBody().getPosition().x && touchPosition.y > player.getPlayerBody().getPosition().y) {
            direction.set(0, false);
            direction.set(1, true);
        } else if(touchPosition.x < player.getPlayerBody().getPosition().x && touchPosition.y < player.getPlayerBody().getPosition().y) {
            direction.set(0, false);
            direction.set(1, false);
        } else if(touchPosition.x > player.getPlayerBody().getPosition().x && touchPosition.y < player.getPlayerBody().getPosition().y) {
            direction.set(0, true);
            direction.set(1, false);
        }
    }

}

That is it, when we touch the controller area, we will see the message as shown below.

The message “Hi” appears after we have touched the controller area

In the next chapter we will create the missile factory so it will be a missile launches from the plane instead of showing the “Hi” message on the screen every time the user touches the controller area.

Click to rate this post!
[Total: 0 Average: 0]
Please follow and like us:

3 Comments

  1. Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your
    site? My blog is in the very same area of interest as yours and my
    visitors would certainly benefit from a lot of the information you provide here.
    Please let me know if this alright with you. Appreciate it!

Leave a Reply

Your email address will not be published. Required fields are marked *