Android Open Source - Avoidance Menu State






From Project

Back to project page Avoidance.

License

The source code is released under:

GNU General Public License

If you think the Android project Avoidance listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/* 
 * Copyright (c) 2012 Florian Minges/* ww  w.j  a v a 2  s  . c om*/
 * 
 * Parts of this file are derived from a menu example, written by Nicholas Gramlich, which
 * is released under the GNU Lesser GPL. It can be found under the following link:
 * http://code.google.com/p/andengineexamples/source/browse/src/org/anddev/andengine/
 *     examples/MenuExample.java
 * 
 * This file is part of Avoidance.
 * 
 * Avoidance is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Avoidance is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Avoidance.  If not, see <http://www.gnu.org/licenses/>. 
 *  
 */

package se.chalmers.avoidance.core.states;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Map;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.scene.background.SpriteBackground;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import se.chalmers.avoidance.constants.EventMessageConstants;
import se.chalmers.avoidance.constants.FileConstants;
import se.chalmers.avoidance.util.ScreenResolution;
import android.opengl.GLES20;

/**
 * Class containing information and data about the menu state.
 * 
 * @author Florian Minges
 */
public class MenuState implements IState, IOnMenuItemClickListener {

  private static final int MENU_START = 0;
  private static final int MENU_HIGHSCORES = 1;
  private static final int MENU_CREDITS = 2;
  private static final int MENU_QUIT = 3;

  private PropertyChangeSupport pcs;
  private MenuScene menuScene;

  /**
   * Constructs a new <code>MenuState</code>.
   * <p>
   * 
   * @param camera
   *            the game engines <code>Camera</code>
   * @param regions
   *            a <code>Map</code> containing loaded textures/regions
   * @param vbom
   *            the game engines <code>VertexBufferObjectManager</code>
   */
  public MenuState(Camera camera, Map<String, TextureRegion> regions,
      VertexBufferObjectManager vbom) {
    initialize(camera, regions, vbom);
    pcs = new PropertyChangeSupport(this);
  }

  /**
   * Initializes the menu scene.
   * 
   * @param camera
   *            the game engines <code>Camera</code>
   * @param regions
   *            a <code>Map</code> containing loaded textures/regions
   * @param vbom
   *            the game engines <code>VertexBufferObjectManager</code>
   */
  private void initialize(Camera camera, Map<String, TextureRegion> regions,
      VertexBufferObjectManager vbom) {
    menuScene = new MenuScene();
    menuScene.setCamera(camera);
    menuScene.setBackground(new Background(0f, 0f, 0f));

    // center the scene
    float xPos = ScreenResolution.getWidthResolution() / 2 - 250;
    float yPos = ScreenResolution.getHeightResolution() / 2 - 300;

    // create menu items
    final SpriteMenuItem startMenuItem = new SpriteMenuItem(MENU_START,
        regions.get(FileConstants.IMG_MENU_START), vbom);
    startMenuItem.setBlendFunction(GLES20.GL_SRC_ALPHA,
        GLES20.GL_ONE_MINUS_SRC_ALPHA);
    this.menuScene.addMenuItem(startMenuItem);
    startMenuItem.setPosition(xPos, yPos);

    final SpriteMenuItem highscoreMenuItem = new SpriteMenuItem(
        MENU_HIGHSCORES,
        regions.get(FileConstants.IMG_MENU_HIGH_SCORE), vbom);
    highscoreMenuItem.setBlendFunction(GLES20.GL_SRC_ALPHA,
        GLES20.GL_ONE_MINUS_SRC_ALPHA);
    this.menuScene.addMenuItem(highscoreMenuItem);
    highscoreMenuItem.setPosition(xPos, yPos + 150);

    final SpriteMenuItem creditsMenuItem = new SpriteMenuItem(MENU_CREDITS,
        regions.get(FileConstants.IMG_MENU_CREDITS), vbom);
    creditsMenuItem.setBlendFunction(GLES20.GL_SRC_ALPHA,
        GLES20.GL_ONE_MINUS_SRC_ALPHA);
    this.menuScene.addMenuItem(creditsMenuItem);
    creditsMenuItem.setPosition(xPos, yPos + 300);

    final SpriteMenuItem quitMenuItem = new SpriteMenuItem(MENU_QUIT,
        regions.get(FileConstants.IMG_MENU_QUIT), vbom);
    quitMenuItem.setBlendFunction(GLES20.GL_SRC_ALPHA,
        GLES20.GL_ONE_MINUS_SRC_ALPHA);
    this.menuScene.addMenuItem(quitMenuItem);
    quitMenuItem.setPosition(xPos, yPos + 450);

    final Sprite backgroundSprite = new Sprite(0, 0, 1024, 768,
        regions.get(FileConstants.IMG_MENU_BACKGROUND), vbom);
    this.menuScene.setBackground(new SpriteBackground(backgroundSprite));

    // this.menuScene.buildAnimations(); <- does not work
    this.menuScene.setBackgroundEnabled(true);
    this.menuScene.setOnMenuItemClickListener(this);

  }

