Monday, March 17, 2014

Game Classes Setup - BaseScene & SceneManager

* Game Classes Setup - BaseScene & SceneManager

Scene management is very crucial part of game development, for this we gonna create two classes called BaseScene and SceneManager.

 BaseScene - This class will be a basic representation of each created scene in our game, it is an abstract class, which will handle basic scene logic.
SceneManager - This class will provide ways to manage our scenes, switching between them, keeping track of current scene, scene type etc.

1. BaseScene class:

package com.mindew.fruitecollector;

import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.Scene;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.app.Activity;

import com.mindew.fruitcollector.ResourcesManager;
import com.mindew.fruitecollector.SceneManager.SceneType;

/**
 * @author Mateusz Mysliwiec
 * @author www.matim-dev.com
 * @version 1.0
 */
public abstract class BaseScene extends Scene
{
    //---------------------------------------------
    // VARIABLES
    //---------------------------------------------
   
    protected Engine engine;
    protected Activity activity;
    protected ResourcesManager resourcesManager;
    protected VertexBufferObjectManager vbom;
    protected Camera camera;
   
    //---------------------------------------------
    // CONSTRUCTOR
    //---------------------------------------------
   
    public BaseScene()
    {
        this.resourcesManager = ResourcesManager.getInstance();
        this.engine = resourcesManager.engine;
        this.activity = resourcesManager.activity;
        this.vbom = resourcesManager.vbom;
        this.camera = resourcesManager.camera;
        createScene();
    }
   
    //---------------------------------------------
    // ABSTRACTION
    //---------------------------------------------
   
    public abstract void createScene();
   
    public abstract void onBackKeyPressed();
   
    public abstract SceneType getSceneType();
   
    public abstract void disposeScene();
}

This is a core class for our every created scene in the game. It will be used as an extension, providing basic scene logic, using abstract methods. It will also have the most commonly used objects taken from previously created ResourcesManager. There is getSceneType , returning SceneType - it will be an enum inside the SceneManager class, ignore the error and move to the next step below to create the scene manager.

2. SceneManager class:

It will be a really important class in our game, responsible for switching between scenes and keeping track of the currently displayed scene. We will use SINGLETON HOLDER, which means we will be able to use this manger from the global level. It will also have an enum, containing our scene types. We also will create 4 BaseScene objects, for our scenes (splash, loading, menu and game scenes)

package com.mindew.fruitecollector;

import org.andengine.engine.Engine;

import com.mindew.fruitcollector.BaseScene;

/**
 * @author Mateusz Mysliwiec
 * @author www.matim-dev.com
 * @version 1.0
 */
public class SceneManager
{
    //---------------------------------------------
    // SCENES
    //---------------------------------------------
   
    private BaseScene splashScene;
    private BaseScene menuScene;
    private BaseScene gameScene;
    private BaseScene loadingScene;
   
    //---------------------------------------------
    // VARIABLES
    //---------------------------------------------
   
    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_MENU,
        SCENE_GAME,
        SCENE_LOADING,
    }
   
    //---------------------------------------------
    // CLASS LOGIC
    //---------------------------------------------
   
    public void setScene(BaseScene scene)
    {
        engine.setScene(scene);
        currentScene = scene;
        currentSceneType = scene.getSceneType();
    }
   
    public void setScene(SceneType sceneType)
    {
        switch (sceneType)
        {
            case SCENE_MENU:
                setScene(menuScene);
                break;
            case SCENE_GAME:
                setScene(gameScene);
                break;
            case SCENE_SPLASH:
                setScene(splashScene);
                break;
            case SCENE_LOADING:
                setScene(loadingScene);
                break;
            default:
                break;
        }
    }
   
    //---------------------------------------------
    // GETTERS AND SETTERS
    //---------------------------------------------
   
    public static SceneManager getInstance()
    {
        return INSTANCE;
    }
   
    public SceneType getCurrentSceneType()
    {
        return currentSceneType;
    }
   
    public BaseScene getCurrentScene()
    {
        return currentScene;
    }
}

We have our basescene and scene manager classes ready to use now.

No comments:

Post a Comment