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