Subversion Repositories AndroidProjects

Rev

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

package com.gebauz.pingK.game;

import com.gebauz.framework.util.MathUtil;
import com.gebauz.framework.util.Sprite2D;
import com.gebauz.pingK.MultitouchInput;
import com.gebauz.pingK.R;

import com.gebauz.pingK.gamestates.GameStateManager;

/** Not a very pretty pause menu implementation. */
public class PauseScreen
{
        private static final int MENU_RESTART = 0;
        private static final int MENU_QUIT = 1;
       
        private GameLogic mGameLogic;
        private Sprite2D mBackground = new Sprite2D();
        private Sprite2D mBlack = new Sprite2D();
        private float mPopupTimer = GameConsts.PAUSE_MENU_POPUP_TIME + 1.0f;
       
        private int mSelectedMenuItem = -1;
       
        public PauseScreen(GameLogic gameLogic)
        {
                mGameLogic = gameLogic;
               
                mBackground.init(R.drawable.pause_bg, 0, mGameLogic.getVirtualPlayFieldHeight() / 2.0f, GameConsts.VIRTUAL_SCREEN_WIDTH, GameConsts.PAUSE_BG_HEIGHT);
                mBackground.pivotX = 0;
                mBackground.pivotY = GameConsts.PAUSE_BG_HEIGHT / 2.0f;
               
                mBlack.init(R.drawable.black50, 0, 0, GameConsts.VIRTUAL_SCREEN_WIDTH, mGameLogic.getVirtualPlayFieldHeight());
                mBlack.pivotX = 0;
                mBlack.pivotY = 0;
        }
       
        public void update(float deltaTime)
        {
                mPopupTimer += deltaTime;
                float bgsize = 0.0f;
                if (mGameLogic.isPaused())
                {
                        bgsize = MathUtil.clamp(mPopupTimer / GameConsts.PAUSE_MENU_POPUP_TIME, 0.0f, 1.0f) * GameConsts.PAUSE_BG_HEIGHT;
                }
                else
                {
                        bgsize = (1.0f-MathUtil.clamp(mPopupTimer / GameConsts.PAUSE_MENU_POPUP_TIME, 0.0f, 1.0f)) * GameConsts.PAUSE_BG_HEIGHT;                       
                }
               
                mBackground.h = bgsize;
                mBackground.pivotY = mBackground.h/2.0f;
               
                mBackground.update(deltaTime);
                mBlack.update(deltaTime);
               
                if (!mGameLogic.isPaused())
                        return;
               
                if (checkMenuItem(R.string.pause_restart, GameConsts.PAUSE_MENU_TEXT_SCALE, GameConsts.PAUSE_MENU_RESTART_POSITION_Y, GameConsts.BUTTON_TOUCH_TOLERANCE))
                {
                        // restart to same state resets game
                        mSelectedMenuItem = MENU_RESTART;
                        GameStateManager.getInstance().switchState("MainGameState");
                        return;
                }
               
                if (checkMenuItem(R.string.pause_quit, GameConsts.PAUSE_MENU_TEXT_SCALE, GameConsts.PAUSE_MENU_QUIT_POSITION_Y, GameConsts.BUTTON_TOUCH_TOLERANCE))
                {
                        mSelectedMenuItem = MENU_QUIT;
                        GameStateManager.getInstance().switchState("TitleState");
                        return;
                }
        }
       
        public void render()
        {              
                if (mGameLogic.isPaused())
                {
                        mBlack.render();
                }
               
                if (!mGameLogic.isPaused() && (mPopupTimer > GameConsts.PAUSE_MENU_POPUP_TIME))
                        return;
               
                mBackground.render();
               
                if (mGameLogic.isPaused() && (mPopupTimer > GameConsts.PAUSE_MENU_POPUP_TIME))                         
                {
                        renderText(R.string.pause_text, GameConsts.PAUSE_TITLE_TEXT_SCALE, GameConsts.PAUSE_TITLE_TEXT_POSITION_Y, false);
                        renderText(R.string.pause_restart, GameConsts.PAUSE_MENU_TEXT_SCALE, GameConsts.PAUSE_MENU_RESTART_POSITION_Y, mSelectedMenuItem == MENU_RESTART);
                        renderText(R.string.pause_quit, GameConsts.PAUSE_MENU_TEXT_SCALE, GameConsts.PAUSE_MENU_QUIT_POSITION_Y, mSelectedMenuItem == MENU_QUIT);
                }
        }
       
        public boolean checkMenuItem(int id, float scale, float posY, float tolerance)
        {
                String text = GameStateManager.getInstance().getContext().getResources().getString(id);
                float w = text.length() * Font.CHARACTER_SIZE * scale;
                float h = Font.CHARACTER_SIZE * scale;
                float posX = (GameConsts.VIRTUAL_SCREEN_WIDTH - w) / 2.0f;
               
                MultitouchInput.Finger finger = MultitouchInput.getInstance().getTouchPointInside(posX - tolerance, posY - tolerance, posX + w + tolerance, posY + h + tolerance);
                return (finger != null);
        }
       
        public void renderText(int id, float scale, float posY, boolean selected)
        {
                String text = GameStateManager.getInstance().getContext().getResources().getString(id);
               
                if (selected)
                {
                        text = "=" + text + "=";
                }
               
                float w = text.length() * Font.CHARACTER_SIZE * scale;
                float h = Font.CHARACTER_SIZE * scale;
                mGameLogic.getFont().drawText(text, (GameConsts.VIRTUAL_SCREEN_WIDTH - w) / 2.0f, posY, scale);        
        }
       
        public void startShowing()
        {
                mPopupTimer = 0.0f;
        }

}