Subversion Repositories AndroidProjects

Rev

Rev 32 | Rev 76 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.gebauz.pingK.gamestates;

import com.gebauz.framework.util.GLUtil;

import android.util.Log;

public class GameStateManager
{
       
        private static GameStateManager mInstance = new GameStateManager();
       
        private BaseGameState mCurrentState = null;
        private String mNextState = "";
       
        private GameStateManager()
        {
        }
       
        public static GameStateManager getInstance()
        {
                return mInstance;
        }
       
        public void switchState(String newState)
        {
                mNextState = newState;         
        }
       
        private void doSwitch(String newState)
        {
                if (mCurrentState != null)
                {
                        mCurrentState.exit();
                        mCurrentState = null;
                }
               
                try
                {
                        String packageName = getClass().getPackage().getName();
                        Class<?> theClass = Class.forName(packageName + "." + newState);
                        mCurrentState = (BaseGameState)theClass.newInstance();
                }
                catch (ClassNotFoundException ex)
                {
                        Log.v("GEBZ", "ClassNotFoundException when switching to " + newState);
                        return;
                }
                catch (InstantiationException ex)
                {
                        Log.v("GEBZ", "InstantiationException when switching to " + newState);
                        return;
                }
                catch (IllegalAccessException ex)
                {
                        Log.v("GEBZ", "IllegalAccessException when switching to " + newState);
                        return;
                }
               
                if (mCurrentState != null)
                {
                        mCurrentState.init();
                        mCurrentState.onSurfaceChanged(GLUtil.getInstance().getWidth(), GLUtil.getInstance().getHeight());
                }
        }
       
        public void onSurfaceChanged(int width, int height)
        {
                if (mCurrentState != null)
                {
                        mCurrentState.onSurfaceChanged(width, height);
                }
        }
       
        public void update(float deltaTime)
        {
               
                if (mNextState != "")
                {
                        // perform the actual switch
                        doSwitch(mNextState);
                        mNextState = "";
                }              
               
                if (mCurrentState != null)
                {
                        mCurrentState.update(deltaTime);
                }
        }
       
        public void render()
        {
                if (mCurrentState != null)
                {
                        mCurrentState.render();
                }              
        }
       
}