Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
torquecoder committed Oct 31, 2016
1 parent 77304e4 commit 45f3c6c
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 4 deletions.
73 changes: 70 additions & 3 deletions app/src/main/java/tech/torque/popper/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
Expand All @@ -16,13 +17,18 @@
import java.util.List;
import java.util.Random;

import tech.torque.popper.utils.HighScoreHelper;
import tech.torque.popper.utils.SimpleAlertDialog;
import tech.torque.popper.utils.SoundHelper;

public class MainActivity extends AppCompatActivity implements Balloon.BalloonListener {

public static final int MIN_ANIMATION_DELAY = 500;
public static final int MAX_ANIMATION_DELAY = 1500;
public static final int MIN_ANIMATION_DURATION = 1000;
public static final int MAX_ANIMATION_DURATION = 8000;
public static final int NUMBER_OF_PINS = 5;
private static final int BALLOONS_PER_LEVEL = 10;

private ViewGroup mContentView;
private int[] mBalloonColors = new int[3];
Expand All @@ -32,6 +38,10 @@ public class MainActivity extends AppCompatActivity implements Balloon.BalloonLi
TextView mScoreDisplay, mLevelDisplay;
private List<ImageView> mPinImages = new ArrayList<>();
private List<Balloon> mBalloons = new ArrayList<>();
private Button mGoButton;
private boolean mPlaying, mGameStopped = true;
private int mBalloonsPopped;
private SoundHelper mSoundHelper;


@Override
Expand Down Expand Up @@ -65,25 +75,64 @@ public void onGlobalLayout() {
mPinImages.add((ImageView) findViewById(R.id.pushpin4));
mPinImages.add((ImageView) findViewById(R.id.pushpin5));

mGoButton = (Button) findViewById(R.id.go_button);


mScoreDisplay = (TextView) findViewById(R.id.score_display);
mLevelDisplay = (TextView) findViewById(R.id.level_display);

updateDisplay();

mSoundHelper = new SoundHelper(this);
mSoundHelper.prepareMusicPlayer(this);
}

private void startGame() {
mScore = 0;
mLevel = 0;
mPinsUsed = 0;
for (ImageView pin: mPinImages
) {
pin.setImageResource(R.drawable.pin);

}
mGameStopped = false;
startLevel();
mSoundHelper.playMusic();
}

private void startLevel() {
mLevel++;
updateDisplay();
BalloonLauncher launcher = new BalloonLauncher();
launcher.execute(mLevel);
mPlaying = true;
mBalloonsPopped = 0;
mGoButton.setText("Stop game");
}

private void finishLevel() {
Toast.makeText(this, String.format("You finished level %d", mLevel), Toast.LENGTH_SHORT).show();
mPlaying = false;
mGoButton.setText(String.format("Start level %d", mLevel + 1));
}

public void goButtonClickHandler(View view) {
startLevel();
if(mPlaying) {
gameOver(false);
} else if(mGameStopped) {
startGame();
} else {
startLevel();
}
}

@Override
public void popBalloon(Balloon balloon, boolean userTouch) {

mBalloonsPopped++;
mSoundHelper.playSound();

mContentView.removeView(balloon);
mBalloons.remove(balloon);

Expand All @@ -103,18 +152,36 @@ public void popBalloon(Balloon balloon, boolean userTouch) {
Toast.makeText(this, "Missed that one!", Toast.LENGTH_SHORT).show();
}
}

if(mBalloonsPopped == BALLOONS_PER_LEVEL) {
finishLevel();
}

updateDisplay();
}

