Skip to content

Commit

Permalink
code improvements, updated pause button style
Browse files Browse the repository at this point in the history
- database management now is handeled exclusively in MainActivity
- GameActivity now returns a Result (Playername, Score)
- gameOver sound bugfix
- small layout tweaks
- pause button now with game button style
  • Loading branch information
vocollapse committed May 5, 2013
1 parent aef56d4 commit 4a4742c
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 50 deletions.
Binary file modified bin/classes/org/blockinger/game/db/ScoreDataSource.class
Binary file not shown.
11 changes: 6 additions & 5 deletions res/layout-land/activity_game.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@
android:id="@+id/View05"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.4" />
android:layout_weight="0.2" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2.4"
android:layout_weight="2.8"
android:gravity="center" >

<ImageButton
Expand Down Expand Up @@ -144,12 +144,12 @@
android:id="@+id/View06"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.4" />
android:layout_weight="0.2" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2.4" >
android:layout_weight="2.8" >

<ImageButton
android:id="@+id/rotateLeftButton"
Expand Down Expand Up @@ -206,6 +206,7 @@

<Button
android:id="@+id/pausebutton_1"
style="@android:style/Widget.ImageWell"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_weight="0"
Expand All @@ -217,6 +218,6 @@
android:id="@+id/top_right_spacer"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.04" />
android:layout_weight="0.05" />

</LinearLayout>
17 changes: 9 additions & 8 deletions res/layout-port/activity_game.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:layout_weight="2.4"
android:gravity="center"
android:orientation="horizontal" >

Expand Down Expand Up @@ -134,19 +134,20 @@
android:layout_weight="1.1"
android:orientation="vertical" >

<View
android:id="@+id/View01"
android:layout_width="wrap_content"
android:layout_height="8dp"
android:layout_weight="0" />

<Button
android:id="@+id/pausebutton_1"
style="@android:style/Widget.ImageWell"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="@string/pausebutton" />

<View
android:id="@+id/View01"
android:layout_width="wrap_content"
android:layout_height="8dp"
android:layout_weight="0" />

</LinearLayout>

<View
Expand All @@ -165,7 +166,7 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2" >
android:layout_weight="2.4" >

<ImageButton
android:id="@+id/rotateLeftButton"
Expand Down
4 changes: 1 addition & 3 deletions src/org/blockinger/game/activities/DefeatDialogFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Dialog onCreateDialog(Bundle savedInstance) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.defeatDialogTitle);
builder.setMessage(
getResources().getString(R.string.scoreLabel) +
getResources().getString(R.string.scoreLabel) +
"\n " + scoreString + "\n\n" +
getResources().getString(R.string.timeLabel) +
"\n " + timeString + "\n\n" +
Expand All @@ -84,8 +84,6 @@ public Dialog onCreateDialog(Bundle savedInstance) {
@Override
public void onClick(DialogInterface dialog, int which) {
((GameActivity)getActivity()).putScore(score);
//GameState.getNewInstance((GameActivity)getActivity());
getActivity().finish();
}
});
return builder.create();
Expand Down
28 changes: 13 additions & 15 deletions src/org/blockinger/game/activities/GameActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,8 @@
import org.blockinger.game.components.Sound;


