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