Android Open Source - DivisionByZero Game Updater






From Project

Back to project page DivisionByZero.

License

The source code is released under:

Apache License

If you think the Android project DivisionByZero 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 com.ggstudios.divisionbyzero;
/*from   ww w. jav  a2 s.  com*/
import com.ggstudios.utils.DebugLog;

import android.os.SystemClock;

public class GameUpdater extends Thread {
  private static final String TAG = "GameUpdater";

  private Object pauseLock = new Object();
  private boolean paused = false;

  private long lastTime;

  private UpdatableCollection gameUpdatables = new UpdatableCollection();
  private UpdatableCollection miscUpdatables = new UpdatableCollection();
  private boolean inGame = true;
  
  private float timeMultiplier = 1.0f;
  // min. time elapsed since an update and max...
  private int minUpdate = 12, maxUpdate = 16;

  private boolean cinematicPlaying = false;
  private Updatable cinematic = null;
  
  private Object stopLock = new Object();
  
  public GameUpdater() { }

  public void pause() {
    DebugLog.d(TAG, "pause");
    synchronized(pauseLock){
      paused = true;
    }
  }

  public void unpause() {
    DebugLog.d(TAG, "unpause");
    synchronized(pauseLock){
      paused = false;
      pauseLock.notifyAll();
    }
  }

  public void addGameUpdatable(Updatable item) {
    gameUpdatables.add(item);
  }
  
  public void clearGameUpdatables() {
    gameUpdatables.clear();
  }
  
  public void removeGameUpdatable(Updatable item) {
    gameUpdatables.remove(item);
  }
  
  public void addUiUpdatable(Updatable item) {
    miscUpdatables.add(item);
  }
  
  public void clearUiUpdatables() {
    miscUpdatables.clear();
  }
  
  public void removeUiUpdatable(Updatable item) {
    miscUpdatables.remove(item);
  }
  
  public boolean hasUiUpdatable(Updatable item) {
    return miscUpdatables.find(item) != -1;
  }
  
  /**
   * Sets up the current state of the game updater to
   * play a cinematic. That is, no other updatable will
   * be processed until the cinematic updatable has completed.
   * @param cinematicUpdatable Updatable to be playing for the cinematic
   */
  public void startCinematic(Updatable cinematicUpdatable) {
    cinematicPlaying = true;
    cinematic = cinematicUpdatable;
  }
  
  public void stopCinematic(){
    cinematic = null;
    cinematicPlaying = false;
  }
  
  @Override
  public void start(){
    super.start();
    DebugLog.d(TAG, "Started");
  }
  
  public void setTimeMultiplier(float multiplier) {
    timeMultiplier = multiplier;
    minUpdate = (int) (12/(timeMultiplier * 1.0f));
    maxUpdate = (int) (16/(timeMultiplier * 1.0f));
  }
  
  public float getTimeMultiplier() {
    return timeMultiplier;
  }

  @Override
  public void run() {
    lastTime = SystemClock.uptimeMillis();
    while (inGame) {      
      final long time = SystemClock.uptimeMillis();
            final long timeDelta = time - lastTime;
            long finalDelta = timeDelta;
            if (timeDelta > minUpdate) {
                float secondsDelta = (time - lastTime) * 0.001f;
                if (secondsDelta > 0.1f) {
                  // if the update time is more than 0.1f
                  // then force 0.1f as update time to avoid collision problems
                    secondsDelta = 0.1f;
                }
                lastTime = time;
                
                if(cinematicPlaying) {
                  cinematic.update(secondsDelta);
                } else {
                  gameUpdatables.update(secondsDelta * timeMultiplier);
                  miscUpdatables.update(secondsDelta);
                }
            }
            
            if (finalDelta < maxUpdate) {
                try {
                    Thread.sleep(maxUpdate - finalDelta);
                } catch (InterruptedException e) {/* do nothing */}
            }

      synchronized(pauseLock){
        if(paused){
          while (paused) {
            try {
              pauseLock.wait();
            } catch (InterruptedException e) {/* do nothing */}
          }
        }
      }
    }
    
    synchronized(stopLock) {
      DebugLog.d(TAG, "GameUpdate has stopped");
      stopLock.notify();
    }
  }

  public void stopUpdaterAndWait() {
    synchronized(stopLock) {
      // the thread already stopped... 
      // just return instead of waiting forever...
      if(!isRunning()) return;
      
      inGame = false;
      synchronized(pauseLock){
        paused = false;
        pauseLock.notify();
      }
      try {
        stopLock.wait();
      } catch (InterruptedException e) {/*do nothing*/}
    }
  }
  
  public boolean isRunning() {
    synchronized(stopLock) {
      return inGame;
    }
  }

