Rev 146 | 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 | { |
| 147 | chris | 60 | TextButton tb = (TextButton)sender; |
| 61 | if (GameServices.getAudio().isMusicEnabled()) |
||
| 62 | { |
||
| 63 | GameServices.getAudio().enableMusic(false); |
||
| 64 | tb.setText(ResourceManager.getResources().getString(R.string.title_music_off)); |
||
| 65 | } |
||
| 66 | else |
||
| 67 | { |
||
| 68 | GameServices.getAudio().enableMusic(true); |
||
| 69 | tb.setText(ResourceManager.getResources().getString(R.string.title_music_on)); |
||
| 70 | } |
||
| 108 | chris | 71 | } |
| 72 | }; |
||
| 73 | |||
| 124 | chris | 74 | BaseButton.TouchListener mHelpListener = new BaseButton.TouchListener() |
| 108 | chris | 75 | { |
| 76 | @Override |
||
| 124 | chris | 77 | public void onTouched(BaseButton sender, int tag) |
| 108 | chris | 78 | { |
| 117 | chris | 79 | mHelp.show(); |
| 108 | chris | 80 | } |
| 81 | }; |
||
| 82 | |||
| 144 | chris | 83 | BaseButton.TouchListener mCreditsListener = new BaseButton.TouchListener() |
| 84 | { |
||
| 85 | @Override |
||
| 86 | public void onTouched(BaseButton sender, int tag) |
||
| 87 | { |
||
| 88 | GameStateManager.getInstance().switchState("CreditsState"); |
||
| 89 | } |
||
| 90 | }; |
||
| 91 | |||
| 108 | chris | 92 | public TitleState() |
| 93 | { |
||
| 94 | |||
| 95 | } |
||
| 96 | |||
| 97 | @Override |
||
| 98 | public void init() |
||
| 99 | { |
||
| 147 | chris | 100 | GameServices.showAd(true); |
| 130 | chris | 101 | GameServices.getAudio().init(mSoundIds); |
| 102 | |||
| 136 | chris | 103 | SharedPreferences myPrefs = ResourceManager.getInstance().getCurrentContext().getSharedPreferences(GameConsts.PREF_NAME, Context.MODE_PRIVATE); |
| 104 | boolean soundsEnabled = myPrefs.getBoolean(GameConsts.PREF_AUDIO_ENABLED, true); |
||
| 105 | enableSound(soundsEnabled); |
||
| 106 | |||
| 108 | chris | 107 | mTitle = new Sprite2D(); |
| 108 | mTitle.init(R.drawable.logo, GameConsts.VIRTUAL_SCREEN_WIDTH/2.0f, GameConsts.TITLE_POSITION_Y, GameConsts.TITLE_WIDTH, GameConsts.TITLE_HEIGHT); |
||
| 109 | mTitle.pivotX = GameConsts.TITLE_WIDTH / 2.0f; |
||
| 110 | mTitle.pivotY = 0.0f; |
||
| 138 | chris | 111 | |
| 112 | /*mTouchPoint = new Sprite2D(); |
||
| 113 | mTouchPoint.init(R.drawable.touchpoint, 0.0f, 0.0f, 64.0f, 64.0f); |
||
| 114 | mTouchPoint.pivotX = mTouchPoint.w / 2.0f; |
||
| 115 | mTouchPoint.pivotY = mTouchPoint.h / 2.0f;*/ |
||
| 108 | chris | 116 | |
| 110 | chris | 117 | mGameStartButton = new TextButton(GameServices.getFont(), ResourceManager.getInstance().getCurrentContext().getString(R.string.title_start_game)); |
| 108 | chris | 118 | mGameStartButton.x = GameConsts.VIRTUAL_SCREEN_WIDTH / 2.0f; |
| 144 | chris | 119 | mGameStartButton.y = GameConsts.TITLE_POSITION_Y + GameConsts.TITLE_HEIGHT + 0.0f; |
| 117 | chris | 120 | mGameStartButton.scale = GameConsts.TITLE_MENU_TEXT_SCALE; |
| 108 | chris | 121 | mGameStartButton.setAlignment(TextButton.HorizontalAlignment.CENTER, TextButton.VerticalAlignment.MIDDLE); |
| 122 | mGameStartButton.setTouchListener(mGameStartListener); |
||
| 123 | |||
| 147 | chris | 124 | String musicEnabled = ResourceManager.getInstance().getCurrentContext().getString(R.string.title_music_on); |
| 125 | if (!GameServices.getAudio().isMusicEnabled()) |
||
| 126 | musicEnabled = ResourceManager.getInstance().getCurrentContext().getString(R.string.title_music_off); |
||
| 127 | mOptionsButton = new TextButton(GameServices.getFont(), musicEnabled); |
||
| 108 | chris | 128 | mOptionsButton.x = GameConsts.VIRTUAL_SCREEN_WIDTH / 2.0f; |
| 144 | chris | 129 | mOptionsButton.y = GameConsts.TITLE_POSITION_Y + GameConsts.TITLE_HEIGHT + 40.0f; |
| 117 | chris | 130 | mOptionsButton.scale = GameConsts.TITLE_MENU_TEXT_SCALE; |
| 108 | chris | 131 | mOptionsButton.setAlignment(TextButton.HorizontalAlignment.CENTER, TextButton.VerticalAlignment.MIDDLE); |
| 132 | mOptionsButton.setTouchListener(mOptionsListener); |
||
| 133 | |||
| 110 | chris | 134 | mHelpButton = new TextButton(GameServices.getFont(), ResourceManager.getInstance().getCurrentContext().getString(R.string.title_help)); |
| 108 | chris | 135 | mHelpButton.x = GameConsts.VIRTUAL_SCREEN_WIDTH / 2.0f; |
| 144 | chris | 136 | mHelpButton.y = GameConsts.TITLE_POSITION_Y + GameConsts.TITLE_HEIGHT + 80.0f; |
| 117 | chris | 137 | mHelpButton.scale = GameConsts.TITLE_MENU_TEXT_SCALE; |
| 108 | chris | 138 | mHelpButton.setAlignment(TextButton.HorizontalAlignment.CENTER, TextButton.VerticalAlignment.MIDDLE); |
| 139 | mHelpButton.setTouchListener(mHelpListener); |
||
| 117 | chris | 140 | |
| 144 | chris | 141 | mCreditsButton = new TextButton(GameServices.getFont(), ResourceManager.getInstance().getCurrentContext().getString(R.string.title_credits)); |
| 142 | mCreditsButton.x = GameConsts.VIRTUAL_SCREEN_WIDTH / 2.0f; |
||
| 143 | mCreditsButton.y = GameConsts.TITLE_POSITION_Y + GameConsts.TITLE_HEIGHT + 120.0f; |
||
| 144 | mCreditsButton.scale = GameConsts.TITLE_MENU_TEXT_SCALE; |
||
| 145 | mCreditsButton.setAlignment(TextButton.HorizontalAlignment.CENTER, TextButton.VerticalAlignment.MIDDLE); |
||
| 146 | mCreditsButton.setTouchListener(mCreditsListener); |
||
| 147 | |||
| 118 | chris | 148 | mHelp = new HelpScreen(GameConsts.VIRTUAL_SCREEN_HEIGHT); |
| 108 | chris | 149 | } |
| 150 | |||
| 151 | @Override |
||
| 152 | public void exit() |
||
| 153 | { |
||
| 154 | mGameStartButton = null; |
||
| 155 | mOptionsButton = null; |
||
| 144 | chris | 156 | mCreditsButton = null; |
| 108 | chris | 157 | mHelpButton = null; |
| 130 | chris | 158 | |
| 159 | GameServices.getAudio().exit(); |
||
| 108 | chris | 160 | } |
| 161 | |||
| 136 | chris | 162 | public void enableSound(boolean enable) |
| 163 | { |
||
| 164 | mSoundEnabled = enable; |
||
| 165 | GameServices.getAudio().setSoundsOn(mSoundEnabled); |
||
| 166 | |||
| 167 | // store in prefs |
||
| 168 | SharedPreferences myPrefs = ResourceManager.getInstance().getCurrentContext().getSharedPreferences(GameConsts.PREF_NAME, Context.MODE_PRIVATE); |
||
| 169 | SharedPreferences.Editor prefsEditor = myPrefs.edit(); |
||
| 170 | prefsEditor.putBoolean(GameConsts.PREF_AUDIO_ENABLED, enable); |
||
| 171 | //prefsEditor.putBoolean(GameConsts.PREF_MUSIC_ENABLED), enable); |
||
| 172 | prefsEditor.commit(); |
||
| 173 | } |
||
| 174 | |||
| 108 | chris | 175 | @Override |
| 176 | public void onSurfaceChanged(int width, int height) |
||
| 177 | { |
||
| 178 | GLUtil.getGL().glViewport(0, 0, width, height); |
||
| 179 | } |
||
| 180 | |||
| 181 | @Override |
||
| 182 | public void update(float deltaTime) |
||
| 183 | { |
||
| 184 | mTimer += deltaTime; |
||
| 185 | mTitle.update(deltaTime); |
||
| 186 | |||
| 187 | /* Finger finger = MultitouchInput.getInstance().getTouchPointInside(0.0f, 0.0f, GameConsts.VIRTUAL_SCREEN_WIDTH, GameConsts.VIRTUAL_SCREEN_HEIGHT); |
||
| 188 | if (finger != null) |
||
| 189 | { |
||
| 190 | // switch state |
||
| 191 | GameStateManager.getInstance().switchState("MainGameState"); |
||
| 192 | }*/ |
||
| 193 | |||
| 117 | chris | 194 | if (!mHelp.isActive()) |
| 195 | { |
||
| 196 | mGameStartButton.update(deltaTime); |
||
| 197 | mOptionsButton.update(deltaTime); |
||
| 198 | mHelpButton.update(deltaTime); |
||
| 144 | chris | 199 | mCreditsButton.update(deltaTime); |
| 117 | chris | 200 | } |
| 201 | |||
| 202 | mHelp.update(deltaTime); |
||
| 108 | chris | 203 | } |
| 204 | |||
| 205 | @Override |
||
| 206 | public void render() |
||
| 207 | { |
||
| 208 | GL10 gl = GLUtil.getGL(); |
||
| 209 | |||
| 210 | gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
||
| 211 | gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); |
||
| 212 | |||
| 213 | gl.glMatrixMode(GL10.GL_PROJECTION); |
||
| 214 | gl.glLoadIdentity(); |
||
| 215 | gl.glOrthof(0, GameConsts.VIRTUAL_SCREEN_WIDTH-1.0f, GameConsts.VIRTUAL_SCREEN_HEIGHT-1.0f, 0, 0, 1); |
||
| 216 | |||
| 112 | chris | 217 | float alpha = GameServices.getRandomFloat(0.7f, 1.0f); |
| 108 | chris | 218 | mTitle.setColor(GameConsts.PINGK_COLOR.x, GameConsts.PINGK_COLOR.y, GameConsts.PINGK_COLOR.z, alpha); |
| 219 | mTitle.render(); |
||
| 220 | |||
| 221 | /* if (Math.cos(mTimer * GameConsts.TITLE_FLASH_INTERVAL) > 0.0f) |
||
| 222 | { |
||
| 223 | Log.v(GameConsts.LOG_TAG, "t=" + mTimer); |
||
| 224 | renderText(R.string.title_touch_to_start, 300.0f, 3.0f); |
||
| 225 | }*/ |
||
| 226 | |||
| 227 | mGameStartButton.render(); |
||
| 228 | mOptionsButton.render(); |
||
| 229 | mHelpButton.render(); |
||
| 146 | chris | 230 | mCreditsButton.render(); |
| 117 | chris | 231 | mHelp.render(); |
| 138 | chris | 232 | |
| 146 | chris | 233 | |
| 138 | chris | 234 | /* MultitouchInput mt = MultitouchInput.getInstance(); |
| 235 | for (int i = 0; i < mt.getNumFingers(); i++) |
||
| 236 | { |
||
| 237 | MultitouchInput.Finger finger = mt.getFinger(i); |
||
| 238 | |||
| 239 | mTouchPoint.x = finger.x; |
||
| 240 | mTouchPoint.y = finger.y; |
||
| 241 | |||
| 242 | mTouchPoint.render(); |
||
| 243 | }*/ |
||
| 108 | chris | 244 | } |
| 245 | |||
| 246 | /* public void renderText(int id, float posY, float scale) |
||
| 247 | { |
||
| 248 | String text = GameStateManager.getInstance().getContext().getString(id); |
||
| 249 | |||
| 250 | float w = text.length() * Font.CHARACTER_SIZE * scale; |
||
| 251 | |||
| 252 | mFont.drawText(text, (GameConsts.VIRTUAL_SCREEN_WIDTH - w) / 2.0f, posY, scale); |
||
| 253 | } |
||
| 254 | */ |
||
| 255 | @Override |
||
| 256 | public boolean doFadeIn() |
||
| 257 | { |
||
| 258 | return true; |
||
| 259 | } |
||
| 260 | |||
| 261 | @Override |
||
| 262 | public boolean doFadeOut() |
||
| 263 | { |
||
| 264 | return true; |
||
| 265 | } |
||
| 266 | } |