Android Open Source - Gloomy-Dungeons-3D Game Activity






From Project

Back to project page Gloomy-Dungeons-3D.

License

The source code is released under:

MIT License

If you think the Android project Gloomy-Dungeons-3D 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

package zame.game;
//from w w  w. ja  va2  s .c o m
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Surface;
import android.view.View;
import android.widget.EditText;
import zame.game.engine.Controls;
import zame.game.engine.Game;
import zame.game.views.GameView;
import zame.game.views.IZameView;

public class GameActivity extends Activity implements SensorEventListener
{
  private static final int DIALOG_ENTER_CODE = 1;
  private static final int REQUEST_CODE_PREFERENCES = 1;

  private static final int MENU_ITEM_CODE = 1;
  private static final int MENU_ITEM_OPTIONS = 2;
  private static final int MENU_ITEM_MENU = 3;

  public static final int ACTION_RELOAD_LEVEL = 1;
  public static final int ACTION_REINITIALIZE = 2;
  public static final int ACTION_LOAD_AUTOSAVE = 3;

  public static GameActivity self;

  private View currentView;
  private int currentLayoutResId;
  private final Handler handler = new Handler();
  private SensorManager sensorManager;
  private Sensor accelerometer;
  private int deviceRotation = 0;
  private boolean justAfterPause = false;
  private boolean soundAlreadyStopped = false; // fix multi-activity issues
  private GameActivityZeemoteHelper zeemoteHelper = null;

  public boolean instantMusicPause = true;
  public GameView.Data gameViewData;

  public static void changeView(int viewId)
  {
    changeView(viewId, 0);
  }

  public static void changeView(int viewId, int additionalAction)
  {
    if (GameActivity.self == null) {
      return;
    }

    final int _viewId = viewId;
    final int _additionalAction = additionalAction;

    GameActivity.self.handler.post(new Runnable() {
      public void run() {
        switch (_additionalAction) {
          case ACTION_RELOAD_LEVEL:
            GameActivity.self.gameViewData.noClearRenderBlackScreenOnce = true;
            break;

          case ACTION_REINITIALIZE:
            MenuActivity.justLoaded = true;
            Game.savedGameParam = "";
            GameActivity.self.gameViewData.game.initialize();
            GameActivity.self.gameViewData.noClearRenderBlackScreenOnce = true;
            break;

          case ACTION_LOAD_AUTOSAVE:
            MenuActivity.justLoaded = true;
            Game.savedGameParam = Game.AUTOSAVE_NAME;
            GameActivity.self.gameViewData.game.initialize();
            // gameViewData.noClearRenderBlackScreenOnce = true;
            break;
        }

        GameActivity.self.setZameView(_viewId);

        if (_additionalAction == ACTION_RELOAD_LEVEL) {
          Game.loadLevel(Game.LOAD_LEVEL_RELOAD);
        }
      }
    });
  }

  @Override
  protected void onCreate(Bundle state)
  {
    super.onCreate(state);
    self = this;

    if (BuildConfig.WITH_ZEEMOTE) {
      zeemoteHelper = new GameActivityZeemoteHelper();
    }

    SoundManager.init(getApplicationContext(), getAssets(), true);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);

    gameViewData = new GameView.Data(getResources(), getAssets());