  public void update(float tpf) {

  }

  /**
   * Returns the menu scene, or null if it is not instantiated yet.
   * 
   * @return the menu scene
   */
  public Scene getScene() {
    return menuScene;
  }

  /**
   * {@inheritDoc}
   */
  public boolean onMenuItemClicked(final MenuScene pMenuScene,
      final IMenuItem pMenuItem, final float pMenuItemLocalX,
      final float pMenuItemLocalY) {
    switch (pMenuItem.getID()) {
    case MENU_START:
      pcs.firePropertyChange(EventMessageConstants.CHANGE_STATE,
          StateID.Menu, StateID.Game);
      return true;
    case MENU_HIGHSCORES:
      pcs.firePropertyChange(EventMessageConstants.CHANGE_STATE,
          StateID.Menu, StateID.Highscore);
      return true;
    case MENU_CREDITS:
      pcs.firePropertyChange(EventMessageConstants.CHANGE_STATE,
          StateID.Menu, StateID.Credits);
      return true;
    case MENU_QUIT:
      /* End Activity. */
      pcs.firePropertyChange(EventMessageConstants.QUIT_GAME, null, true);
      return true;
    default:
      return false;
    }
  }

  /**
   * Adds a listener to this state.
   * 
   * @param pcl
   *            the listener to add
   */
  public void addPropertyChangeListener(PropertyChangeListener pcl) {
    pcs.addPropertyChangeListener(pcl);
  }

  /**
   * Removes a listener from this state.
   * 
   * @param pcl
   *            the listener to remove
   */
  public void removePropertyChangeListener(PropertyChangeListener pcl) {
    pcs.removePropertyChangeListener(pcl);
  }
}




Java Source Code List

se.chalmers.avoidance.MainActivity.java
se.chalmers.avoidance.constants.EventMessageConstants.java
se.chalmers.avoidance.constants.FileConstants.java
se.chalmers.avoidance.constants.FontConstants.java
se.chalmers.avoidance.constants.GameConstants.java
se.chalmers.avoidance.core.EntityFactory.java
se.chalmers.avoidance.core.collisionhandlers.CollisionHandler.java
se.chalmers.avoidance.core.collisionhandlers.EnemyCollisionHandler.java
se.chalmers.avoidance.core.collisionhandlers.GameOverNotifier.java
se.chalmers.avoidance.core.collisionhandlers.KillplayerobstacleCollisionHandler.java
se.chalmers.avoidance.core.collisionhandlers.PitobstacleCollisionHandler.java
se.chalmers.avoidance.core.collisionhandlers.PowerUpCollisionHandler.java
se.chalmers.avoidance.core.collisionhandlers.WallCollisionHandler.java
se.chalmers.avoidance.core.components.Acceleration.java
se.chalmers.avoidance.core.components.Buff.java
se.chalmers.avoidance.core.components.Friction.java
se.chalmers.avoidance.core.components.Immortal.java
se.chalmers.avoidance.core.components.Jump.java
se.chalmers.avoidance.core.components.Score.java
se.chalmers.avoidance.core.components.Size.java
se.chalmers.avoidance.core.components.Sound.java
se.chalmers.avoidance.core.components.Spatial.java
se.chalmers.avoidance.core.components.Time.java
se.chalmers.avoidance.core.components.Transform.java
se.chalmers.avoidance.core.components.Velocity.java
se.chalmers.avoidance.core.states.GameOverScene.java
se.chalmers.avoidance.core.states.GameState.java
se.chalmers.avoidance.core.states.HighScoreState.java
se.chalmers.avoidance.core.states.IState.java
se.chalmers.avoidance.core.states.MenuState.java
se.chalmers.avoidance.core.states.StateID.java
se.chalmers.avoidance.core.states.StateManager.java
se.chalmers.avoidance.core.systems.CollisionSystem.java
se.chalmers.avoidance.core.systems.EnemyControlSystem.java
se.chalmers.avoidance.core.systems.HudRenderSystem.java
se.chalmers.avoidance.core.systems.PlayerControlSystem.java
se.chalmers.avoidance.core.systems.SoundSystem.java
se.chalmers.avoidance.core.systems.SpatialRenderSystem.java
se.chalmers.avoidance.core.systems.SpawnSystem.java
se.chalmers.avoidance.input.AccelerometerListener.java
se.chalmers.avoidance.input.TouchListener.java
se.chalmers.avoidance.util.AudioManager.java
se.chalmers.avoidance.util.FileUtils.java
se.chalmers.avoidance.util.ScreenResolution.java
se.chalmers.avoidance.util.Utils.java