My favorites | Sign in
Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.divideandconquer;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;
import android.graphics.Color;

import java.util.Stack;

/**
* The activity for the game. Listens for callbacks from the game engine, and
* response appropriately, such as bringing up a 'game over' dialog when a ball
* hits a moving line and there is only one life left.
*/
public class DivideAndConquerActivity extends Activity
implements DivideAndConquerView.BallEngineCallBack,
NewGameCallback,
DialogInterface.OnCancelListener {

private static final int NEW_GAME_NUM_BALLS = 1;
private static final double LEVEL_UP_THRESHOLD = 0.8;
private static final int COLLISION_VIBRATE_MILLIS = 50;

private boolean mVibrateOn;

private int mNumBalls = NEW_GAME_NUM_BALLS;

private DivideAndConquerView mBallsView;

private static final int WELCOME_DIALOG = 20;
private static final int GAME_OVER_DIALOG = 21;
private WelcomeDialog mWelcomeDialog;
private GameOverDialog mGameOverDialog;

private TextView mLivesLeft;
private TextView mPercentContained;
private int mNumLives;
private Vibrator mVibrator;
private TextView mLevelInfo;
private int mNumLivesStart = 5;

private Toast mCurrentToast;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Turn off the title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.main);
mBallsView = (DivideAndConquerView) findViewById(R.id.ballsView);
mBallsView.setCallback(this);

mPercentContained = (TextView) findViewById(R.id.percentContained);
mLevelInfo = (TextView) findViewById(R.id.levelInfo);
mLivesLeft = (TextView) findViewById(R.id.livesLeft);

// we'll vibrate when the ball hits the moving line
mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
}

/** {@inheritDoc} */
public void onEngineReady(BallEngine ballEngine) {
// display 10 balls bouncing around for visual effect
ballEngine.reset(SystemClock.elapsedRealtime(), 10);
mBallsView.setMode(DivideAndConquerView.Mode.Bouncing);

// show the welcome dialog
showDialog(WELCOME_DIALOG);
}

@Override
protected Dialog onCreateDialog(int id) {
if (id == WELCOME_DIALOG) {
mWelcomeDialog = new WelcomeDialog(this, this);
mWelcomeDialog.setOnCancelListener(this);
return mWelcomeDialog;
} else if (id == GAME_OVER_DIALOG) {
mGameOverDialog = new GameOverDialog(this, this);
mGameOverDialog.setOnCancelListener(this);
return mGameOverDialog;
}
return null;
}

@Override
protected void onPause() {
super.onPause();
mBallsView.setMode(DivideAndConquerView.Mode.PausedByUser);
}

@Override
protected void onResume() {
super.onResume();

mVibrateOn = PreferenceManager.getDefaultSharedPreferences(this)
.getBoolean(Preferences.KEY_VIBRATE, true);

mNumLivesStart = Preferences.getCurrentDifficulty(this).getLivesToStart();
}

private static final int MENU_NEW_GAME = Menu.FIRST;
private static final int MENU_SETTINGS = Menu.FIRST + 1;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);

menu.add(0, MENU_NEW_GAME, MENU_NEW_GAME, "New Game");
menu.add(0, MENU_SETTINGS, MENU_SETTINGS, "Settings");

return true;
}

/**
* We pause the game while the menu is open; this remembers what it was
* so we can restore when the menu closes
*/
Stack<DivideAndConquerView.Mode> mRestoreMode = new Stack<DivideAndConquerView.Mode>();

@Override
public boolean onMenuOpened(int featureId, Menu menu) {
saveMode();
mBallsView.setMode(DivideAndConquerView.Mode.Paused);
return super.onMenuOpened(featureId, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);

switch (item.getItemId()) {
case MENU_NEW_GAME:
cancelToasts();
onNewGame();
break;
case MENU_SETTINGS:
final Intent intent = new Intent();
intent.setClass(this, Preferences.class);
startActivity(intent);
break;
}

mRestoreMode.pop(); // don't want to restore when an action was taken

return true;
}

@Override
public void onOptionsMenuClosed(Menu menu) {
super.onOptionsMenuClosed(menu);
restoreMode();
}


private void saveMode() {
// don't want to restore to a state where user can't resume game.
final DivideAndConquerView.Mode mode = mBallsView.getMode();
final DivideAndConquerView.Mode toRestore = (mode == DivideAndConquerView.Mode.Paused) ?
DivideAndConquerView.Mode.PausedByUser : mode;
mRestoreMode.push(toRestore);
}

private void restoreMode() {
if (!mRestoreMode.isEmpty()) {
mBallsView.setMode(mRestoreMode.pop());
}
}