private void gameOver(boolean b) {
private void gameOver(boolean allPinsUsed) {
Toast.makeText(this, "Game Over!", Toast.LENGTH_SHORT).show();
mSoundHelper.pauseMusic();

for (Balloon balloon: mBalloons
) {
mContentView.removeView(balloon);
balloon.setPopped(true);

}
mBalloons.clear();
mPlaying = false;
mGameStopped = true;
mGoButton.setText("Start game");

if(allPinsUsed) {
if(HighScoreHelper.isTopScore(this, mScore)) {
HighScoreHelper.setTopScore(this, mScore);
SimpleAlertDialog dialog = SimpleAlertDialog.newInstance("New High Score!", String.format("Your new high score is %d", mScore));
dialog.show(getSupportFragmentManager(), null);
}
}
}

private void updateDisplay() {
Expand All @@ -138,7 +205,7 @@ protected Void doInBackground(Integer... params) {
int minDelay = maxDelay / 2;

int balloonsLaunched = 0;
while (balloonsLaunched < 3) {
while (mPlaying && balloonsLaunched < BALLOONS_PER_LEVEL) {

// Get a random horizontal position for the next balloon
Random random = new Random(new Date().getTime());
Expand Down
28 changes: 27 additions & 1 deletion app/src/main/java/tech/torque/popper/utils/HighScoreHelper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
package tech.torque.popper.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class HighScoreHelper {

}
private static final String PREFS_GLOBAL = "prefs_global";
private static final String PREF_TOP_SCORE = "pref_top_score";

private static SharedPreferences getPreferences(Context context) {
return context.getSharedPreferences(
PREFS_GLOBAL, Context.MODE_PRIVATE);
}

// Setters and getters for global preferences
public static boolean isTopScore(Context context, int newScore) {
int topScore = getPreferences(context).getInt(PREF_TOP_SCORE, 0);
return newScore > topScore;
}

public static int getTopScore(Context context) {
return getPreferences(context).getInt(PREF_TOP_SCORE, 0);
}

public static void setTopScore(Context context, int score) {
SharedPreferences.Editor editor = getPreferences(context).edit();
editor.putInt(PREF_TOP_SCORE, score);
editor.apply();
}

}
48 changes: 48 additions & 0 deletions app/src/main/java/tech/torque/popper/utils/SimpleAlertDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package tech.torque.popper.utils;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;

public class SimpleAlertDialog extends DialogFragment {

private static final String TITLE_KEY = "title_key";
private static final String MESSAGE_KEY = "message_key";

public SimpleAlertDialog() {
}

public static SimpleAlertDialog newInstance(String title, String message) {

Bundle args = new Bundle();
args.putString(TITLE_KEY, title);
args.putString(MESSAGE_KEY, message);

SimpleAlertDialog fragment = new SimpleAlertDialog();
fragment.setArguments(args);
return fragment;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

Bundle args = getArguments();
if (args == null) throw new AssertionError();

String title = args.getString(TITLE_KEY);
String prompt = args.getString(MESSAGE_KEY);

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle(title)
.setMessage(prompt)
.setCancelable(false);

builder.setPositiveButton(android.R.string.ok, null);
return builder.create();

}

}
75 changes: 75 additions & 0 deletions app/src/main/java/tech/torque/popper/utils/SoundHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package tech.torque.popper.utils;


import android.app.Activity;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.view.View;

import tech.torque.popper.R;

public class SoundHelper {

private MediaPlayer mMusicPlayer;

private SoundPool mSoundPool;
private int mSoundID;
private boolean mLoaded;
private float mVolume;

public SoundHelper(Activity activity) {

AudioManager audioManager = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
float actVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mVolume = actVolume / maxVolume;

activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes audioAttrib = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
mSoundPool = new SoundPool.Builder().setAudioAttributes(audioAttrib).setMaxStreams(6).build();
} else {
//noinspection deprecation
mSoundPool = new SoundPool(6, AudioManager.STREAM_MUSIC, 0);
}

mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
mLoaded = true;
}
});
mSoundID = mSoundPool.load(activity, R.raw.balloon_pop, 1);
}

public void playSound() {
if (mLoaded) {
mSoundPool.play(mSoundID, mVolume, mVolume, 1, 0, 1f);
}
}

public void prepareMusicPlayer(Context context) {
mMusicPlayer = MediaPlayer.create(context.getApplicationContext(), R.raw.pleasant_music);
mMusicPlayer.setVolume(.5f, .5f);
mMusicPlayer.setLooping(true);
}

public void playMusic() {
if(mMusicPlayer != null) {
mMusicPlayer.start();
}
}

public void pauseMusic() {
if(mMusicPlayer != null && mMusicPlayer.isPlaying()) {
mMusicPlayer.pause();
}
}
}
Binary file added app/src/main/res/raw/balloon_pop.wav
Binary file not shown.
Binary file added app/src/main/res/raw/pleasant_music.mp3
Binary file not shown.

0 comments on commit 45f3c6c

Please sign in to comment.