Subversion Repositories AndroidProjects

Rev

Rev 269 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.gebauz.Bauzoid.app;

import java.util.Random;

import com.gebauz.Bauzoid.audio.Audio;
import com.gebauz.Bauzoid.gamestates.GameStateManager;
import com.gebauz.Bauzoid.graphics.Graphics;
import com.gebauz.Bauzoid.input.Input;

/** Root object for the game. Manages game-global objects, acts as the root of the game graph, and kicks off update and render calls.
 *
 * This is the parent class for the following subsystems:
 * - GameStateManager: Root object for game states.
 * - Graphics: Root object for all graphics objects
 * - Audio: Root object for all audio objects
 * - ..
 *
 * It also offers convenient access to the Android application's Context
 */

public class Game
{
        private GameStateManager mGameStateManager = null;
       
        private Graphics mGraphics = null;
        private Audio mAudio = null;
        private Input mInput = null;
        private Random mRandomizer = new Random();
       
        private CustomServices mCustomServices = null;
       
        /** Constructor. */
        public Game()
        {
                // Set the context to use for engine exceptions

                mGameStateManager = new GameStateManager(this);
               
                mGraphics = new Graphics(this);
                mAudio = new Audio(this);
                mInput = new Input(this);
        }
       
        /** Called for game-global initialization. */
        public void init()
        {
                mGraphics.init();
                mAudio.init();
                mInput.init();
                mGameStateManager.init();
        }
       
        /** Called for game-global destruction. */
        public void exit()
        {
                mGameStateManager.exit();
                mInput.exit();
                mAudio.exit();
                mGraphics.exit();
       
                if (mCustomServices != null)
                {
                        mCustomServices.exit();
                        mCustomServices = null;
                }              
        }
       
        /** Update game logic state. */
        public void update(float deltaTime)
        {
                if (mCustomServices != null)
                {
                        mCustomServices.update(deltaTime);
                }
               
                mGameStateManager.update(deltaTime);
                mAudio.update(deltaTime);
                mInput.update(deltaTime);
        }
       
        /** Render game. */
        public void render()
        {
                if (mCustomServices != null)
                {
                        mCustomServices.render();
                }
               
                mGameStateManager.render();
                mAudio.render();
                mInput.render();
        }
       
        /** Called when the surface dimensions have changed. */
        public void resize(int w, int h)
        {
                mGraphics.updateSurfaceDimensions(w, h);
                mGameStateManager.onSurfaceChanged(w, h);
        }
       
        /** Called when the OpenGL Context is about to be destroyed. */
        public void pause()
        {
                mGraphics.onSurfaceLost();
                mAudio.onPause();
                mInput.onPause();
        }
       
        /** Called when the OpenGL Context has been recreated after onSurfaceLost(). */
        public void resume()
        {
                mGraphics.onSurfaceRecreated();
                mAudio.onResume();
                mAudio.onResume();
        }
       
        /** Get the main graphics object. */
        public final Graphics getGraphics()
        {
                return mGraphics;
        }
       
        /** Get the main audio object. */
        public final Audio getAudio()
        {
                return mAudio;
        }

        /** Get the main input object. */
        public final Input getInput()
        {
                return mInput;
        }
       
        /** Get the app specific custom services. */
        public final CustomServices getCustomServices()
        {
                return mCustomServices;
        }
       
        /** Install an app specific custom services. */
        public final void setCustomServices(CustomServices customServices)
        {
                if (mCustomServices != null)
                {
                        mCustomServices.exit();
                }
               
                mCustomServices = customServices;
               
                if (mCustomServices != null)
                {
                        mCustomServices.init();
                }
        }
       
        /** Get the GameStateManager. */
        public final GameStateManager getGameStateManager()
        {
                return mGameStateManager;
        }

        public Random getRandomizer()
        {
                return mRandomizer;
        }
       
        public float getRandomFloat(float min, float max)
        {
                return min + mRandomizer.nextFloat() * (max-min);
        }
       
        public int getRandomInt(int min, int max)
        {
                return min + mRandomizer.nextInt(max-min);
        }
}