Subversion Repositories AndroidProjects

Rev

Blame | Last modification | View Log | RSS feed

package com.gebauz.bauzoid.app;

import com.badlogic.gdx.ApplicationListener;
import com.gebauz.bauzoid.game.Game;
import com.gebauz.bauzoid.game.Game.IGameFactory;

/** Base class for applications using Bauzoid engine.
 * Implemented as a Generic, T is reached through to Game<T>
 */

public abstract class BauzoidApp implements ApplicationListener
{
        public interface IAdHandler
        {
                public void show(boolean visible);
        }
       
        private String mGameTitle = "BauzoidGame";
        protected Game mGame = null;
        private long mSystemTimeMs = System.currentTimeMillis();
        private boolean mAdsEnabled = false;
       
        private IGameFactory mGameFactory = null;
        private IAdHandler mAdHandler = null;
       
        public BauzoidApp(String title, boolean adsEnabled)
        {
                this(title, adsEnabled, null, null);
        }

        public BauzoidApp(String title, boolean adsEnabled, IGameFactory gameFactory)
        {
                this(title, adsEnabled, gameFactory, null);
        }
       
        public BauzoidApp(String title, boolean adsEnabled, IAdHandler handler)
        {
                this(title, adsEnabled, null, handler);
        }
       
        public BauzoidApp(String title, boolean adsEnabled, IGameFactory gameFactory, IAdHandler handler)
        {
                mGameTitle = title;
                mAdsEnabled = adsEnabled;
                mGameFactory = gameFactory;
                mAdHandler = handler;
        }
       
        @Override
        public void create()
        {
                mGame = new Game(this, mGameTitle, mAdsEnabled, mGameFactory);
                mGame.init();
               
                initGame();
               
                mSystemTimeMs = System.currentTimeMillis();
        }

        @Override
        public void dispose()
        {
                exitGame();
               
                mGame.exit();
                mGame = null;
        }

        @Override
        public void pause()
        {
                mGame.pause();
        }

        @Override
        public void resume()
        {
                mGame.resume();
        }

        @Override
        public void resize(int w, int h)
        {
                mGame.resize(w, h);
        }
       
        @Override
        public void render()
        {
                float deltaTime = updateInternalTimer();
               
                //deltaTime = 1/5.0f;

                mGame.update(deltaTime);
               
                /*try
                {
                        Thread.sleep(500);
                }
                catch (InterruptedException e)
                {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }*/


                mGame.render();
        }
       
        /** Update the internal timer and return the resulting deltaTime. Can be used for times when a single method will use a lot of time. */
        public float updateInternalTimer()
        {
                long currentTimeMs = System.currentTimeMillis();
                long deltaTimeMs = currentTimeMs - mSystemTimeMs;
                mSystemTimeMs = currentTimeMs;
               
                return (float)deltaTimeMs / 1000.0f;
        }

        /** Called after the engine internals have been initialized - this is the point to kick off the initial game state. */
        public abstract void initGame();
       
        /** Called before the engine internals are cleaned up. */
        public abstract void exitGame();
       
        /** Get the instance of the game. */
        public Game getGame()
        {
                return mGame;
        }
       
        public long getSystemTimeMs()
        {
                return mSystemTimeMs;
        }
       
        public void showAds(boolean visible)
        {
                if (mAdHandler != null)
                        mAdHandler.show(visible);
        }
       
        public void setAdHandler(IAdHandler handler)
        {
                mAdHandler = handler;
        }

}