Java tutorial
/* * Copyright (c) 2014, Miguel Angel Astor Romero * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Read the LICENSE file for more details. */ package com.gamejolt.mikykr5.ceidecpong.states; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.InputProcessor; import com.badlogic.gdx.Screen; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector3; import com.gamejolt.mikykr5.ceidecpong.GameCore; /** * Base class for all the game states. * * @author Miguel Astor */ public abstract class BaseState implements Screen, InputProcessor { /** * Class used for logging. */ private static final String CLASS_NAME = BaseState.class.getSimpleName(); /** * A {@link GameCore} instance to use it's {@link SpriteBatch} */ protected GameCore core; /** * A flag to indicate that the state is the currently active state. */ protected boolean stateEnabled; /** * A pixel perfect camera used to render all 2D assets for a concrete state instance. */ protected OrthographicCamera pixelPerfectCamera; /** * An auxiliary vector used to convert screen coordinates to world coordinates. */ protected final Vector3 win2world; /** * An auxiliary vector used to hold the unprojected world coordinates of a touch or click. */ protected final Vector2 touchPointWorldCoords; /** * Sets up all the general state fields. * * @param core The game core. * @throws IllegalArgumentException If core is null. */ public BaseState(final GameCore core) throws IllegalArgumentException { if (core == null) throw new IllegalArgumentException(CLASS_NAME + ": Core is null."); this.core = core; this.pixelPerfectCamera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); win2world = new Vector3(0.0f, 0.0f, 0.0f); touchPointWorldCoords = new Vector2(); } /*;;;;;;;;;;;;;;;;; ; STATE METHODS ; ;;;;;;;;;;;;;;;;;*/ /** * Executes a set of standard actions when the state is set by the {@link GameCore}. */ public void onStateEnabled() { stateEnabled = true; Gdx.input.setInputProcessor(this); Gdx.input.setCatchBackKey(true); Gdx.input.setCatchMenuKey(true); } /** * Executes a set of standard actions when the state is removed by the {@link GameCore}. */ public void onStateDisabled() { stateEnabled = false; Gdx.input.setInputProcessor(null); Gdx.input.setCatchBackKey(false); Gdx.input.setCatchMenuKey(false); } /*;;;;;;;;;;;;;;;;;; ; SCREEN METHODS ; ;;;;;;;;;;;;;;;;;;*/ @Override public abstract void render(float delta); @Override public abstract void dispose(); @Override public void resize(int width, int height) { this.pixelPerfectCamera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); } @Override public void show() { } @Override public void hide() { } @Override public void pause() { } @Override public void resume() { } /*;;;;;;;;;;;;;;;;;; ; HELPER METHODS ; ;;;;;;;;;;;;;;;;;;*/ /** * Converts a point in screen coordinates to world coordinates inside the view of this state's {@link BaseState#pixelPerfectCamera}. * The result is stored in the {@link BaseState#touchPointWorldCoords} vector. * * @param screenX The x coordinate in window space. * @param screenY The y coordinate in window space. */ protected void unprojectTouch(int screenX, int screenY) { win2world.set(screenX, screenY, 0.0f); pixelPerfectCamera.unproject(win2world); touchPointWorldCoords.set(win2world.x, win2world.y); } /*;;;;;;;;;;;;;;;;;;;;;;;;;;; ; INPUT PROCESSOR METHODS ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;*/ @Override public boolean keyDown(int keycode) { return false; }; @Override public boolean keyUp(int keycode) { return false; }; @Override public boolean keyTyped(char character) { return false; }; @Override public boolean touchDown(int screenX, int screenY, int pointer, int button) { return false; }; @Override public boolean touchUp(int screenX, int screenY, int pointer, int button) { return false; }; @Override public boolean touchDragged(int screenX, int screenY, int pointer) { return false; }; @Override public boolean mouseMoved(int screenX, int screenY) { return false; }; @Override public boolean scrolled(int amount) { return false; }; }