Subversion Repositories AndroidProjects

Rev

Rev 143 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
120 chris 1
package com.gebauz.pingK.common.game.gamestates;
2
 
3
import javax.microedition.khronos.opengles.GL10;
4
 
143 chris 5
import android.content.Context;
6
import android.content.SharedPreferences;
7
 
120 chris 8
import com.gebauz.pingK.common.R;
9
import com.gebauz.pingK.common.framework.GLUtil;
127 chris 10
import com.gebauz.pingK.common.framework.MathUtil;
143 chris 11
import com.gebauz.pingK.common.framework.ResourceManager;
120 chris 12
import com.gebauz.pingK.common.framework.Sprite2D;
13
import com.gebauz.pingK.common.game.GameConsts;
127 chris 14
import com.gebauz.pingK.common.game.GameServices;
120 chris 15
import com.gebauz.pingK.common.game.MultitouchInput;
16
import com.gebauz.pingK.common.game.MultitouchInput.Finger;
17
 
18
public class LogoState extends BaseGameState
127 chris 19
{              
20
        static private float UPDATE_INTERVAL = 0.3f;
21
 
22
        public class Bar
23
        {
24
                private float x;
25
                private float y;
26
                private float w;
27
                private float h;
28
                private float alpha;
29
 
30
                private float mCurrentHeight = 0.0f;
31
                private float mTargetHeight = 0.0f;
32
                private float mUpdateTimer = GameServices.getRandomFloat(0.0f, UPDATE_INTERVAL);
33
 
34
                private float mMoveDelta;
35
 
36
                public Bar(float _x, float _y, float _w, float _h, float _alpha, float _move)
37
                {
38
                        x = _x;
39
                        y = _y;
40
                        w = _w;
41
                        h = _h;
42
                        alpha = _alpha;
43
 
44
                        mTargetHeight = GameServices.getRandomFloat(0.2f*h, h);
45
                        mCurrentHeight = _h;
46
                        mMoveDelta = _move;
47
                }
48
 
49
                public void update(float deltaTime)
50
                {
51
                        mUpdateTimer += deltaTime;
52
                        if (mUpdateTimer > UPDATE_INTERVAL)
53
                        {
54
                                mTargetHeight = GameServices.getRandomFloat(0.15f*h, h);
55
                                mUpdateTimer = 0.0f;
56
                        }
57
 
58
                        //float diff = mTargetHeight - mCurrentHeight;
59
                        //mCurrentHeight += (diff * deltaTime * 10.0f);
60
                        if (mTargetHeight > mCurrentHeight)
61
                        {
62
                                float diff = mTargetHeight - mCurrentHeight;
63
                                mCurrentHeight += (diff * deltaTime * 15.0f);
64
                        }
65
                        else if (mTargetHeight < mCurrentHeight)
66
                        {
67
                                mCurrentHeight -= 300.0f * deltaTime;
68
                                if (mCurrentHeight < mTargetHeight)
69
                                        mCurrentHeight = mTargetHeight;
70
                                /*float diff = mTargetHeight - mCurrentHeight;
71
                                mCurrentHeight += (diff * deltaTime * 3.0f);*/
72
                        }
73
 
74
                        x -= mMoveDelta * deltaTime;
75
 
76
                        if (x < (-w))
77
                        {
78
                                x = GameConsts.VIRTUAL_SCREEN_WIDTH + (w);
79
                        }
80
                }
81
 
82
                public void render()
83
                {
84
                        mBar.x = x;
85
                        mBar.y = y;
86
                        mBar.w = w;
87
                        mBar.h = mCurrentHeight;
88
                        //mBar.h = h;
89
                        mBar.pivotX = mBar.w/2.0f;
90
                        mBar.pivotY = mBar.h;
91
 
92
                        mBar.setColor(1.0f, 1.0f, 1.0f, alpha);
93
 
94
                        mBar.render();
95
                }
96
        }
97
 
98
        private int NUM_BARS = 24;
99
 
120 chris 100
        private float mTimer = 0.0f;
101
 
102
        private Sprite2D mBackground;
127 chris 103
        private Sprite2D mBar;
104
        private Sprite2D mLogo;
120 chris 105
 
143 chris 106
        private boolean mSoundEnabled = true;
107
 
127 chris 108
        private Bar[] mBars = new Bar[NUM_BARS];
109
 
110
        private float mWidths[] =
111
        {
112
                80.0f,
113
                100.0f,
114
                150.0f,
115
                250.0f
116
        };
117
 
118
        private float mHeights[] =
119
        {
120
                400.0f,
121
                380.0f,
122
                350.0f,
123
                300.0f,
124
        };
125
 
126
        private float mAlphas[] =
127
        {
128
                0.3f,
129
                0.5f,
130
                0.7f,
131
                0.9f,
132
        };
133
 
134
        private float mSpeeds[] =
135
        {
136
                50.0f,
137
                100.0f,
138
                250.0f,
139
                700.0f,
140
        };
141
 
120 chris 142
        public LogoState()
143
        {
144
        }
145
 
146
        @Override
147
        public void init()
148
        {
147 chris 149
                GameServices.showAd(false);
150
 
143 chris 151
            SharedPreferences myPrefs = ResourceManager.getInstance().getCurrentContext().getSharedPreferences(GameConsts.PREF_NAME, Context.MODE_PRIVATE);
152
        boolean soundsEnabled = myPrefs.getBoolean(GameConsts.PREF_AUDIO_ENABLED, true);
153
        enableSound(soundsEnabled);  
154
 
120 chris 155
                mBackground = new Sprite2D();
127 chris 156
                mBackground.init(R.drawable.logo_bg, 0.0f, 0.0f, GameConsts.VIRTUAL_SCREEN_WIDTH, GameConsts.VIRTUAL_SCREEN_HEIGHT);
120 chris 157
                mBackground.pivotX = 0.0f;
158
                mBackground.pivotY = 0.0f;
127 chris 159
 
160
                mBar = new Sprite2D();
161
                mBar.init(R.drawable.logo_bars);
162
 
163
                mLogo = new Sprite2D();
164
                mLogo.init(R.drawable.logo_gebauz, GameConsts.VIRTUAL_SCREEN_WIDTH/2.0f, GameConsts.VIRTUAL_SCREEN_HEIGHT/2.0f, GameConsts.LOGO_GEBAUZ_WIDTH, GameConsts.LOGO_GEBAUZ_HEIGHT);
165
                mLogo.pivotX = mLogo.w/2.0f;
166
                mLogo.pivotY = mLogo.h/2.0f;
167
 
168
                for (int i = 0; i < NUM_BARS; i++)
169
                {
170
                        int z = GameServices.getRandomInt(0, 3);
171
 
172
                        // the smaller, the farther in the back
173
                        mBars[i] = new Bar(GameServices.getRandomFloat(-mWidths[z], GameConsts.VIRTUAL_SCREEN_WIDTH + mWidths[z]), GameConsts.VIRTUAL_SCREEN_HEIGHT,
174
                                        GameServices.getRandomFloat(mWidths[z] * 0.8f, mWidths[z] * 1.2f),
175
                                        GameServices.getRandomFloat(mHeights[z] * 0.4f, mHeights[z] * 1.1f),
176
                                        GameServices.getRandomFloat(mAlphas[z] * 0.75f, mAlphas[z] * 1.0f),
177
                                        GameServices.getRandomFloat(mSpeeds[z] * 0.75f, mSpeeds[z] * 1.5f));
178
                }
143 chris 179
 
180
                GameServices.getAudio().playMusic(R.raw.music1);
120 chris 181
        }
182
 
183
        @Override
184
        public void exit()
185
        {
186
                mBackground = null;
187
        }
188
 
189
        @Override
190
        public void onSurfaceChanged(int width, int height)
191
        {
192
                GLUtil.getGL().glViewport(0, 0, width, height);
193
        }
194
 
195
        @Override
196
        public void update(float deltaTime)
197
        {
198
                mTimer += deltaTime;
199
/*                      mTitle.update(deltaTime);*/
200
 
201
                Finger finger = MultitouchInput.getInstance().getTouchPointInside(0.0f, 0.0f, GameConsts.VIRTUAL_SCREEN_WIDTH, GameConsts.VIRTUAL_SCREEN_HEIGHT);
202
                if ((finger != null) || (mTimer > GameConsts.LOGO_TIMEOUT))
203
                {
204
                        // switch state
205
                        GameStateManager.getInstance().switchState("TitleState");
206
                }
207
 
208
                mBackground.update(deltaTime);
127 chris 209
                for (int i = 0; i < NUM_BARS; i++)
210
                {
211
                        mBars[i].update(deltaTime);
212
                }
120 chris 213
 
214
/*                      if (!mHelp.isActive())
215
                        {
216
                                mGameStartButton.update(deltaTime);
217
                                mOptionsButton.update(deltaTime);
218
                                mHelpButton.update(deltaTime);
219
                        }
220
 
221
                        mHelp.update(deltaTime);*/
222
        }
223
 
224
        @Override
225
        public void render()
226
        {
227
        GL10 gl = GLUtil.getGL();
228
 
229
        gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
230
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
231
 
232
                gl.glMatrixMode(GL10.GL_PROJECTION);
233
                gl.glLoadIdentity();
234
        gl.glOrthof(0, GameConsts.VIRTUAL_SCREEN_WIDTH-1.0f, GameConsts.VIRTUAL_SCREEN_HEIGHT-1.0f, 0, 0, 1);
235
 
236
        gl.glMatrixMode(GL10.GL_MODELVIEW);
237
 
238
        mBackground.render();
127 chris 239
 
240
                for (int i = 0; i < NUM_BARS; i++)
241
                {
242
                        mBars[i].render();
243
                }
244
 
245
                if (mTimer > GameConsts.LOGO_GEBAUZ_SHOW)
246
                {
247
                        float alpha = MathUtil.clamp((mTimer - GameConsts.LOGO_GEBAUZ_SHOW) / GameConsts.LOGO_GEBAUZ_FADE_TIME, 0.0f, 1.0f);
248
                        mLogo.setColor(1.0f, 1.0f, 1.0f, alpha);
249
                        mLogo.render();
250
                }
120 chris 251
 
252
/*                      float alpha = GameServices.getRandomFloat(0.7f, 1.0f);
253
                        mTitle.setColor(GameConsts.PINGK_COLOR.x, GameConsts.PINGK_COLOR.y, GameConsts.PINGK_COLOR.z, alpha);
254
                mTitle.render();
255
 
256
                mGameStartButton.render();
257
                        mOptionsButton.render();
258
                        mHelpButton.render();
259
                        mHelp.render();*/
260
        }
261
 
262
        @Override
263
        public boolean doFadeIn()
264
        {
265
                return true;
266
        }
267
 
268
        @Override
269
        public boolean doFadeOut()
270
        {
271
                return true;
272
        }
143 chris 273
 
274
        public void enableSound(boolean enable)
275
        {
276
                mSoundEnabled = enable;
277
                GameServices.getAudio().setSoundsOn(mSoundEnabled);
278
 
279
                // store in prefs
280
                SharedPreferences myPrefs = ResourceManager.getInstance().getCurrentContext().getSharedPreferences(GameConsts.PREF_NAME, Context.MODE_PRIVATE);
281
        SharedPreferences.Editor prefsEditor = myPrefs.edit();
282
        prefsEditor.putBoolean(GameConsts.PREF_AUDIO_ENABLED, enable);
283
        //prefsEditor.putBoolean(GameConsts.PREF_MUSIC_ENABLED), enable);
284
        prefsEditor.commit();
285
        }
120 chris 286
 
287
}