Rev 835 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 835 | chris | 1 | package com.gebauz.bauzoid.game; |
| 2 | |||
| 3 | import java.util.Random; |
||
| 4 | |||
| 5 | import com.badlogic.gdx.Gdx; |
||
| 6 | import com.badlogic.gdx.InputAdapter; |
||
| 7 | import com.badlogic.gdx.assets.AssetManager; |
||
| 858 | chris | 8 | import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver; |
| 835 | chris | 9 | import com.gebauz.bauzoid.app.BauzoidApp; |
| 10 | import com.gebauz.bauzoid.app.Consts; |
||
| 11 | import com.gebauz.bauzoid.app.CustomServices; |
||
| 12 | import com.gebauz.bauzoid.app.Settings; |
||
| 13 | import com.gebauz.bauzoid.audio.Audio; |
||
| 14 | import com.gebauz.bauzoid.gamestates.GameStateManager; |
||
| 15 | import com.gebauz.bauzoid.graphics.Font; |
||
| 16 | import com.gebauz.bauzoid.graphics.FontCollection; |
||
| 17 | import com.gebauz.bauzoid.graphics.Graphics; |
||
| 858 | chris | 18 | import com.gebauz.bauzoid.graphics.sprite.AtlasDefinition; |
| 19 | import com.gebauz.bauzoid.graphics.sprite.AtlasSpriteAsyncLoader; |
||
| 835 | chris | 20 | import com.gebauz.bauzoid.input.Input; |
| 21 | |||
| 22 | /** Root object for the game. Manages game-global objects, acts as the root of the game graph, and kicks off update and render calls. |
||
| 23 | * |
||
| 24 | * This is the parent class for the following subsystems: |
||
| 25 | * - GameStateManager: Root object for game states. |
||
| 26 | * - Graphics: Root object for all graphics objects |
||
| 27 | * - Audio: Root object for all audio objects |
||
| 28 | * - .. |
||
| 29 | * |
||
| 30 | * It also offers convenient access to the Android application's Context |
||
| 31 | * |
||
| 32 | * It is implemented as a Generic, with T being the concrete implementation fo the CustomServices class for the app. |
||
| 33 | */ |
||
| 34 | public class Game |
||
| 35 | { |
||
| 36 | // Embedded Types======================================================================================= |
||
| 37 | |||
| 38 | /** Toggles debug mode and stepping mode - however, the client app must implement these functionalities. */ |
||
| 39 | public class BauzoidInputProcessor extends InputAdapter |
||
| 40 | { |
||
| 41 | @Override |
||
| 42 | public boolean keyDown (int keycode) |
||
| 43 | { |
||
| 44 | /* switch (keycode) |
||
| 45 | { |
||
| 46 | case com.badlogic.gdx.Input.Keys.BACK: |
||
| 47 | case com.badlogic.gdx.Input.Keys.D: |
||
| 48 | // toggle debug info |
||
| 49 | mEnableDebugInfo = !mEnableDebugInfo; |
||
| 50 | break; |
||
| 51 | case com.badlogic.gdx.Input.Keys.P: |
||
| 52 | // toggle debug pause mode |
||
| 53 | mEnableDebugStepperMode = !mEnableDebugStepperMode; |
||
| 54 | break; |
||
| 55 | case com.badlogic.gdx.Input.Keys.S: |
||
| 56 | // allow one step |
||
| 57 | mAllowStep = true; |
||
| 58 | break; |
||
| 59 | }*/ |
||
| 60 | return false; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | /** Interface to allow client apps to override aspects of the game. */ |
||
| 65 | public static interface IGameFactory |
||
| 66 | { |
||
| 67 | public abstract Input createInput(Game game); |
||
| 68 | public abstract Graphics createGraphics(Game game); |
||
| 69 | public abstract Audio createAudio(Game game); |
||
| 70 | public abstract FontCollection createFontCollection(Game game); |
||
| 71 | } |
||
| 72 | |||
| 73 | public static class DefaultGameFactory implements IGameFactory |
||
| 74 | { |
||
| 75 | @Override |
||
| 76 | public Input createInput(Game game) |
||
| 77 | { |
||
| 78 | return new Input(game); |
||
| 79 | } |
||
| 80 | |||
| 81 | @Override |
||
| 82 | public Graphics createGraphics(Game game) |
||
| 83 | { |
||
| 84 | return new Graphics(game); |
||
| 85 | } |
||
| 86 | |||
| 87 | @Override |
||
| 88 | public Audio createAudio(Game game) |
||
| 89 | { |
||
| 90 | return new Audio(game); |
||
| 91 | } |
||
| 92 | |||
| 93 | @Override |
||
| 94 | public FontCollection createFontCollection(Game game) |
||
| 95 | { |
||
| 96 | return new FontCollection(game); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | // Fields=============================================================================================== |
||
| 101 | |||
| 102 | private BauzoidApp mApp = null; |
||
| 103 | private String mGameTitle = "BauzoidGame"; |
||
| 104 | private boolean mAdsEnabled = false; |
||
| 105 | private GameStateManager mGameStateManager = null; |
||
| 106 | |||
| 107 | private Graphics mGraphics = null; |
||
| 108 | private Audio mAudio = null; |
||
| 109 | private Input mInput = null; |
||
| 110 | private FontCollection mFonts = null; |
||
| 111 | |||
| 112 | private final long mRandomSeed = System.currentTimeMillis(); |
||
| 113 | //private final long mRandomSeed = 1367274339256L; |
||
| 114 | private Random mRandomizer = new Random(mRandomSeed); |
||
| 115 | |||
| 116 | private AssetManager mAssetManager = null; |
||
| 117 | |||
| 118 | private Settings mSettings = null; |
||
| 119 | |||
| 120 | private CustomServices mCustomServices = null; |
||
| 121 | |||
| 122 | /** When true, allows game components to display debug information. */ |
||
| 123 | private boolean mEnableDebugInfo = false; |
||
| 124 | private boolean mEnableDebugStepperMode = false; |
||
| 125 | private boolean mAllowStep = false; |
||
| 126 | |||
| 127 | private long mPreviousTime = 0; |
||
| 128 | private int mFrameCount = 0; |
||
| 129 | private float mFps = 0.0f; |
||
| 130 | private float mRunTime = 0.0f; |
||
| 131 | private float mDeltaTime = 0.0f; |
||
| 132 | |||
| 133 | private IGameFactory mGameFactory = new DefaultGameFactory(); |
||
| 134 | |||
| 135 | // Methods============================================================================================== |
||
| 136 | |||
| 137 | /** Constructor. */ |
||
| 138 | public Game(BauzoidApp app, String title, boolean adsEnabled, IGameFactory gameFactory) |
||
| 139 | { |
||
| 140 | Gdx.app.log(Consts.LOG_TAG, "Seed Used: " + mRandomSeed); |
||
| 141 | |||
| 142 | mApp = app; |
||
| 143 | |||
| 144 | // Set the context to use for engine exceptions |
||
| 145 | mGameTitle = title; |
||
| 146 | mAdsEnabled = adsEnabled; |
||
| 147 | |||
| 148 | mGameStateManager = new GameStateManager(this); |
||
| 149 | |||
| 150 | if (gameFactory != null) |
||
| 151 | mGameFactory = gameFactory; |
||
| 152 | |||
| 153 | mGraphics = mGameFactory.createGraphics(this); |
||
| 154 | mAudio = mGameFactory.createAudio(this); |
||
| 155 | mInput = mGameFactory.createInput(this); |
||
| 156 | mFonts = mGameFactory.createFontCollection(this); |
||
| 157 | |||
| 158 | mSettings = new Settings(); |
||
| 159 | |||
| 160 | mAssetManager = new AssetManager(); |
||
| 161 | |||
| 162 | mPreviousTime = mApp.getSystemTimeMs(); |
||
| 163 | |||
| 164 | Gdx.input.setCatchBackKey(true); |
||
| 858 | chris | 165 | |
| 166 | // install asynchronous loaders |
||
| 167 | mAssetManager.setLoader(AtlasDefinition.class, new AtlasSpriteAsyncLoader(new InternalFileHandleResolver())); |
||
| 835 | chris | 168 | } |
| 169 | |||
| 170 | /** Called for game-global initialization. */ |
||
| 171 | public void init() |
||
| 172 | { |
||
| 173 | mGraphics.init(); |
||
| 174 | mAudio.init(); |
||
| 175 | mInput.init(); |
||
| 176 | mFonts.init(); |
||
| 177 | |||
| 178 | mGameStateManager.init(); |
||
| 179 | |||
| 180 | mSettings.loadSettings(Gdx.files.internal("data/config.txt")); |
||
| 181 | |||
| 182 | Gdx.input.setInputProcessor(new BauzoidInputProcessor()); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** Called for game-global destruction. */ |
||
| 186 | public void exit() |
||
| 187 | { |
||
| 188 | mGameStateManager.exit(); |
||
| 189 | |||
| 190 | mFonts.exit(); |
||
| 191 | mInput.exit(); |
||
| 192 | mAudio.exit(); |
||
| 193 | mGraphics.exit(); |
||
| 194 | |||
| 195 | if (mCustomServices != null) |
||
| 196 | { |
||
| 197 | mCustomServices.exit(); |
||
| 198 | mCustomServices = null; |
||
| 199 | } |
||
| 200 | |||
| 201 | if (mAssetManager != null) |
||
| 202 | { |
||
| 203 | mAssetManager.dispose(); |
||
| 204 | mAssetManager = null; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | /** Update game logic state. */ |
||
| 209 | public void update(float deltaTime) |
||
| 210 | { |
||
| 211 | mDeltaTime = deltaTime; |
||
| 212 | mRunTime += deltaTime; |
||
| 213 | |||
| 214 | if (mCustomServices != null) |
||
| 215 | { |
||
| 216 | mCustomServices.update(deltaTime); |
||
| 217 | } |
||
| 218 | |||
| 219 | mGameStateManager.update(deltaTime); |
||
| 220 | mAudio.update(deltaTime); |
||
| 221 | mInput.update(deltaTime); |
||
| 222 | |||
| 223 | updateFps(deltaTime); |
||
| 224 | |||
| 225 | mAllowStep = false; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** Update the internal timer so that the next deltaTime won't be too large. This is used for instance during GameState initialization when init() could take a long time. */ |
||
| 229 | public void updateInternalTimer() |
||
| 230 | { |
||
| 231 | mDeltaTime = mApp.updateInternalTimer(); |
||
| 232 | mRunTime += mDeltaTime; |
||
| 233 | } |
||
| 234 | |||
| 235 | public void updateFps(float deltaTime) |
||
| 236 | { |
||
| 237 | long currentTime = getCurrentTimeMs(); |
||
| 238 | long timeInterval = currentTime - mPreviousTime; |
||
| 239 | |||
| 240 | if(timeInterval > 1000) |
||
| 241 | { |
||
| 242 | // calculate the number of frames per second |
||
| 243 | mFps = mFrameCount / (timeInterval / 1000.0f); |
||
| 244 | mPreviousTime = currentTime; |
||
| 245 | mFrameCount = 0; |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | /** Render game. */ |
||
| 250 | public void render() |
||
| 251 | { |
||
| 252 | if (mCustomServices != null) |
||
| 253 | { |
||
| 254 | mCustomServices.render(); |
||
| 255 | } |
||
| 256 | |||
| 257 | mGameStateManager.render(); |
||
| 258 | mAudio.render(); |
||
| 259 | mInput.render(); |
||
| 260 | |||
| 261 | mFrameCount++; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** Called when the surface dimensions have changed. */ |
||
| 265 | public void resize(int w, int h) |
||
| 266 | { |
||
| 267 | mGraphics.updateSurfaceDimensions(w, h); |
||
| 268 | mInput.updateSurfaceDimensions(w, h); |
||
| 269 | mGameStateManager.onSurfaceChanged(w, h); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** Called when the OpenGL Context is about to be destroyed. */ |
||
| 273 | public void pause() |
||
| 274 | { |
||
| 275 | mGraphics.onSurfaceLost(); |
||
| 276 | mAudio.onPause(); |
||
| 277 | mInput.onPause(); |
||
| 278 | mGameStateManager.onPause(); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** Called when the OpenGL Context has been recreated after onSurfaceLost(). */ |
||
| 282 | public void resume() |
||
| 283 | { |
||
| 284 | mGraphics.onSurfaceRecreated(); |
||
| 285 | mAudio.onResume(); |
||
| 286 | mInput.onResume(); |
||
| 287 | mGameStateManager.onResume(); |
||
| 288 | } |
||
| 289 | |||
| 290 | /** Install an app specific custom services. */ |
||
| 291 | public final void setCustomServices(CustomServices customServices) |
||
| 292 | { |
||
| 293 | if (mCustomServices != null) |
||
| 294 | mCustomServices.exit(); |
||
| 295 | |||
| 296 | mCustomServices = customServices; |
||
| 297 | |||
| 298 | if (mCustomServices != null) |
||
| 299 | mCustomServices.init(); |
||
| 300 | } |
||
| 301 | |||
| 302 | public float getRandomFloat(float min, float max) |
||
| 303 | { |
||
| 304 | return min + mRandomizer.nextFloat() * (max-min); |
||
| 305 | } |
||
| 306 | |||
| 307 | public int getRandomInt(int min, int max) |
||
| 308 | { |
||
| 309 | return min + mRandomizer.nextInt(max-min+1); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** Returns -1 or 1 with a 50:50 chance. */ |
||
| 313 | public int getRandomSign() |
||
| 314 | { |
||
| 315 | int n = getRandomInt(0, 1); |
||
| 316 | if (n == 1) |
||
| 317 | return -1; |
||
| 318 | return 1; |
||
| 319 | } |
||
| 320 | |||
| 321 | public void showAds(boolean visible) |
||
| 322 | { |
||
| 323 | mApp.showAds(visible); |
||
| 324 | } |
||
| 325 | |||
| 326 | // Getters/Setters================================================================================== |
||
| 327 | |||
| 328 | /** Get the main graphics object. */ |
||
| 329 | public final Graphics getGraphics() { return mGraphics; } |
||
| 330 | |||
| 331 | /** Get the main audio object. */ |
||
| 332 | public final Audio getAudio() { return mAudio; } |
||
| 333 | |||
| 334 | /** Get the main input object. */ |
||
| 335 | public final Input getInput() { return mInput; } |
||
| 336 | |||
| 337 | /** Get the font collection. */ |
||
| 338 | public final FontCollection getFonts() { return mFonts; } |
||
| 339 | |||
| 340 | /** Get a specific font. */ |
||
| 341 | public final Font getFont(String name) { return mFonts.getFont(name); } |
||
| 342 | |||
| 343 | /** Get the app specific custom services. */ |
||
| 344 | public final CustomServices getCustomServices() { return mCustomServices; } |
||
| 345 | |||
| 346 | /** Get the GameStateManager. */ |
||
| 347 | public final GameStateManager getGameStateManager() { return mGameStateManager; } |
||
| 348 | |||
| 349 | /** Get the Randomizer. */ |
||
| 350 | public Random getRandomizer() { return mRandomizer; } |
||
| 351 | |||
| 352 | /** Get the AssetManager. */ |
||
| 353 | public AssetManager getAssetManager() { return mAssetManager; } |
||
| 354 | |||
| 355 | /** Get the Game Title. */ |
||
| 356 | public final String getGameTitle() { return mGameTitle; } |
||
| 357 | |||
| 358 | /** Check if ads are enabled. */ |
||
| 359 | public final boolean areAdsEnabled() { return mAdsEnabled; } |
||
| 360 | |||
| 361 | /** Set a settings object. */ |
||
| 362 | public final void setSettings(Settings settings) { mSettings = settings; } |
||
| 363 | |||
| 364 | /** Get the settings object. */ |
||
| 365 | public final Settings getSettings() { return mSettings; } |
||
| 366 | |||
| 367 | /** Get the current system time in milliseconds. */ |
||
| 368 | public final long getCurrentTimeMs() { return mApp.getSystemTimeMs(); } |
||
| 369 | |||
| 370 | /** Get the time the game has run in seconds. Useful for sine/cosine stuffs and animating. */ |
||
| 371 | public final float getTime() { return mRunTime; } |
||
| 372 | |||
| 373 | /** Get the frames per second. */ |
||
| 374 | public final float getFps() { return mFps; } |
||
| 375 | |||
| 376 | /** Get seconds per frame. */ |
||
| 377 | public final float getFrameTime() |
||
| 378 | { |
||
| 379 | if (mFps == 0.0f) |
||
| 380 | return 0.0f; |
||
| 381 | return (1/mFps); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** Get the last frame's delta time. */ |
||
| 385 | public final float getDeltaTime() { return mDeltaTime; } |
||
| 386 | |||
| 387 | public boolean isDebugInfoEnabled() { return mEnableDebugInfo; } |
||
| 388 | public void enableDebugInfo(boolean enableDebugInfo) { mEnableDebugInfo = enableDebugInfo; } |
||
| 389 | |||
| 390 | public boolean isDebugStepperModeEnabled() { return mEnableDebugStepperMode; } |
||
| 391 | public boolean stepNextFrame() { return mAllowStep; } |
||
| 392 | } |