AndEngineのサンプルプロジェクト(その3)

 シーン遷移を管理する SceneManager.java。

package com.chabaori.sample;

import org.andengine.engine.Engine;
import org.andengine.ui.IGameInterface.OnCreateSceneCallback;

public class SceneManager{
	private BaseScene splashScene;
	private BaseScene gameScene;
	
	private static final SceneManager INSTANCE = new SceneManager();
	private SceneType currentSceneType = SceneType.SCENE_SPLASH;
	private BaseScene currentScene;
	private Engine engine = ResourcesManager.getInstance().engine;
	
	public enum SceneType{
		SCENE_SPLASH,
		SCENE_GAME,
	}
	
	public void setScene(BaseScene scene){
		engine.setScene(scene);
		currentScene = scene;
		currentSceneType = scene.getSceneType();
	}

	public void setScene(SceneType sceneType){
		switch (sceneType){
			case SCENE_GAME:
				setScene(gameScene);
				break;
			case SCENE_SPLASH:
				setScene(splashScene);
				break;
			default:
				break;
		}
	}
	
	public void createGameScene(){
		ResourcesManager.getInstance().loadResources();
		gameScene = new GameScene();
		SceneManager.getInstance().setScene(gameScene);
        disposeSplashScene();
	}
	
	public void createSplashScene(OnCreateSceneCallback pOnCreateSceneCallback){
		ResourcesManager.getInstance().loadSplashScreen();
		splashScene = new SplashScene();
		currentScene = splashScene;
		pOnCreateSceneCallback.onCreateSceneFinished(splashScene);
	}
	
	private void disposeSplashScene(){
		ResourcesManager.getInstance().unloadSplashScreen();
		splashScene.disposeScene();
		splashScene = null;
	}
	
	public static SceneManager getInstance(){
		return INSTANCE;
	}
	
	public SceneType getCurrentSceneType(){
		return currentSceneType;
	}
	
	public BaseScene getCurrentScene(){
		return currentScene;
	}
}

AndEngineのサンプルプロジェクト(その2)

 今回は、ResourcesManager.java、画像などのリソースを一括管理するクラスです。

package com.chabaori.sample;

import org.andengine.engine.Engine;
import org.andengine.engine.camera.BoundCamera;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
														
public class ResourcesManager{
	
	private static final ResourcesManager INSTANCE = new ResourcesManager();
	
	public Engine engine;
	public MainActivity activity;
	public BoundCamera camera;
	public VertexBufferObjectManager vbom;

	private BitmapTextureAtlas splashTextureAtlas;
	public ITextureRegion splash_region;
	private BitmapTextureAtlas backgroundTextureAtlas;
	public ITextureRegion background_region;
	private BitmapTextureAtlas spriteTextureAtlas;
	public ITextureRegion sprite_region;

	public void loadSplashScreen(){
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
        splashTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
        splash_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(splashTextureAtlas, activity, "splash.png", 0, 0);
        splashTextureAtlas.load();
        backgroundTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 512, 1024, TextureOptions.BILINEAR);
        background_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(backgroundTextureAtlas, activity, "background.png", 0, 0);
        backgroundTextureAtlas.load();
	}
	
	public void unloadSplashScreen(){
		splashTextureAtlas.unload();
		splashTextureAtlas = null;
		splash_region = null;
	}
	
	public void loadResources(){
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
        spriteTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
        sprite_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(spriteTextureAtlas, activity, "sprite.png", 0, 0);
        spriteTextureAtlas.load();
	}
	
	public static void prepareManager(Engine engine, MainActivity activity, BoundCamera camera, VertexBufferObjectManager vbom){
		getInstance().engine = engine;
		getInstance().activity = activity;
		getInstance().camera = camera;
		getInstance().vbom = vbom;
	}
	
	public static ResourcesManager getInstance(){
		return INSTANCE;
	}
}

 画像は、background.png と splash.png、sprite.png を assets フォルダ内に gfx フォルダを作って置いておきます。
background splash sprite

AndEngineのサンプルプロジェクト(その1)

 タッチ制御のプログラムを抜書きしても「全体がわからないから使いどころがわからない」というご意見もあるかもしれないと思ったので、プサンプルプロジェクトを作ってみました。
 
 起動後スプラッシュ画面を表示し、2秒後にメイン Scene に遷移し Sprite を表示します。(タッチ制御は書いてない!)

 スプラッシュ画面を表示し、
device-2014-06-08-002913
 Sprite を表示する。
device-2014-06-08-002834
 登場するクラスファイルは、
  MainActivity.java
  ResourcesManager.java
  SceneManager.java
  BaseScene.java
  SplashScene.java
  GameScene.java

 今日は、MainActivity.java を載せましょう。

package com.chabaori.sample;

import java.io.IOException;

import org.andengine.engine.Engine;
import org.andengine.engine.LimitedFPSEngine;
import org.andengine.engine.camera.BoundCamera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.ui.activity.BaseGameActivity;
import android.view.KeyEvent;

public class MainActivity extends BaseGameActivity {
	private BoundCamera camera;
	static final int CAMERA_WIDTH = 480;
	static final int CAMERA_HEIGHT = 800;
	   
	@Override
	public Engine onCreateEngine(EngineOptions pEngineOptions){
		return new LimitedFPSEngine(pEngineOptions, 60);
	}
	
	@Override
	public EngineOptions onCreateEngineOptions(){
		camera = new BoundCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		camera.setBoundsEnabled(true);
		EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new FillResolutionPolicy(), this.camera);
		engineOptions.getRenderOptions().getConfigChooserOptions().setRequestedMultiSampling(true);
		engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
		return engineOptions;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event){  
	    if (keyCode == KeyEvent.KEYCODE_BACK){
	    	SceneManager.getInstance().getCurrentScene().onBackKeyPressed();
	    }
	    return false; 
	}
	
	@Override
	protected void onPause(){
	    super.onPause();
	}

	@Override
	protected synchronized void onResume(){
	    super.onResume();
	    System.gc();
	}

	@Override
	public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws IOException{
		ResourcesManager.prepareManager(mEngine, this, camera, getVertexBufferObjectManager());
		pOnCreateResourcesCallback.onCreateResourcesFinished();
	}

	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException{
		SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback);
	}

	@Override
	public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws IOException{
		mEngine.registerUpdateHandler(new TimerHandler(2.0f, new ITimerCallback(){
            @Override
			public void onTimePassed(final TimerHandler pTimerHandler){
                mEngine.unregisterUpdateHandler(pTimerHandler);
                SceneManager.getInstance().createGameScene();
            }
		}));
		pOnPopulateSceneCallback.onPopulateSceneFinished();
	}
}

「コメントもなしでホントに載せるだけ!?」と、思われたかもいらっしゃるでしょうか。
 
 
 えぇ、載せるだけです。