JavaDevMatt.pl

Programista na emigracji… aktualnie po emigracji. ;)

LibGDX – Refactor i nowa grafika

W tym odcinku posprzątaliśmy trochę klasę GameplayScreen i dodaliśmy nową grafikę.

Kod źródłowy lekcji: https://github.com/JavaDevMatt/tutorialclicker/tree/tut8

 

Klasa GameplayScreen

package pl.javadevmatt.tutorialclicker.screens;

import com.badlogic.gdx.graphics.Texture;

import pl.javadevmatt.tutorialclicker.TutorialClickerGame;
import pl.javadevmatt.tutorialclicker.entities.Player;
import pl.javadevmatt.tutorialclicker.ui.IClickCallback;
import pl.javadevmatt.tutorialclicker.ui.PlayerButton;
import pl.javadevmatt.tutorialclicker.ui.ResetScoreButton;
import pl.javadevmatt.tutorialclicker.ui.ScoreLabel;

public class GameplayScreen extends AbstractScreen{
	
	private Texture bgTexture;
	private Player player;
	private PlayerButton playerButton;
	private ResetScoreButton resetScoreButton;
	private ScoreLabel scoreLabel;

	public GameplayScreen(TutorialClickerGame game) {
		super(game);
	}
	
	@Override
	protected void init() {
		bgTexture = new Texture("bg.png");
		initPlayer();
		initPlayerButton();
		initResetScoreButton();
		initScoreLabel();
	}
	
	private void initResetScoreButton() {
		resetScoreButton = new ResetScoreButton(new IClickCallback() {
			
			@Override
			public void onClick() {
				game.resetGameScore();
			}
		});
		
		stage.addActor(resetScoreButton);
	}

	private void initScoreLabel() {
		scoreLabel = new ScoreLabel();
		stage.addActor(scoreLabel);
	}

	private void initPlayerButton() {
		playerButton = new PlayerButton(new IClickCallback() {
			@Override
			public void onClick() {
				player.reactOnClick();
				game.addPoint();
			}
		});
		
		stage.addActor(playerButton);
	}

	private void initPlayer() {
		player = new Player();
		stage.addActor(player);
	}
	
	@Override
	public void render(float delta) {
		super.render(delta);
		update();
		
		spriteBatch.begin();
		spriteBatch.draw(bgTexture, 0, 0);
		spriteBatch.end();
		
		spriteBatch.begin();
		stage.draw();
		spriteBatch.end();
		
	}

	private void update() {
		scoreLabel.setText("Score: " + game.getPoints());
		stage.act();
	}


	

}

IClickCallback

package pl.javadevmatt.tutorialclicker.ui;

public interface IClickCallback {
	void onClick();
}

PlayerButton

package pl.javadevmatt.tutorialclicker.ui;

import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class PlayerButton extends Button{
	
	public PlayerButton(final IClickCallback callback){
		super(new ButtonStyle());
		
		init(callback);
	}

	private void init(final IClickCallback callback) {
		this.setWidth(460);
		this.setHeight(360);
		this.setX(10);
		this.setY(170);
		
		this.addListener(new ClickListener(){
			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				
				callback.onClick();
				
				return super.touchDown(event, x, y, pointer, button);
			}
		});
	}

}

ResetScoreButton

package pl.javadevmatt.tutorialclicker.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class ResetScoreButton extends Button{
	
	public ResetScoreButton(final IClickCallback callback){
		super(prepareResetButtonStyle());
		
		init(callback);
	}

	private void init(final IClickCallback callback) {
		this.setWidth(100);
		this.setHeight(100);
		this.setX(330);
		this.setY(560);
		
		this.addListener(new ClickListener(){
			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				callback.onClick();
				return super.touchDown(event, x, y, pointer, button);
			}
		});
	}
	
	private static ButtonStyle prepareResetButtonStyle() {
		TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("ui-red.atlas"));
		Skin skin = new Skin(atlas);
		ButtonStyle buttonStyle = new ButtonStyle();
		buttonStyle.up = skin.getDrawable("button_02");
		buttonStyle.down = skin.getDrawable("button_03");
		
		return buttonStyle;
	} 

}

ScoreLabel

package pl.javadevmatt.tutorialclicker.ui;

import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.ui.Label;

public class ScoreLabel extends Label{
	
	public ScoreLabel(){
		super("", prepareLabelStyle());
		init();
	}

	private void init() {
		this.setX(70);
		this.setY(650);
	}

	private static LabelStyle prepareLabelStyle() {
		LabelStyle labelStyle = new LabelStyle();
		labelStyle.font = new BitmapFont();
		return labelStyle;
	}
}

banerjd