/** {@inheritDoc} */
public void onBallHitsMovingLine(final BallEngine ballEngine, float x, float y) {
if (--mNumLives == 0) {
saveMode();
mBallsView.setMode(DivideAndConquerView.Mode.Paused);

// vibrate three times
if (mVibrateOn) {
mVibrator.vibrate(
new long[]{0l, COLLISION_VIBRATE_MILLIS,
50l, COLLISION_VIBRATE_MILLIS,
50l, COLLISION_VIBRATE_MILLIS},
-1);
}
showDialog(GAME_OVER_DIALOG);
} else {
if (mVibrateOn) {
mVibrator.vibrate(COLLISION_VIBRATE_MILLIS);
}
updateLivesDisplay(mNumLives);
if (mNumLives <= 1) {
mBallsView.postDelayed(mOneLifeToastRunnable, 700);
} else {
mBallsView.postDelayed(mLivesBlinkRedRunnable, 700);
}
}
}

private Runnable mOneLifeToastRunnable = new Runnable() {
public void run() {
showToast("1 life left!");
}
};

private Runnable mLivesBlinkRedRunnable = new Runnable() {
public void run() {
mLivesLeft.setTextColor(Color.RED);
mLivesLeft.postDelayed(mLivesTextWhiteRunnable, 2000);
}
};

/** {@inheritDoc} */
public void onAreaChange(final BallEngine ballEngine) {
final float percentageFilled = ballEngine.getPercentageFilled();
updatePercentDisplay(percentageFilled);
if (percentageFilled > LEVEL_UP_THRESHOLD) {
levelUp(ballEngine);
}
}

/**
* Go to the next level
* @param ballEngine The ball engine.
*/
private void levelUp(final BallEngine ballEngine) {
mNumBalls++;

updatePercentDisplay(0);
updateLevelDisplay(mNumBalls);
ballEngine.reset(SystemClock.elapsedRealtime(), mNumBalls);
mBallsView.setMode(DivideAndConquerView.Mode.Bouncing);
if (mNumBalls % 4 == 0) {
mNumLives++;
updateLivesDisplay(mNumLives);
showToast("bonus life!");
}
if (mNumBalls == 10) {
showToast("Level 10? You ROCK!");
} else if (mNumBalls == 15) {
showToast("BALLS TO THE WALL!");
}
}

private Runnable mLivesTextWhiteRunnable = new Runnable() {

public void run() {
mLivesLeft.setTextColor(Color.WHITE);
}
};

private void showToast(String text) {
cancelToasts();
mCurrentToast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
mCurrentToast.show();
}

private void cancelToasts() {
if (mCurrentToast != null) {
mCurrentToast.cancel();
mCurrentToast = null;
}
}

/**
* Update the header that displays how much of the space has been contained.
* @param amountFilled The fraction, between 0 and 1, that is filled.
*/
private void updatePercentDisplay(float amountFilled) {
final int prettyPercent = (int) (amountFilled *100);
mPercentContained.setText(
getString(R.string.percent_contained, prettyPercent));
}

/** {@inheritDoc} */
public void onNewGame() {
mNumBalls = NEW_GAME_NUM_BALLS;
mNumLives = mNumLivesStart;
updatePercentDisplay(0);
updateLivesDisplay(mNumLives);
updateLevelDisplay(mNumBalls);
mBallsView.getEngine().reset(SystemClock.elapsedRealtime(), mNumBalls);
mBallsView.setMode(DivideAndConquerView.Mode.Bouncing);
}

/**
* Update the header displaying the current level
*/
private void updateLevelDisplay(int numBalls) {
mLevelInfo.setText(getString(R.string.level, numBalls));
}

/**
* Update the display showing the number of lives left.
* @param numLives The number of lives left.
*/
void updateLivesDisplay(int numLives) {
String text = (numLives == 1) ?
getString(R.string.one_life_left) : getString(R.string.lives_left, numLives);
mLivesLeft.setText(text);
}

/** {@inheritDoc} */
public void onCancel(DialogInterface dialog) {
if (dialog == mWelcomeDialog || dialog == mGameOverDialog) {
// user hit back, they're done
finish();
}
}
}
Show details Hide details

Change log

r152 by kros...@google.com on Apr 13, 2009   Diff
Whoops, never commited the "explosion"
update from the holidays.  Was going to
clean it up a bit, but never got around to
it.. better to commit it now then lose
source forever :)
Go to: 
Project members, sign in to write a code review

Older revisions

r149 by kros...@google.com on Jan 03, 2009   Diff
v 1.2: improved graphics, balls can
collide
r134 by kros...@google.com on Oct 31, 2008   Diff
Added 'shrink to fit', removed EULA.
TODO: the way the main view knows that
the shrinkng of a region is done is
kinda hacky, I will improve that at
some point.
r93 by kros...@google.com on Oct 18, 2008   Diff
Added version information to manifest
file.  Added EULA.  Played with colors
a bit.
All revisions of this file

File info

Size: 10785 bytes, 335 lines