All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kauridev.lunarbase.AbstractGameView Maven / Gradle / Ivy

Go to download

Lunar Base is a simple game framework written in Java. It is written using an entity-component-system architecture.

The newest version!
/*
 * This file is part of the lunar-base package.
 *
 * Copyright (c) 2014 Eric Fritz
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.kauridev.lunarbase;

/**
 * A base implementation for views.
 *
 * @author Eric Fritz
 */
abstract public class AbstractGameView implements GameView
{
	/**
	 * View visibility and transition states.
	 */
	public enum ViewState
	{
		Active,
		Hidden,
		TransitionOn,
		TransitionOff
	}

	/**
	 * The view manager.
	 */
	private GameViewManager manager;

	/**
	 * The current view transition state.
	 */
	private ViewState state = ViewState.TransitionOn;

	/**
	 * Whether the view is currently transitioning off.
	 */
	private boolean isExiting = false;

	/**
	 * Whether the view is an overlay.
	 */
	private boolean isOverlay = false;

	/**
	 * The current transition position.
	 */
	private double transitionPosition;

	/**
	 * Indicates how long the view takes to transition on when it is created.
	 */
	private long transitionOnTime = 0;

	/**
	 * Indicates how long the view takes to transition off when it is deactivated.
	 */
	private long transitionOffTime = 0;

	/**
	 * Creates a new AbstractGameView.
	 *
	 * @param manager The view manager.
	 */
	public AbstractGameView(GameViewManager manager) {
		this.manager = manager;
	}

	/**
	 * Returns the view manager.
	 *
	 * @return The view manager.
	 */
	public GameViewManager getManager() {
		return manager;
	}

	/**
	 * Returns the current view transition state.
	 *
	 * @return The current view transition state.
	 */
	public ViewState getState() {
		return state;
	}

	/**
	 * There are two possible reasons why a view might be transitioning off. It could be temporarily
	 * going away to make room for another view that is on top of it, or it could be going away for
	 * good. This property indicates whether the view is exiting for real: if set, the view will
	 * automatically remove itself as soon as the transition finishes.
	 *
	 * @return Whether the view is transition off.
	 */
	public boolean isExiting() {
		return isExiting;
	}

	/**
	 * Gets the current position of the view transition, ranging from zero (fully active, no
	 * transition) to one (transitioned fully off to nothing).
	 *
	 * @return Current state of transition.
	 */
	public double getTransitionPosition() {
		return transitionPosition;
	}

	/**
	 * Gets how long the view takes to transition on when it is created.
	 *
	 * @return How long the view takes to transition on.
	 */
	public long getTransitionOnTime() {
		return transitionOnTime;
	}

	/**
	 * Sets how long the view takes to transition on when it is created.
	 *
	 * @param transitionOnTime How long the view takes to transition on.
	 */
	public void setTransitionOnTime(long transitionOnTime) {
		this.transitionOnTime = transitionOnTime;
	}

	/**
	 * Gets how long the view takes to transition off when it is deactivated.
	 *
	 * @return How long the view takes to transition off.
	 */
	public long getTransitionOffTime() {
		return transitionOffTime;
	}

	/**
	 * Sets how long the view takes to transition off when it is deactivated.
	 *
	 * @param transitionOffTime How long the view takes to transition off.
	 */
	public void setTransitionOffTime(long transitionOffTime) {
		this.transitionOffTime = transitionOffTime;
	}

	/**
	 * Tells the view to begin exiting. After the specified transition off time, the view will
	 * remove itself from its manager. This allows the view to gracefully transition off. (See
	 * {@link #update(double, boolean)} for implementation details).
	 */
	public void beginExiting() {
		isExiting = true;
	}

	@Override
	public void update(double elapsed, boolean hasFocus) {
		if (isExiting) {
			state = ViewState.TransitionOff;

			if (updateTransition(elapsed, transitionOffTime, -1)) {
				manager.removeView(this);
				exit();
			}
		} else {
			if (!hasFocus) {
				if (updateTransition(elapsed, transitionOffTime, -1)) {
					state = ViewState.Hidden;
				} else {
					state = ViewState.TransitionOff;
				}
			} else {
				if (updateTransition(elapsed, transitionOnTime, 1)) {
					state = ViewState.Active;
				} else {
					state = ViewState.TransitionOn;
				}

				update(elapsed);
			}
		}
	}

	/**
	 * Called when it has been determined that game logic needs to be processed.
	 *
	 * @param elapsed The number of milliseconds elapsed since the last call to update.
	 */
	abstract public void update(double elapsed);

	/**
	 * Helper for updating the view transition position.
	 *
	 * @param elapsed   The number of milliseconds passed since the last call to update.
	 * @param time      The total transition time (off or on).
	 * @param direction The direction to move the transition.
	 *
	 * @return true if the transition has completed, false otherwise.
	 */
	private boolean updateTransition(double elapsed, long time, int direction) {
		transitionPosition += direction * (time == 0 ? 1 : elapsed / time);

		if (transitionPosition < 0 || transitionPosition > 1) {
			transitionPosition = Math.max(0, Math.min(1, transitionPosition));
			return true;
		}

		return false;
	}

	@Override
	public boolean isOverlay() {
		return isOverlay;
	}

	/**
	 * Sets whether this view is an overlay.
	 *
	 * @param isOverlay Whether the view is an overlay.
	 */
	public void setOverlay(boolean isOverlay) {
		this.isOverlay = isOverlay;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy