Android Open Source - shapemergency Game Screen






From Project

Back to project page shapemergency.

License

The source code is released under:

GNU General Public License

If you think the Android project shapemergency 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.adsg0186.shapemergency;
/*from  www  . j a  v  a2 s . c o m*/
/*
import android.content.Context;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
*/


import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.util.Log;

import com.adsg0186.shapemergency.testgame1.FiringGameTest;
import com.adsg0186.shapemergency.testgame1.config.GameConfig;
import com.adsg0186.shapemergency.testgame1.config.SavedGame;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.github.adsgray.gdxtry1.engine.*;
import com.github.adsgray.gdxtry1.engine.input.DefaultDirectionListener;
import com.github.adsgray.gdxtry1.engine.input.DragAndFlingDirectionListener;
import com.github.adsgray.gdxtry1.engine.input.SimpleDirectionGestureDetector;
import com.github.adsgray.gdxtry1.engine.input.SimpleDirectionGestureDetector.DirectionListener;
import com.github.adsgray.gdxtry1.engine.output.Renderer;
import com.github.adsgray.gdxtry1.engine.output.SoundIF;
import com.github.adsgray.gdxtry1.engine.output.SoundPoolPlayer;
import com.github.adsgray.gdxtry1.engine.util.Game;
import com.github.adsgray.gdxtry1.engine.util.GameCommand;
import com.github.adsgray.gdxtry1.engine.util.GameFactory;
import com.github.adsgray.gdxtry1.engine.util.HighScoreSaveIF;
import com.github.adsgray.gdxtry1.engine.util.LocalHighScore;
import com.github.adsgray.gdxtry1.engine.util.WorldTickTask;

public class GameScreen implements ApplicationListener {

    private static final int CAMERA_WIDTH = 800;
  private static final int CAMERA_HEIGHT = 1422;
  private static final int numBlobs = 10;

  private WorldIF world;
  private Renderer renderConfig;
  private OrthographicCamera camera;
  private SpriteBatch batch;
  private ShapeRenderer shapes;
  private Timer worldTimer;
  private TimerTask worldTick;
  private GameFinished gameFinished; // executed by the Game when it is complete
  private GameCommand exitGame; // called by this ApplicationListener to go back to menu screen
    private HighScoreSaveIF highScore;
  Game game;
  protected int difficultyLevel = 1;
  Context context;
  
  private class GameFinished implements GameCommand {
      protected Game game;
      
      public void setGame(Game game) { this.game = game; }

        @Override public void execute(int score) {
            Log.d("trace", "Game finished");
            game.stop();
            SavedGame.get().clearSavedGame();

            // make a funciton that maps from difficulty level to score string
            // used here and in score display activity/view

            highScore.submitScore(String.format("high_score_%d", GameConfig.get().getDifficultyLevel()), score);
            exitGame.execute(score);
        }
  }
  

  public GameScreen(Context context, GameCommand exitGame, int difficultyLevel) {
      super();
      this.context = context;
      this.exitGame = exitGame;
      this.difficultyLevel = difficultyLevel;
  }
  
  // amazingly, yes, in resume() we need an entirely new
  // Timer and Task. You can't just create a new timer and put
  // the old task on it. That's an exception.
  // 01-04 16:29:19.190: E/AndroidRuntime(14979): java.lang.IllegalStateException: TimerTask is scheduled already
  private void startWorldTicker() {
      // create timer task that will call tick on world every 25 ms
      worldTimer = new Timer("worldTickTimer");
      worldTick = WorldTickTask.createInstance(world);
      worldTimer.scheduleAtFixedRate(worldTick, 0, 25);
  }

  @Override
  public void create() {
      shapes = new ShapeRenderer();
    batch = new SpriteBatch();
    gameFinished = new GameFinished();
        highScore = LocalHighScore.get();
    
    Log.d("trace", "in gamescreen create");

    Renderer.createRealInstance(shapes, batch);
      renderConfig = Renderer.getRealInstance();
      world = GameFactory.defaultWorld();
      
      Gdx.graphics.setContinuousRendering(false);
      startWorldTicker();
     
    camera = new OrthographicCamera();
    camera.setToOrtho(false, CAMERA_WIDTH, CAMERA_HEIGHT); // the camera is like a window into our game world
    
    // Setup swipe/touch handling:
    // The SimpleDirectionGestureDetector processes events, mangles the coordinates
    // so that they're in relation to the camera, and fires the events in
    // the DirecitonListener

    //DirectionListener dl = new TestDirectionListener(world);
      //populateWorld();

    DirectionListener dl = new DragAndFlingDirectionListener();
    game = new FiringGameTest((DragAndFlingDirectionListener)dl, world, renderConfig, context, gameFinished);
    gameFinished.setGame(game);
    game.init();
    //GameCommand toggleSound = game.getSoundToggle(); // get sound toggler command
    GameCommand difficulty = game.getDifficultySetter();
    difficulty.execute(difficultyLevel); // 0 = easy, 1 = normal, 2 = insane
    //toggleSound.execute(1); // enable sound
    game.start();

    Gdx.input.setInputProcessor(new SimpleDirectionGestureDetector(camera, dl));
    //Gdx.input.setCatchBackKey(true);
  }
  

