Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

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