  public boolean isPaused() {
    return paused;
  }
}




Java Source Code List

com.ggstudios.divisionbyzero.ActivityAbout.java
com.ggstudios.divisionbyzero.ActivityLevelSelection.java
com.ggstudios.divisionbyzero.ActivityMainMenu.java
com.ggstudios.divisionbyzero.ActivitySettings.java
com.ggstudios.divisionbyzero.ActivitySplashScreen.java
com.ggstudios.divisionbyzero.ApplicationMain.java
com.ggstudios.divisionbyzero.BaseActivity.java
com.ggstudios.divisionbyzero.BaseDialog.java
com.ggstudios.divisionbyzero.BulletManager.java
com.ggstudios.divisionbyzero.Bullet.java
com.ggstudios.divisionbyzero.Button.java
com.ggstudios.divisionbyzero.Circle.java
com.ggstudios.divisionbyzero.ClickableCollection.java
com.ggstudios.divisionbyzero.Clickable.java
com.ggstudios.divisionbyzero.ConfirmDialog.java
com.ggstudios.divisionbyzero.Core.java
com.ggstudios.divisionbyzero.CustomGLSurfaceView.java
com.ggstudios.divisionbyzero.DialogManager.java
com.ggstudios.divisionbyzero.DrawableCollection.java
com.ggstudios.divisionbyzero.DrawableString.java
com.ggstudios.divisionbyzero.Drawable.java
com.ggstudios.divisionbyzero.EndDialog.java
com.ggstudios.divisionbyzero.Event.java
com.ggstudios.divisionbyzero.ExplosionGenerator.java
com.ggstudios.divisionbyzero.ExtrasManager.java
com.ggstudios.divisionbyzero.FontManager.java
com.ggstudios.divisionbyzero.GameRenderer.java
com.ggstudios.divisionbyzero.GameUpdater.java
com.ggstudios.divisionbyzero.Game.java
com.ggstudios.divisionbyzero.Grid.java
com.ggstudios.divisionbyzero.Hud.java
com.ggstudios.divisionbyzero.InGameMenu.java
com.ggstudios.divisionbyzero.InfoDialog.java
com.ggstudios.divisionbyzero.Label.java
com.ggstudios.divisionbyzero.LevelManager.java
com.ggstudios.divisionbyzero.LevelMap.java
com.ggstudios.divisionbyzero.LineGuide.java
com.ggstudios.divisionbyzero.MainActivity.java
com.ggstudios.divisionbyzero.Map.java
com.ggstudios.divisionbyzero.MessageDialog.java
com.ggstudios.divisionbyzero.ParticleEngine.java
com.ggstudios.divisionbyzero.PathFinder.java
com.ggstudios.divisionbyzero.PauseMenu.java
com.ggstudios.divisionbyzero.PictureBox.java
com.ggstudios.divisionbyzero.Player.java
com.ggstudios.divisionbyzero.PopupMenu.java
com.ggstudios.divisionbyzero.Rectangle.java
com.ggstudios.divisionbyzero.Shader.java
com.ggstudios.divisionbyzero.SpawnManager.java
com.ggstudios.divisionbyzero.SpriteAnimation.java
com.ggstudios.divisionbyzero.SpriteManager.java
com.ggstudios.divisionbyzero.Sprite.java
com.ggstudios.divisionbyzero.Statbar.java
com.ggstudios.divisionbyzero.StateManager.java
com.ggstudios.divisionbyzero.TargetRectangle.java
com.ggstudios.divisionbyzero.TextureManager.java
com.ggstudios.divisionbyzero.TowerInfoDialog.java
com.ggstudios.divisionbyzero.TowerLibrary.java
com.ggstudios.divisionbyzero.TowerManager.java
com.ggstudios.divisionbyzero.TowerMenu.java
com.ggstudios.divisionbyzero.Tower.java
com.ggstudios.divisionbyzero.UpdatableCollection.java
com.ggstudios.divisionbyzero.Updatable.java
com.ggstudios.divisionbyzero.UpgradeDialog.java
com.ggstudios.divisionbyzero.Utils.java
com.ggstudios.divisionbyzero.VBO.java
com.ggstudios.divisionbyzero.WaveControlDialog.java
com.ggstudios.divisionbyzero.WaveControlHud.java
com.ggstudios.divisionbyzero.ZoomControl.java
com.ggstudios.utils.BitmapUtils.java
com.ggstudios.utils.BufferUtils.java
com.ggstudios.utils.DebugLog.java
com.ggstudios.utils.ShaderUtils.java
com.ggstudios.widget.LevelMapBackground.java
fix.android.opengl.GLES20.java