    @Override
    public void dispose() {
        // TODO Auto-generated method stub
        Log.d("trace", "GameScreen dispose");
        shapes.dispose();
        batch.dispose();
        Renderer.get().dispose();
    }

    @Override
    public void pause() {
        worldTick.cancel();
        worldTimer.cancel();
        worldTimer.purge();
        worldTimer = null;
        Log.d("trace", "GameScreen pause");
        /*
         * stash game state somewhere
         * score
         * blobs
         * etc
         */
        game.save();
    }

  protected long millisOfLastTick = 0;
  //protected long milliDelta = 33; // about 30 ticks per second?
  protected long milliDelta = 25; // 40 ticks per second

  @Override
  public void render() {
      
      // TODO: config class for colours
      //Gdx.gl.glClearColor(0.199f, 0.398f, 0.598f, 0.4f);  // OpenGL code to make the screen blue
      Gdx.gl.glClearColor(0f, 0f, 0f, 0.4f);  
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);      // OpenGL code to clear the screen
      
      // only tick 30 times per second
      /*
      long curMillis = System.currentTimeMillis();
      if (curMillis - millisOfLastTick >= milliDelta) {
          millisOfLastTick = curMillis;
          world.tick();
      }
      */

      // but we have to render all the time
      camera.update();
      batch.setProjectionMatrix(camera.combined);
      shapes.setProjectionMatrix(camera.combined);
      world.render();
  }

    @Override
    public void resize(int arg0, int arg1) {
        // TODO Auto-generated method stub
        Log.d("trace", "GameScreen resize");
        
    }

    @Override
    public void resume() {
        Log.d("trace", "GameScreen resume");
        if (worldTimer == null) {
            startWorldTicker();
        }
    }
    
}




Java Source Code List

com.adsg0186.shapemergency.GameActivity.java
com.adsg0186.shapemergency.GameScreen.java
com.adsg0186.shapemergency.HelpView.java
com.adsg0186.shapemergency.HighScoreView.java
com.adsg0186.shapemergency.MainActivity.java
com.adsg0186.shapemergency.SettingsView.java
com.adsg0186.shapemergency.testgame1.AngryTargetMissileSource.java
com.adsg0186.shapemergency.testgame1.BonusFactory.java
com.adsg0186.shapemergency.testgame1.BossTargetMissileSource.java
com.adsg0186.shapemergency.testgame1.CreateEnemyTrigger.java
com.adsg0186.shapemergency.testgame1.DefenderCollisionTrigger.java
com.adsg0186.shapemergency.testgame1.FiringGameTest.java
com.adsg0186.shapemergency.testgame1.GameSound.java
com.adsg0186.shapemergency.testgame1.MissileBlobSource.java
com.adsg0186.shapemergency.testgame1.MissileCollisionTrigger.java
com.adsg0186.shapemergency.testgame1.ShieldCollisionTrigger.java
com.adsg0186.shapemergency.testgame1.TargetMissileSource.java
com.adsg0186.shapemergency.testgame1.TargetUtils.java
com.adsg0186.shapemergency.testgame1.Vibrate.java
com.adsg0186.shapemergency.testgame1.blobs.BonusDropper.java
com.adsg0186.shapemergency.testgame1.blobs.BonusIF.java
com.adsg0186.shapemergency.testgame1.blobs.BossEnemy.java
com.adsg0186.shapemergency.testgame1.blobs.DamagableIF.java
com.adsg0186.shapemergency.testgame1.blobs.DamagerIF.java
com.adsg0186.shapemergency.testgame1.blobs.DefaultEnemy.java
com.adsg0186.shapemergency.testgame1.blobs.EnemyBomb.java
com.adsg0186.shapemergency.testgame1.blobs.EnemyFactory.java
com.adsg0186.shapemergency.testgame1.blobs.EnemyIF.java
com.adsg0186.shapemergency.testgame1.blobs.FiringBlobDecorator.java
com.adsg0186.shapemergency.testgame1.blobs.FlashMessage.java
com.adsg0186.shapemergency.testgame1.blobs.HitpointBonusDecorator.java
com.adsg0186.shapemergency.testgame1.blobs.ScoreTextDisplay.java
com.adsg0186.shapemergency.testgame1.blobs.ShieldRing.java
com.adsg0186.shapemergency.testgame1.config.BaseGameConfig.java
com.adsg0186.shapemergency.testgame1.config.EasyGameConfig.java
com.adsg0186.shapemergency.testgame1.config.GameConfigIF.java
com.adsg0186.shapemergency.testgame1.config.GameConfig.java
com.adsg0186.shapemergency.testgame1.config.GamePreferences.java
com.adsg0186.shapemergency.testgame1.config.InsaneGameConfig.java
com.adsg0186.shapemergency.testgame1.config.SavedGame.java