    currentView = null;
    currentLayoutResId = -1;
    setZameView(R.layout.game);
  }

  public void setZameView(int layoutResId)
  {
    if (currentLayoutResId == layoutResId) {
      return;
    }

    if ((currentView != null) && (currentView instanceof IZameView)) {
      ((IZameView)currentView).onPause();
    }

    currentLayoutResId = layoutResId;
    setContentView(layoutResId);
    currentView = findViewById(R.id.RootZameView);

    if ((currentView != null) && (currentView instanceof IZameView)) {
      ((IZameView)currentView).onResume();
    } else {
      Log.w(Common.LOG_KEY, "GameActivity.setZameView: non-IZameView view");
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu)
  {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(zeemoteHelper == null ? R.menu.game : zeemoteHelper.getMenuResId(), menu);
    return true;
  }

  @Override
  public boolean onPrepareOptionsMenu(Menu menu)
  {
    if (zeemoteHelper != null) {
      zeemoteHelper.onPrepareOptionsMenu(menu);
    }

    return super.onPrepareOptionsMenu(menu);
  }

  @SuppressWarnings("deprecation")
  @Override
  public boolean onOptionsItemSelected(MenuItem item)
  {
    switch (item.getItemId())
    {
      case R.id.menu_code:
        showDialog(DIALOG_ENTER_CODE);
        return true;

      case R.id.menu_options:
        startActivity(new Intent(GameActivity.this, GamePreferencesActivity.class));
        return true;

      case R.id.menu_menu:
        instantMusicPause = false;
        finish();
        return true;
    }

    if (zeemoteHelper != null) {
      return zeemoteHelper.onOptionsItemSelected(item);
    } else {
      return false;
    }
  }

  @Override
  public void onBackPressed()
  {
    instantMusicPause = false;
    super.onBackPressed();
  }

  @SuppressWarnings("deprecation")
  @Override
  protected void onStart()
  {
    super.onStart();
    Config.initialize();

    if (zeemoteHelper != null) {
      zeemoteHelper.onStart(this);
    }

    if (Config.accelerometerEnabled) {
      sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
      accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

      // documentation says that getOrientation() is deprecated, and we must use getRotation instead()
      // but getRotation() available only for >= 2.2
      // if we look for getRotation() into android sources, we found nice piece of code:
      // public int getRotation() { return getOrientation(); }
      // so it should be safe to use getOrientation() instead of getRotation()
      deviceRotation = getWindowManager().getDefaultDisplay().getOrientation();
    } else {
      sensorManager = null;
      accelerometer = null;
    }

    SoundManager.setPlaylist(SoundManager.LIST_MAIN);
  }

  @Override
  public void onWindowFocusChanged(boolean hasFocus)
  {
    super.onWindowFocusChanged(hasFocus);

    if (hasFocus) {
      if (!justAfterPause) {
        SoundManager.ensurePlaylist();
        SoundManager.onStart();
        soundAlreadyStopped = false;
      }
    } else {
      if (!soundAlreadyStopped) {
        SoundManager.onPause(instantMusicPause);
        soundAlreadyStopped = true;
      }

      instantMusicPause = true;
    }
  }

  @Override
  protected void onResume()
  {
    super.onResume();
    justAfterPause = false;

    if (Config.accelerometerEnabled && (sensorManager != null)) {
      sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    }

    if ((currentView != null) && (currentView instanceof IZameView)) {
      ((IZameView)currentView).onResume();
    }
  }

  @Override
  protected void onPause()
  {
    super.onPause();
    justAfterPause = true;

    if (zeemoteHelper != null) {
      zeemoteHelper.onPause();
    }

    if (!soundAlreadyStopped) {
      SoundManager.onPause(instantMusicPause);
      soundAlreadyStopped = true;
    }

    instantMusicPause = true;

    if ((currentView != null) && (currentView instanceof IZameView)) {
      ((IZameView)currentView).onPause();
    }

    if (Config.accelerometerEnabled && (sensorManager != null)) {
      sensorManager.unregisterListener(this);
    }

    ZameApplication.flushEvents();
  }

  @SuppressWarnings("deprecation")
  @Override
  protected Dialog onCreateDialog(int id)
  {
    switch (id)
    {
      case DIALOG_ENTER_CODE: {
        final View codeDialogView = LayoutInflater.from(GameActivity.this).inflate(R.layout.code_dialog, null);

        return new AlertDialog.Builder(GameActivity.this)
          .setIcon(R.drawable.ic_dialog_alert)
          .setTitle(R.string.dlg_enter_code)
          .setView(codeDialogView)
          .setPositiveButton(R.string.dlg_ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
              EditText inp = (EditText)codeDialogView.findViewById(R.id.CodeText);
              gameViewData.game.setGameCode(inp.getText().toString());
              inp.setText("");
            }
          })
          .setNegativeButton(R.string.dlg_cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            }
          })
          .create();
      }
    }

    return null;
  }

  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy)
  {
  }

  @Override
  public void onSensorChanged(SensorEvent e)
  {
    float sensorX;
    float sensorY;

    if (Config.accelerometerEnabled && (e.sensor.getType() == Sensor.TYPE_ACCELEROMETER)) {
      switch (deviceRotation) {
        case Surface.ROTATION_90:
          sensorX = e.values[1];
          sensorY = -e.values[0];
          break;

        case Surface.ROTATION_180:
          sensorX = -e.values[0];
          sensorY = -e.values[1];
          break;

        case Surface.ROTATION_270:
          sensorX = -e.values[1];
          sensorY = e.values[0];
          break;

        default:
          sensorX = e.values[0];
          sensorY = e.values[1];
          break;
      }

      Controls.accelerometerX = sensorX / SensorManager.GRAVITY_EARTH;
      Controls.accelerometerY = sensorY / SensorManager.GRAVITY_EARTH;

      if (Config.rotateScreen) {
        Controls.accelerometerX = -Controls.accelerometerX;
        Controls.accelerometerY = -Controls.accelerometerY;
      }
    }
  }
}




Java Source Code List

zame.game.AppConfig.java
zame.game.AppConfig.java
zame.game.Common.java
zame.game.ConfigZeemote.java
zame.game.ConfigZeemote.java
zame.game.Config.java
zame.game.GameActivityZeemoteHelper.java
zame.game.GameActivityZeemoteHelper.java
zame.game.GameActivity.java
zame.game.GamePreferencesActivity.java
zame.game.GamePreferencesActivity.java
zame.game.MenuActivityHelper.java
zame.game.MenuActivityHelper.java
zame.game.MenuActivity.java
zame.game.Renderer.java
zame.game.SoundManager.java
zame.game.ZameApplicationAnalyticsHelper.java
zame.game.ZameApplicationAnalyticsHelper.java
zame.game.ZameApplication.java
zame.game.ZameGame.java
zame.game.ZameJniRenderer.java
zame.game.engine.Action.java
zame.game.engine.AutoWall.java
zame.game.engine.Controls.java
zame.game.engine.Door.java
zame.game.engine.GameHelper.java
zame.game.engine.GameHelper.java
zame.game.engine.Game.java
zame.game.engine.Labels.java
zame.game.engine.LevelConfig.java
zame.game.engine.LevelRenderer.java
zame.game.engine.Level.java
zame.game.engine.Mark.java
zame.game.engine.Monster.java
zame.game.engine.Overlay.java
zame.game.engine.PortalTracer.java
zame.game.engine.State.java
zame.game.engine.Stats.java
zame.game.engine.TextureLoader.java
zame.game.engine.Weapons.java
zame.game.views.EndLevelView.java
zame.game.views.GameOverView.java
zame.game.views.GameView.java
zame.game.views.IZameView.java
zame.game.views.MenuViewHelper.java
zame.game.views.MenuViewHelper.java
zame.game.views.MenuView.java
zame.game.views.PreLevelView.java
zame.game.views.ZameGameView.java
zame.libs.FrameLayout.java
zame.libs.GLSurfaceView21.java
zame.libs.Grid.java
zame.libs.KeyMapPreference.java
zame.libs.LabelMaker.java
zame.libs.ListPreference.java
zame.libs.NumericSprite.java
zame.libs.SeekBarPreference.java
zame.promo.PromoView.java