Android Open Source - ssniper-andengine Base Activity






From Project

Back to project page ssniper-andengine.

License

The source code is released under:

Apache License

If you think the Android project ssniper-andengine 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.cladophora.ssniper;
/*  w  w w.j  av a 2 s . c o  m*/
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.ViewGroup;
import com.cladophora.ssniper.entity.SPen;
import com.cladophora.ssniper.scene.GameScene;
import com.cladophora.ssniper.scene.PauseScene;
import com.cladophora.ssniper.scene.SplashScene;
import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.ZoomCamera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.view.RenderSurfaceView;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import java.util.Random;

public class BaseActivity extends SimpleBaseGameActivity {
    // ===========================================================
    // Constants
    // ===========================================================

    // ===========================================================
    // Fields
    // ===========================================================
    public static BaseActivity instance;
    public static int CAMERA_WIDTH;
    public static int CAMERA_HEIGHT;
    public static float SCOPE_SIZE;
    public static String SCREEN_TYPE;

    private static SPenHoverView mSPenHoverView;
    public static Scene mCurrentScene;
  public static Camera mCamera;
    public static ZoomCamera mScopeCamera;
    public static Random r = new Random();

    public static Font mNotoSansFont;
    public static Font mDigitFont;
    public static Font mDigitFontL;

    // ===========================================================
    // Constructors
    // ===========================================================

  public static BaseActivity getSharedInstance() {
       if (instance == null) {
           instance = new BaseActivity();
       }
       return instance;
  }

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSPenHoverView = new SPenHoverView(this);
        final ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        this.addContentView(mSPenHoverView, params);
        this.mRenderSurfaceView.bringToFront();
    }

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================
    @Override
    protected void onSetContentView() {
        this.mRenderSurfaceView = new RenderSurfaceView(this);
        this.mRenderSurfaceView.setRenderer(this.mEngine, this);
        this.setContentView(this.mRenderSurfaceView, createSurfaceViewLayoutParams());
    }

  @Override
  public EngineOptions onCreateEngineOptions() {
        instance = this;
        CAMERA_WIDTH = DeviceUtil.getWidth(instance);
        CAMERA_HEIGHT = DeviceUtil.getHeight(instance);
        //SCREEN_TYPE = DeviceUtil.getScreenType(instance);
    mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

        int smallDim;
        if (CAMERA_HEIGHT < CAMERA_WIDTH) {
            smallDim = CAMERA_HEIGHT;
        } else {
            smallDim = CAMERA_WIDTH;
        }
        switch(smallDim) {
            case 720:
                SCOPE_SIZE = 400;
                break;
            case 800:
                SCOPE_SIZE = 400;
                break;
            case 1080:
                SCOPE_SIZE = 500;
                break;
            case 1600:
                SCOPE_SIZE = 600;
                break;
            default:
                SCOPE_SIZE = 400;
                break;
        }

        mScopeCamera = new ZoomCamera(0, 0, SCOPE_SIZE, SCOPE_SIZE);
        final RatioResolutionPolicy pResolutionPolicy = new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT);
        final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, pResolutionPolicy, mCamera );
        engineOptions.getAudioOptions().setNeedsSound(true);
        return engineOptions;
  }

    @Override
    public Engine onCreateEngine(final EngineOptions pEngineOptions) {
        return new ScopeEngine(pEngineOptions, this.mScopeCamera);
    }

    @Override
  protected void onCreateResources() {
        SpriteManager.loadGraphics();
        SpriteManager.loadFonts();
        com.cladophora.ssniper.SoundManager.loadSounds();
    }

    @Override
    protected void onPause() {
        if (GameScene.inGame) {
            SPen.setEntityPositions();
            GameScene.pause();
        }
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        SpriteManager.loadGraphics();
        SpriteManager.loadFonts();
        com.cladophora.ssniper.SoundManager.loadSounds();
        SpriteManager.initializeRifleSprites();
        if (GameScene.inGame) {
            GameScene.resume();
        }
    }

    @Override
    public synchronized void onResumeGame() {
        if (this.mEngine != null) {
            super.onResumeGame();
        }
    }

    @Override
  protected Scene onCreateScene() {
    mCurrentScene = new SplashScene();
    return mCurrentScene;
  }

    @Override
    public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent) {
        if(pEvent.getAction() != KeyEvent.ACTION_DOWN  || !GameScene.inGame) {
            return super.onKeyDown(pKeyCode, pEvent);
        }
        if(pKeyCode == KeyEvent.KEYCODE_MENU) {
            GameScene.mCurrentScene.setChildScene(new PauseScene(mCamera), false, true, true);
            return true;
        } else if (pKeyCode == KeyEvent.KEYCODE_VOLUME_DOWN && (!GameScene.slowTimeActive && GameScene.slowTimeBank > (GameScene.SLOW_TIME_BANK_MAX * 0.5f))) {
            GameScene.activateSlowTime();
            return true;
        } else {
            return super.onKeyDown(pKeyCode, pEvent);
        }
    }

    // ===========================================================
    // Methods
    // ===========================================================
    // to change the current main scene
    public static void setCurrentScene(Scene scene) {
        mCurrentScene = null;
        mCurrentScene = scene;
        BaseActivity.getSharedInstance().getEngine().setScene(mCurrentScene);
    }


    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================

}




Java Source Code List

com.cladophora.ssniper.BaseActivity.java
com.cladophora.ssniper.DeviceUtil.java
com.cladophora.ssniper.EnemyPool.java
com.cladophora.ssniper.GameEffects.java
com.cladophora.ssniper.GameEvents.java
com.cladophora.ssniper.GameLoopUpdateHandler.java
com.cladophora.ssniper.GameUtil.java
com.cladophora.ssniper.HUDManager.java
com.cladophora.ssniper.HoverListener.java
com.cladophora.ssniper.ItemPool.java
com.cladophora.ssniper.LevelLoader.java
com.cladophora.ssniper.PlantPool.java
com.cladophora.ssniper.SPenHoverView.java
com.cladophora.ssniper.ScopeEngine.java
com.cladophora.ssniper.SoundManager.java
com.cladophora.ssniper.SpriteManager.java
com.cladophora.ssniper.andengine.IRandomTimerCallBack.java
com.cladophora.ssniper.andengine.RandomTimerHandler.java
com.cladophora.ssniper.entity.EnemyLayer.java
com.cladophora.ssniper.entity.Enemy.java
com.cladophora.ssniper.entity.ItemLayer.java
com.cladophora.ssniper.entity.Item.java
com.cladophora.ssniper.entity.PlantLayer.java
com.cladophora.ssniper.entity.Plant.java
com.cladophora.ssniper.entity.Reticle.java
com.cladophora.ssniper.entity.Rifle.java
com.cladophora.ssniper.entity.SPen.java
com.cladophora.ssniper.scene.GameScene.java
com.cladophora.ssniper.scene.InstructionScene.java
com.cladophora.ssniper.scene.IntroScene.java
com.cladophora.ssniper.scene.MainMenuScene.java
com.cladophora.ssniper.scene.PauseScene.java
com.cladophora.ssniper.scene.ResultScene.java
com.cladophora.ssniper.scene.ShopScene.java
com.cladophora.ssniper.scene.SplashScene.java
com.cladophora.ssniper.scene.SurvivorIntroScene.java
com.cladophora.ssniper.scene.SurvivorScene.java
com.cladophora.ssniper.scene.TutorialScene.java
com.cladophora.ssniper.scene.WeaponInfoScene.java