import android.database.Cursor;
//import android.media.MediaPlayer;
//import android.media.MediaPlayer.OnSeekCompleteListener;
import android.content.Intent;
import android.os.Bundle;
//import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.view.MotionEvent;
import android.view.View;
Expand All @@ -71,8 +68,8 @@ public class GameActivity extends FragmentActivity {
private DefeatDialogFragment dialog;
//private int songtime;

public static final int start_new_game = 0;
public static final int resume_old_game = 1;
public static final int NEW_GAME = 0;
public static final int RESUME_GAME = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -82,7 +79,7 @@ protected void onCreate(Bundle savedInstanceState) {

/* Read Starting Arguments */
Bundle b = getIntent().getExtras();
int value = start_new_game;
int value = NEW_GAME;

/* Create Components */
game = (GameState)getLastCustomNonConfigurationInstance();
Expand All @@ -91,7 +88,7 @@ protected void onCreate(Bundle savedInstanceState) {
if(b!=null)
value = b.getInt("mode");

if((value == start_new_game)) {
if((value == NEW_GAME)) {
game = GameState.getNewInstance(this);
game.setLevel(b.getInt("level"));
} else
Expand Down Expand Up @@ -202,7 +199,7 @@ public boolean onTouch(View v, MotionEvent event) {

((BlockBoardView)findViewById(R.id.boardView)).init();
((BlockBoardView)findViewById(R.id.boardView)).setHost(this);
MainActivity.getAdapter().notifyDataSetChanged();
//MainActivity.getAdapter().notifyDataSetChanged(); TODO: wtf.
}

/**
Expand Down Expand Up @@ -241,10 +238,13 @@ public void putScore(long score) {
String playerName = game.getPlayerName();
if(playerName == null || playerName.equals(""))
playerName = getResources().getString(R.string.anonymous);//"Anonymous";
MainActivity.getDS().createScore(score, playerName);
MainActivity.getDS().open();
Cursor cursor = MainActivity.getDS().getCursor();
MainActivity.getAdapter().changeCursor(cursor);

Intent data = new Intent();
data.putExtra(MainActivity.PLAYERNAME_KEY, playerName);
data.putExtra(MainActivity.SCORE_KEY, score);
setResult(MainActivity.RESULT_OK, data);

finish();
}

@Override
Expand All @@ -260,7 +260,6 @@ protected void onDestroy() {
sound.release();
sound = null;
game.disconnect();
game = null;
};

@Override
Expand All @@ -271,7 +270,6 @@ protected void onResume() {

@Override
public Object onRetainCustomNonConfigurationInstance () {
game.disconnect();
return game;
}

Expand Down
50 changes: 35 additions & 15 deletions src/org/blockinger/game/activities/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,20 @@

public class MainActivity extends ListActivity {

public static ScoreDataSource datasource;
private Cursor mc;
private static SimpleCursorAdapter adapter;
public static final int SCORE_REQUEST = 0x0;

/** This key is used to access the player name, which is returned from the gameactivity upon completion (gameover).
* The Package Prefix is mandatory for Intent data
*/
public static final String PLAYERNAME_KEY = "org.blockinger.game.activities.playername";

/** This key is used to access the player name, which is returned from the gameactivity upon completion (gameover).
* The Package Prefix is mandatory for Intent data
*/
public static final String SCORE_KEY = "org.blockinger.game.activities.score";

public ScoreDataSource datasource;
private SimpleCursorAdapter adapter;
private AlertDialog.Builder startLevelDialog;
private AlertDialog.Builder donateDialog;
private int startLevel;
Expand All @@ -85,6 +96,7 @@ protected void onCreate(Bundle savedInstanceState) {
sound = new Sound(this,Sound.NO_MUSIC);

/* Database Management */
Cursor mc;
datasource = new ScoreDataSource(this);
datasource.open();
mc = datasource.getCursor();
Expand Down Expand Up @@ -175,11 +187,25 @@ public boolean onOptionsItemSelected(MenuItem item) {
public void start() {
Intent intent = new Intent(this, GameActivity.class);
Bundle b = new Bundle();
b.putInt("mode", GameActivity.start_new_game); //Your id
b.putInt("mode", GameActivity.NEW_GAME); //Your id
b.putInt("level", startLevel); //Your id
b.putString("playername", ((TextView)findViewById(R.id.nicknameEditView)).getText().toString()); //Your id
intent.putExtras(b); //Put your id to your next Intent
startActivity(intent);
startActivityForResult(intent,SCORE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode != SCORE_REQUEST)
return;
if(resultCode != RESULT_OK)
return;

String playerName = data.getStringExtra(PLAYERNAME_KEY);
long score = data.getLongExtra(SCORE_KEY,0);

datasource.open();
datasource.createScore(score, playerName);
}


Expand Down Expand Up @@ -214,10 +240,10 @@ public void onStopTrackingTouch(SeekBar arg0) {
public void onClickResume(View view) {
Intent intent = new Intent(this, GameActivity.class);
Bundle b = new Bundle();
b.putInt("mode", GameActivity.resume_old_game); //Your id
b.putInt("mode", GameActivity.RESUME_GAME); //Your id
b.putString("playername", ((TextView)findViewById(R.id.nicknameEditView)).getText().toString()); //Your id
intent.putExtras(b); //Put your id to your next Intent
startActivity(intent);
startActivityForResult(intent,SCORE_REQUEST);
}

/* public void onClickQuit(View view) {
Expand All @@ -230,13 +256,15 @@ public void onClickResume(View view) {
protected void onStop() {
super.onStop();
sound.pause();
datasource.close();
};

@Override
protected void onDestroy() {
super.onDestroy();
sound.release();
sound = null;
datasource.close();
};

@Override
Expand All @@ -254,12 +282,4 @@ protected void onResume() {
}
};

public static ScoreDataSource getDS() {
return datasource;
}

public static SimpleCursorAdapter getAdapter() {
return adapter;
}

}
3 changes: 2 additions & 1 deletion src/org/blockinger/game/components/GameState.java
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,8 @@ public long getNextPlayerMoveTime() {
}

public static void destroy() {
instance.disconnect();
if(instance != null)
instance.disconnect();
instance = null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/org/blockinger/game/components/Sound.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public Sound(Activity c, int musicChoice) {
// clearSoundPlayer.setLooping(false);
// clearSoundPlayer.setVolume(0.01f * PreferenceManager.getDefaultSharedPreferences(c).getInt("pref_soundvolume", 60), 0.01f * PreferenceManager.getDefaultSharedPreferences(c).getInt("pref_soundvolume", 60));

soundID_gameOverPlayer = soundPool.load(c, R.raw.synthaccord, 1);
soundID_gameOverPlayer = soundPool.load(c, R.raw.gameover, 1);
// gameOverPlayer = MediaPlayer.create(c,R.raw.gameover);
// gameOverPlayer.setLooping(false);
// gameOverPlayer.setVolume(0.015f * PreferenceManager.getDefaultSharedPreferences(c).getInt("pref_soundvolume", 60), 0.01f * PreferenceManager.getDefaultSharedPreferences(c).getInt("pref_soundvolume", 60));
Expand Down
3 changes: 1 addition & 2 deletions src/org/blockinger/game/db/ScoreDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public Score createScore(long score, String playerName) {
ContentValues values = new ContentValues();
values.put(HighscoreOpenHelper.COLUMN_SCORE, score);
values.put(HighscoreOpenHelper.COLUMN_PLAYERNAME, playerName);
long insertId = database.insert(HighscoreOpenHelper.TABLE_HIGHSCORES, null,
values);
long insertId = database.insert(HighscoreOpenHelper.TABLE_HIGHSCORES, null, values);
Cursor cursor = database.query(HighscoreOpenHelper.TABLE_HIGHSCORES,
allColumns, HighscoreOpenHelper.COLUMN_ID + " = " + insertId, null,
null, null, HighscoreOpenHelper.COLUMN_SCORE + " DESC");
Expand Down

0 comments on commit 4a4742c

Please sign in to comment.