Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1671 | chris | 1 | package com.gebauz.bauzoid.app; |
| 2 | |||
| 3 | import com.badlogic.gdx.ApplicationListener; |
||
| 4 | import com.gebauz.bauzoid.game.Game; |
||
| 5 | import com.gebauz.bauzoid.game.Game.IGameFactory; |
||
| 6 | |||
| 7 | /** Base class for applications using Bauzoid engine. |
||
| 8 | * Implemented as a Generic, T is reached through to Game<T> |
||
| 9 | */ |
||
| 10 | public abstract class BauzoidApp implements ApplicationListener |
||
| 11 | { |
||
| 12 | public interface IAdHandler |
||
| 13 | { |
||
| 14 | public void show(boolean visible); |
||
| 15 | |||
| 16 | public void onResume(); |
||
| 17 | public void onPause(); |
||
| 18 | public void onDestroy(); |
||
| 19 | } |
||
| 20 | |||
| 21 | private String mGameTitle = "BauzoidGame"; |
||
| 22 | protected Game mGame = null; |
||
| 23 | private long mSystemTimeMs = System.currentTimeMillis(); |
||
| 24 | private boolean mAdsEnabled = false; |
||
| 25 | |||
| 26 | private IGameFactory mGameFactory = null; |
||
| 27 | private IAdHandler mAdHandler = null; |
||
| 28 | |||
| 29 | public BauzoidApp(String title, boolean adsEnabled) |
||
| 30 | { |
||
| 31 | this(title, adsEnabled, null, null); |
||
| 32 | } |
||
| 33 | |||
| 34 | public BauzoidApp(String title, boolean adsEnabled, IGameFactory gameFactory) |
||
| 35 | { |
||
| 36 | this(title, adsEnabled, gameFactory, null); |
||
| 37 | } |
||
| 38 | |||
| 39 | public BauzoidApp(String title, boolean adsEnabled, IAdHandler handler) |
||
| 40 | { |
||
| 41 | this(title, adsEnabled, null, handler); |
||
| 42 | } |
||
| 43 | |||
| 44 | public BauzoidApp(String title, boolean adsEnabled, IGameFactory gameFactory, IAdHandler handler) |
||
| 45 | { |
||
| 46 | mGameTitle = title; |
||
| 47 | mAdsEnabled = adsEnabled; |
||
| 48 | mGameFactory = gameFactory; |
||
| 49 | mAdHandler = handler; |
||
| 50 | } |
||
| 51 | |||
| 52 | @Override |
||
| 53 | public void create() |
||
| 54 | { |
||
| 55 | mGame = new Game(this, mGameTitle, mAdsEnabled, mGameFactory); |
||
| 56 | mGame.init(); |
||
| 57 | |||
| 58 | initGame(); |
||
| 59 | |||
| 60 | mSystemTimeMs = System.currentTimeMillis(); |
||
| 61 | } |
||
| 62 | |||
| 63 | @Override |
||
| 64 | public void dispose() |
||
| 65 | { |
||
| 66 | exitGame(); |
||
| 67 | |||
| 68 | if (mAdHandler != null) |
||
| 69 | mAdHandler.onDestroy(); |
||
| 70 | |||
| 71 | mGame.exit(); |
||
| 72 | mGame = null; |
||
| 73 | } |
||
| 74 | |||
| 75 | @Override |
||
| 76 | public void pause() |
||
| 77 | { |
||
| 78 | if (mAdHandler != null) |
||
| 79 | mAdHandler.onPause(); |
||
| 80 | |||
| 81 | mGame.pause(); |
||
| 82 | } |
||
| 83 | |||
| 84 | @Override |
||
| 85 | public void resume() |
||
| 86 | { |
||
| 87 | mGame.resume(); |
||
| 88 | |||
| 89 | if (mAdHandler != null) |
||
| 90 | mAdHandler.onResume(); |
||
| 91 | } |
||
| 92 | |||
| 93 | @Override |
||
| 94 | public void resize(int w, int h) |
||
| 95 | { |
||
| 96 | mGame.resize(w, h); |
||
| 97 | } |
||
| 98 | |||
| 99 | @Override |
||
| 100 | public void render() |
||
| 101 | { |
||
| 102 | float deltaTime = updateInternalTimer(); |
||
| 103 | |||
| 104 | //deltaTime = 1/5.0f; |
||
| 105 | |||
| 106 | mGame.update(deltaTime); |
||
| 107 | |||
| 108 | /*try |
||
| 109 | { |
||
| 110 | Thread.sleep(500); |
||
| 111 | } |
||
| 112 | catch (InterruptedException e) |
||
| 113 | { |
||
| 114 | // TODO Auto-generated catch block |
||
| 115 | e.printStackTrace(); |
||
| 116 | }*/ |
||
| 117 | |||
| 118 | mGame.render(); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** Update the internal timer and return the resulting deltaTime. Can be used for times when a single method will use a lot of time. */ |
||
| 122 | public float updateInternalTimer() |
||
| 123 | { |
||
| 124 | long currentTimeMs = System.currentTimeMillis(); |
||
| 125 | long deltaTimeMs = currentTimeMs - mSystemTimeMs; |
||
| 126 | mSystemTimeMs = currentTimeMs; |
||
| 127 | |||
| 128 | return (float)deltaTimeMs / 1000.0f; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** Called after the engine internals have been initialized - this is the point to kick off the initial game state. */ |
||
| 132 | public abstract void initGame(); |
||
| 133 | |||
| 134 | /** Called before the engine internals are cleaned up. */ |
||
| 135 | public abstract void exitGame(); |
||
| 136 | |||
| 137 | /** Get the instance of the game. */ |
||
| 138 | public Game getGame() |
||
| 139 | { |
||
| 140 | return mGame; |
||
| 141 | } |
||
| 142 | |||
| 143 | public long getSystemTimeMs() |
||
| 144 | { |
||
| 145 | return mSystemTimeMs; |
||
| 146 | } |
||
| 147 | |||
| 148 | public void showAds(boolean visible) |
||
| 149 | { |
||
| 150 | if (mAdHandler != null) |
||
| 151 | mAdHandler.show(visible); |
||
| 152 | } |
||
| 153 | |||
| 154 | public void setAdHandler(IAdHandler handler) |
||
| 155 | { |
||
| 156 | mAdHandler = handler; |
||
| 157 | } |
||
| 158 | |||
| 159 | } |