Rev 144 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 108 | chris | 1 | package com.gebauz.pingK.common.game.gamestates; |
| 2 | |||
| 3 | import java.util.Random; |
||
| 4 | |||
| 5 | import javax.microedition.khronos.opengles.GL10; |
||
| 6 | |||
| 7 | import com.gebauz.pingK.common.R; |
||
| 8 | import com.gebauz.pingK.common.framework.GLUtil; |
||
| 109 | chris | 9 | import com.gebauz.pingK.common.framework.ResourceManager; |
| 108 | chris | 10 | import com.gebauz.pingK.common.framework.Sprite2D; |
| 124 | chris | 11 | import com.gebauz.pingK.common.game.BaseButton; |
| 108 | chris | 12 | import com.gebauz.pingK.common.game.Font; |
| 13 | import com.gebauz.pingK.common.game.GameConsts; |
||
| 110 | chris | 14 | import com.gebauz.pingK.common.game.GameServices; |
| 117 | chris | 15 | import com.gebauz.pingK.common.game.HelpScreen; |
| 138 | chris | 16 | import com.gebauz.pingK.common.game.MultitouchInput; |
| 108 | chris | 17 | import com.gebauz.pingK.common.game.TextButton; |
| 18 | |||
| 136 | chris | 19 | import android.content.Context; |
| 20 | import android.content.SharedPreferences; |
||
| 108 | chris | 21 | import android.util.Log; |
| 22 | |||
| 23 | |||
| 24 | public class TitleState extends BaseGameState |
||
| 25 | { |
||
| 26 | private float mTimer = 0.0f; |
||
| 27 | |||
| 28 | private TextButton mGameStartButton; |
||
| 29 | private TextButton mOptionsButton; |
||
| 30 | private TextButton mHelpButton; |
||
| 144 | chris | 31 | private TextButton mCreditsButton; |
| 108 | chris | 32 | |
| 33 | private Sprite2D mTitle = null; |
||
| 34 | |||
| 138 | chris | 35 | //private Sprite2D mTouchPoint = null; |
| 36 | |||
| 136 | chris | 37 | private boolean mSoundEnabled = true; |
| 38 | |||
| 117 | chris | 39 | private HelpScreen mHelp; |
| 40 | |||
| 130 | chris | 41 | int mSoundIds[] = |
| 42 | { |
||
| 43 | R.raw.paddleimpact, |
||
| 44 | }; |
||
| 45 | |||
| 124 | chris | 46 | BaseButton.TouchListener mGameStartListener = new BaseButton.TouchListener() |
| 108 | chris | 47 | { |
| 48 | @Override |
||
| 124 | chris | 49 | public void onTouched(BaseButton sender, int tag) |
| 108 | chris | 50 | { |
| 51 | GameStateManager.getInstance().switchState("MainGameState"); |
||
| 52 | } |
||
| 53 | }; |
||
| 54 | |||
| 124 | chris | 55 | BaseButton.TouchListener mOptionsListener = new BaseButton.TouchListener() |
| 108 | chris | 56 | { |
| 57 | @Override |
||
| 124 | chris | 58 | public void onTouched(BaseButton sender, int tag) |
| 108 | chris | 59 | { |
| 60 | |||
| 61 | } |
||
| 62 | }; |
||
| 63 | |||
| 124 | chris | 64 | BaseButton.TouchListener mHelpListener = new BaseButton.TouchListener() |
| 108 | chris | 65 | { |
| 66 | @Override |
||
| 124 | chris | 67 | public void onTouched(BaseButton sender, int tag) |
| 108 | chris | 68 | { |
| 117 | chris | 69 | mHelp.show(); |
| 108 | chris | 70 | } |
| 71 | }; |
||
| 72 | |||
| 144 | chris | 73 | BaseButton.TouchListener mCreditsListener = new BaseButton.TouchListener() |
| 74 | { |
||
| 75 | @Override |
||
| 76 | public void onTouched(BaseButton sender, int tag) |
||
| 77 | { |
||
| 78 | GameStateManager.getInstance().switchState("CreditsState"); |
||
| 79 | } |
||
| 80 | }; |
||
| 81 | |||
| 108 | chris | 82 | public TitleState() |
| 83 | { |
||
| 84 | |||
| 85 | } |
||
| 86 | |||
| 87 | @Override |
||
| 88 | public void init() |
||
| 89 | { |
||
| 130 | chris | 90 | GameServices.getAudio().init(mSoundIds); |
| 91 | |||
| 136 | chris | 92 | SharedPreferences myPrefs = ResourceManager.getInstance().getCurrentContext().getSharedPreferences(GameConsts.PREF_NAME, Context.MODE_PRIVATE); |
| 93 | boolean soundsEnabled = myPrefs.getBoolean(GameConsts.PREF_AUDIO_ENABLED, true); |
||
| 94 | enableSound(soundsEnabled); |
||
| 95 | |||
| 108 | chris | 96 | mTitle = new Sprite2D(); |
| 97 | mTitle.init(R.drawable.logo, GameConsts.VIRTUAL_SCREEN_WIDTH/2.0f, GameConsts.TITLE_POSITION_Y, GameConsts.TITLE_WIDTH, GameConsts.TITLE_HEIGHT); |
||
| 98 | mTitle.pivotX = GameConsts.TITLE_WIDTH / 2.0f; |
||
| 99 | mTitle.pivotY = 0.0f; |
||
| 138 | chris | 100 | |
| 101 | /*mTouchPoint = new Sprite2D(); |
||
| 102 | mTouchPoint.init(R.drawable.touchpoint, 0.0f, 0.0f, 64.0f, 64.0f); |
||
| 103 | mTouchPoint.pivotX = mTouchPoint.w / 2.0f; |
||
| 104 | mTouchPoint.pivotY = mTouchPoint.h / 2.0f;*/ |
||
| 108 | chris | 105 | |
| 110 | chris | 106 | mGameStartButton = new TextButton(GameServices.getFont(), ResourceManager.getInstance().getCurrentContext().getString(R.string.title_start_game)); |
| 108 | chris | 107 | mGameStartButton.x = GameConsts.VIRTUAL_SCREEN_WIDTH / 2.0f; |
| 144 | chris | 108 | mGameStartButton.y = GameConsts.TITLE_POSITION_Y + GameConsts.TITLE_HEIGHT + 0.0f; |
| 117 | chris | 109 | mGameStartButton.scale = GameConsts.TITLE_MENU_TEXT_SCALE; |
| 108 | chris | 110 | mGameStartButton.setAlignment(TextButton.HorizontalAlignment.CENTER, TextButton.VerticalAlignment.MIDDLE); |
| 111 | mGameStartButton.setTouchListener(mGameStartListener); |
||
| 112 | |||
| 110 | chris | 113 | mOptionsButton = new TextButton(GameServices.getFont(), ResourceManager.getInstance().getCurrentContext().getString(R.string.title_music_on)); |
| 108 | chris | 114 | mOptionsButton.x = GameConsts.VIRTUAL_SCREEN_WIDTH / 2.0f; |
| 144 | chris | 115 | mOptionsButton.y = GameConsts.TITLE_POSITION_Y + GameConsts.TITLE_HEIGHT + 40.0f; |
| 117 | chris | 116 | mOptionsButton.scale = GameConsts.TITLE_MENU_TEXT_SCALE; |
| 108 | chris | 117 | mOptionsButton.setAlignment(TextButton.HorizontalAlignment.CENTER, TextButton.VerticalAlignment.MIDDLE); |
| 118 | mOptionsButton.setTouchListener(mOptionsListener); |
||
| 119 | |||
| 110 | chris | 120 | mHelpButton = new TextButton(GameServices.getFont(), ResourceManager.getInstance().getCurrentContext().getString(R.string.title_help)); |
| 108 | chris | 121 | mHelpButton.x = GameConsts.VIRTUAL_SCREEN_WIDTH / 2.0f; |
| 144 | chris | 122 | mHelpButton.y = GameConsts.TITLE_POSITION_Y + GameConsts.TITLE_HEIGHT + 80.0f; |
| 117 | chris | 123 | mHelpButton.scale = GameConsts.TITLE_MENU_TEXT_SCALE; |
| 108 | chris | 124 | mHelpButton.setAlignment(TextButton.HorizontalAlignment.CENTER, TextButton.VerticalAlignment.MIDDLE); |
| 125 | mHelpButton.setTouchListener(mHelpListener); |
||
| 117 | chris | 126 | |
| 144 | chris | 127 | mCreditsButton = new TextButton(GameServices.getFont(), ResourceManager.getInstance().getCurrentContext().getString(R.string.title_credits)); |
| 128 | mCreditsButton.x = GameConsts.VIRTUAL_SCREEN_WIDTH / 2.0f; |
||
| 129 | mCreditsButton.y = GameConsts.TITLE_POSITION_Y + GameConsts.TITLE_HEIGHT + 120.0f; |
||
| 130 | mCreditsButton.scale = GameConsts.TITLE_MENU_TEXT_SCALE; |
||
| 131 | mCreditsButton.setAlignment(TextButton.HorizontalAlignment.CENTER, TextButton.VerticalAlignment.MIDDLE); |
||
| 132 | mCreditsButton.setTouchListener(mCreditsListener); |
||
| 133 | |||
| 118 | chris | 134 | mHelp = new HelpScreen(GameConsts.VIRTUAL_SCREEN_HEIGHT); |
| 108 | chris | 135 | } |
| 136 | |||
| 137 | @Override |
||
| 138 | public void exit() |
||
| 139 | { |
||
| 140 | mGameStartButton = null; |
||
| 141 | mOptionsButton = null; |
||
| 144 | chris | 142 | mCreditsButton = null; |
| 108 | chris | 143 | mHelpButton = null; |
| 130 | chris | 144 | |
| 145 | GameServices.getAudio().exit(); |
||
| 108 | chris | 146 | } |
| 147 | |||
| 136 | chris | 148 | public void enableSound(boolean enable) |
| 149 | { |
||
| 150 | mSoundEnabled = enable; |
||
| 151 | GameServices.getAudio().setSoundsOn(mSoundEnabled); |
||
| 152 | |||
| 153 | // store in prefs |
||
| 154 | SharedPreferences myPrefs = ResourceManager.getInstance().getCurrentContext().getSharedPreferences(GameConsts.PREF_NAME, Context.MODE_PRIVATE); |
||
| 155 | SharedPreferences.Editor prefsEditor = myPrefs.edit(); |
||
| 156 | prefsEditor.putBoolean(GameConsts.PREF_AUDIO_ENABLED, enable); |
||
| 157 | //prefsEditor.putBoolean(GameConsts.PREF_MUSIC_ENABLED), enable); |
||
| 158 | prefsEditor.commit(); |
||
| 159 | } |
||
| 160 | |||
| 108 | chris | 161 | @Override |
| 162 | public void onSurfaceChanged(int width, int height) |
||
| 163 | { |
||
| 164 | GLUtil.getGL().glViewport(0, 0, width, height); |
||
| 165 | } |
||
| 166 | |||
| 167 | @Override |
||
| 168 | public void update(float deltaTime) |
||
| 169 | { |
||
| 170 | mTimer += deltaTime; |
||
| 171 | mTitle.update(deltaTime); |
||
| 172 | |||
| 173 | /* Finger finger = MultitouchInput.getInstance().getTouchPointInside(0.0f, 0.0f, GameConsts.VIRTUAL_SCREEN_WIDTH, GameConsts.VIRTUAL_SCREEN_HEIGHT); |
||
| 174 | if (finger != null) |
||
| 175 | { |
||
| 176 | // switch state |
||
| 177 | GameStateManager.getInstance().switchState("MainGameState"); |
||
| 178 | }*/ |
||
| 179 | |||
| 117 | chris | 180 | if (!mHelp.isActive()) |
| 181 | { |
||
| 182 | mGameStartButton.update(deltaTime); |
||
| 183 | mOptionsButton.update(deltaTime); |
||
| 184 | mHelpButton.update(deltaTime); |
||
| 144 | chris | 185 | mCreditsButton.update(deltaTime); |
| 117 | chris | 186 | } |
| 187 | |||
| 188 | mHelp.update(deltaTime); |
||
| 108 | chris | 189 | } |
| 190 | |||
| 191 | @Override |
||
| 192 | public void render() |
||
| 193 | { |
||
| 194 | GL10 gl = GLUtil.getGL(); |
||
| 195 | |||
| 196 | gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
||
| 197 | gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); |
||
| 198 | |||
| 199 | gl.glMatrixMode(GL10.GL_PROJECTION); |
||
| 200 | gl.glLoadIdentity(); |
||
| 201 | gl.glOrthof(0, GameConsts.VIRTUAL_SCREEN_WIDTH-1.0f, GameConsts.VIRTUAL_SCREEN_HEIGHT-1.0f, 0, 0, 1); |
||
| 202 | |||
| 112 | chris | 203 | float alpha = GameServices.getRandomFloat(0.7f, 1.0f); |
| 108 | chris | 204 | mTitle.setColor(GameConsts.PINGK_COLOR.x, GameConsts.PINGK_COLOR.y, GameConsts.PINGK_COLOR.z, alpha); |
| 205 | mTitle.render(); |
||
| 206 | |||
| 207 | /* if (Math.cos(mTimer * GameConsts.TITLE_FLASH_INTERVAL) > 0.0f) |
||
| 208 | { |
||
| 209 | Log.v(GameConsts.LOG_TAG, "t=" + mTimer); |
||
| 210 | renderText(R.string.title_touch_to_start, 300.0f, 3.0f); |
||
| 211 | }*/ |
||
| 212 | |||
| 213 | mGameStartButton.render(); |
||
| 214 | mOptionsButton.render(); |
||
| 215 | mHelpButton.render(); |
||
| 146 | chris | 216 | mCreditsButton.render(); |
| 117 | chris | 217 | mHelp.render(); |
| 138 | chris | 218 | |
| 146 | chris | 219 | |
| 138 | chris | 220 | /* MultitouchInput mt = MultitouchInput.getInstance(); |
| 221 | for (int i = 0; i < mt.getNumFingers(); i++) |
||
| 222 | { |
||
| 223 | MultitouchInput.Finger finger = mt.getFinger(i); |
||
| 224 | |||
| 225 | mTouchPoint.x = finger.x; |
||
| 226 | mTouchPoint.y = finger.y; |
||
| 227 | |||
| 228 | mTouchPoint.render(); |
||
| 229 | }*/ |
||
| 108 | chris | 230 | } |
| 231 | |||
| 232 | /* public void renderText(int id, float posY, float scale) |
||
| 233 | { |
||
| 234 | String text = GameStateManager.getInstance().getContext().getString(id); |
||
| 235 | |||
| 236 | float w = text.length() * Font.CHARACTER_SIZE * scale; |
||
| 237 | |||
| 238 | mFont.drawText(text, (GameConsts.VIRTUAL_SCREEN_WIDTH - w) / 2.0f, posY, scale); |
||
| 239 | } |
||
| 240 | */ |
||
| 241 | @Override |
||
| 242 | public boolean doFadeIn() |
||
| 243 | { |
||
| 244 | return true; |
||
| 245 | } |
||
| 246 | |||
| 247 | @Override |
||
| 248 | public boolean doFadeOut() |
||
| 249 | { |
||
| 250 | return true; |
||
| 251 | } |
||
| 252 | } |