Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 200 | chris | 1 | package com.gebauz.Bauzoid.gamestates; |
| 2 | |||
| 3 | import java.lang.reflect.InvocationTargetException; |
||
| 4 | |||
| 5 | import com.gebauz.Bauzoid.app.Consts; |
||
| 6 | import com.gebauz.Bauzoid.app.Game; |
||
| 7 | import com.gebauz.Bauzoid.app.GameObject; |
||
| 8 | import com.gebauz.Bauzoid.math.MathUtil; |
||
| 9 | |||
| 10 | /** Manages game state switching, updating, rendering, and initialization/destruction. */ |
||
| 11 | public class GameStateManager extends GameObject |
||
| 12 | { |
||
| 13 | static final float FADE_TIME = 1.0f; |
||
| 14 | |||
| 15 | private enum StateMode |
||
| 16 | { |
||
| 17 | MODE_NORMAL, // everything normal |
||
| 18 | MODE_FADEIN, // fade in |
||
| 19 | MODE_FADEOUT // fade out |
||
| 20 | }; |
||
| 21 | |||
| 22 | private BaseGameState mCurrentState = null; |
||
| 23 | private Class<?> mNextStateClass = null; |
||
| 24 | private StateMode mStateMode = StateMode.MODE_NORMAL; |
||
| 25 | private float mFadeTimer = 0.0f; |
||
| 26 | |||
| 27 | //private SimpleGeometry mFadeOverlay = null; |
||
| 28 | |||
| 29 | /** Constructor. */ |
||
| 30 | public GameStateManager(Game game) |
||
| 31 | { |
||
| 32 | super(game); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** Called when the GameStateManager should initially set up its internals. */ |
||
| 36 | public void init() |
||
| 37 | { |
||
| 38 | |||
| 39 | } |
||
| 40 | |||
| 41 | /** Called when the GameStateManager should clean up. */ |
||
| 42 | public void exit() |
||
| 43 | { |
||
| 44 | if (mCurrentState != null) |
||
| 45 | { |
||
| 46 | // reset everything |
||
| 47 | |||
| 48 | mCurrentState.exit(); |
||
| 49 | mCurrentState = null; |
||
| 50 | |||
| 51 | mNextStateClass = null; |
||
| 52 | mStateMode = StateMode.MODE_NORMAL; |
||
| 53 | mFadeTimer = 0.0f; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | /** Queue a game state switch. */ |
||
| 58 | public void switchTo(Class<?> nextState) |
||
| 59 | { |
||
| 60 | if ((mCurrentState != null) && (mCurrentState.doFadeOut())) |
||
| 61 | { |
||
| 62 | mStateMode = StateMode.MODE_FADEOUT; |
||
| 63 | mFadeTimer = 0.0f; |
||
| 64 | } |
||
| 65 | |||
| 66 | mNextStateClass = nextState; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** Switch to state specified by String - must be fully qualified Class name! */ |
||
| 70 | public void switchTo(String nextState) |
||
| 71 | { |
||
| 72 | try |
||
| 73 | { |
||
| 74 | mNextStateClass = Class.forName(nextState); |
||
| 75 | } |
||
| 76 | catch (ClassNotFoundException ex) |
||
| 77 | { |
||
| 78 | // Log.v(Consts.LOG_TAG, "ClassNotFoundException when switching to " + nextState); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | /** Actually perform the switch. */ |
||
| 83 | private void performSwitch(Class<?> newState) |
||
| 84 | { |
||
| 85 | if (newState == null) |
||
| 86 | { |
||
| 87 | return; |
||
| 88 | } |
||
| 89 | |||
| 90 | if (mCurrentState != null) |
||
| 91 | { |
||
| 92 | mCurrentState.exit(); |
||
| 93 | mCurrentState = null; |
||
| 94 | } |
||
| 95 | |||
| 96 | try |
||
| 97 | { |
||
| 98 | //mCurrentState = (BaseGameState)newState.newInstance(); |
||
| 99 | mCurrentState = (BaseGameState)newState.getDeclaredConstructor(Game.class).newInstance(getGame()); |
||
| 100 | } |
||
| 101 | catch (NoSuchMethodException ex) |
||
| 102 | { |
||
| 103 | // Log.v(Consts.LOG_TAG, "NoSuchMethodException when switching to " + newState.toString()); |
||
| 104 | return; |
||
| 105 | } |
||
| 106 | catch (InvocationTargetException ex) |
||
| 107 | { |
||
| 108 | // Log.v(Consts.LOG_TAG, "InvocationTargetEception when switching to " + newState.toString()); |
||
| 109 | return; |
||
| 110 | } |
||
| 111 | catch (InstantiationException ex) |
||
| 112 | { |
||
| 113 | // Log.v(Consts.LOG_TAG, "InstantiationException when switching to " + newState.toString()); |
||
| 114 | return; |
||
| 115 | } |
||
| 116 | catch (IllegalAccessException ex) |
||
| 117 | { |
||
| 118 | // Log.v(Consts.LOG_TAG, "IllegalAccessException when switching to " + newState.toString()); |
||
| 119 | return; |
||
| 120 | } |
||
| 121 | |||
| 122 | if (mCurrentState != null) |
||
| 123 | { |
||
| 124 | mCurrentState.init(); |
||
| 125 | //mCurrentState.onSurfaceChanged(GLUtil.getInstance().getWidth(), GLUtil.getInstance().getHeight()); |
||
| 126 | |||
| 127 | if (mCurrentState.doFadeIn()) |
||
| 128 | { |
||
| 129 | mStateMode = StateMode.MODE_FADEIN; |
||
| 130 | mFadeTimer = 0.0f; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** Update the current game state (if one is running). */ |
||
| 136 | public void update(float deltaTime) |
||
| 137 | { |
||
| 138 | if (mStateMode != StateMode.MODE_NORMAL) |
||
| 139 | { |
||
| 140 | mFadeTimer += deltaTime; |
||
| 141 | if (mFadeTimer > FADE_TIME) |
||
| 142 | { |
||
| 143 | mStateMode = StateMode.MODE_NORMAL; |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | if ((mStateMode == StateMode.MODE_NORMAL) && (mNextStateClass != null)) |
||
| 148 | { |
||
| 149 | // perform the actual switch |
||
| 150 | performSwitch(mNextStateClass); |
||
| 151 | mNextStateClass = null; |
||
| 152 | } |
||
| 153 | |||
| 154 | if ((mCurrentState != null) && (mStateMode == StateMode.MODE_NORMAL)) |
||
| 155 | { |
||
| 156 | mCurrentState.update(deltaTime); |
||
| 157 | } |
||
| 158 | |||
| 159 | float alpha = MathUtil.clamp(mFadeTimer / FADE_TIME, 0.0f, 1.0f); |
||
| 160 | if (mStateMode == StateMode.MODE_FADEOUT) |
||
| 161 | alpha = 1.0f - alpha; |
||
| 162 | |||
| 163 | // TODO: do it with shader! |
||
| 164 | /* Mesh.AttributeArray colors = new Mesh.AttributeArray(Mesh.COLOR_ELEMENTS_COUNT * mFadeOverlay.getColorCount()); |
||
| 165 | for (int i = 0; i < mFadeOverlay.getColorCount(); i++) |
||
| 166 | { |
||
| 167 | colors.fill(alpha, alpha, alpha, 1.0f); |
||
| 168 | //colors.fill(1.0f, 1.0f, 1.0f, alpha); |
||
| 169 | } |
||
| 170 | mFadeOverlay.setColors(colors.getAttributeArray());*/ |
||
| 171 | } |
||
| 172 | |||
| 173 | /** Render the current game state (if one is running). */ |
||
| 174 | public void render() |
||
| 175 | { |
||
| 176 | if (mCurrentState != null) |
||
| 177 | { |
||
| 178 | mCurrentState.render(); |
||
| 179 | } |
||
| 180 | |||
| 181 | if (mStateMode != StateMode.MODE_NORMAL) |
||
| 182 | { |
||
| 183 | /* gl.glMatrixMode(GL10.GL_PROJECTION); |
||
| 184 | gl.glLoadIdentity(); |
||
| 185 | gl.glOrthof(0, GameConsts.VIRTUAL_SCREEN_WIDTH-1.0f, GameConsts.VIRTUAL_SCREEN_HEIGHT-1.0f, 0, 0, 1); |
||
| 186 | gl.glMatrixMode(GL10.GL_MODELVIEW); |
||
| 187 | gl.glLoadIdentity(); |
||
| 188 | |||
| 189 | |||
| 190 | if (mFadeTimer <= GameConsts.FADE_TIME) |
||
| 191 | { |
||
| 192 | RenderStates rs = GLUtil.getRenderStates(); |
||
| 193 | rs.blending.setEnabled(true); |
||
| 194 | rs.blending.setBlendingMode(BlendingMode.MULTIPLY); |
||
| 195 | rs.depthTest.setEnabled(false); |
||
| 196 | rs.culling.setEnabled(false); |
||
| 197 | rs.getTextureStage(0).bindTexture(null, true); |
||
| 198 | rs.activate(); |
||
| 199 | mFadeOverlay.render(); |
||
| 200 | rs.deactivate(); |
||
| 201 | }*/ |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | /** Called when the surface properties changed. */ |
||
| 206 | public void onSurfaceChanged(int w, int h) |
||
| 207 | { |
||
| 208 | if (mCurrentState != null) |
||
| 209 | { |
||
| 210 | mCurrentState.onSurfaceChanged(w, h); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | /** Called when resuming after pausing and initially. */ |
||
| 215 | public void onResume() |
||
| 216 | { |
||
| 217 | |||
| 218 | } |
||
| 219 | |||
| 220 | /** Called when the app is paused. */ |
||
| 221 | public void onPause() |
||
| 222 | { |
||
| 223 | |||
| 224 | } |
||
| 225 | |||
| 226 | } |