Step further with highscores

This commit is contained in:
Ruben 2016-08-20 17:22:01 +01:00
parent 383e9d9a9a
commit cb046a2e01
3 changed files with 125 additions and 9 deletions

View file

@ -2,6 +2,7 @@ package com.rubenvandeven.emotionhero;
import android.Manifest;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.PackageManager;
import android.media.AudioAttributes;
import android.media.AudioManager;
@ -25,6 +26,11 @@ import com.affectiva.android.affdex.sdk.detector.CameraDetector;
import com.affectiva.android.affdex.sdk.detector.Detector;
import com.affectiva.android.affdex.sdk.detector.Face;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -35,6 +41,7 @@ import java.util.List;
public class GamingActivity extends AppCompatActivity implements Detector.ImageListener, CameraDetector.CameraEventListener, Detector.FaceListener {
final static String LOG_TAG = "EmotionHero";
final static String HIGHSCORE_FILENAME = "highscores.json";
final int PERMISSIONS_REQUEST_CAMERA = 1;
@ -205,9 +212,7 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL
// if(!currentScenario.isWithinTime(timestamp))
if(currentScenario.isFinished())
{
setText("LEVEL ENDED");
stopDetector();
restartButton.setVisibility(View.VISIBLE);
finishLevel();
return;
}
if (list == null)
@ -318,4 +323,60 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL
protected void createOldSoundPool(){
sound = new SoundPool(5, AudioManager.STREAM_MUSIC,0);
}
public Highscores getHighscores() {
try{
FileInputStream fis = openFileInput(HIGHSCORE_FILENAME);
StringBuilder builder = new StringBuilder();
int ch;
while((ch = fis.read()) != -1){
builder.append((char)ch);
}
return Highscores.fromJson(builder.toString());
} catch (IOException e) {
return new Highscores();
}
}
public void addHighscore(Highscore score) {
Highscores scores = getHighscores();
scores.add(score);
saveHighscores(scores);
}
public void saveHighscores(Highscores scores) {
try {
FileOutputStream fos = openFileOutput(HIGHSCORE_FILENAME, Context.MODE_PRIVATE);
fos.write(scores.toJson().getBytes());
fos.close();
} catch(IOException e) {
// for now skip error
Log.e(LOG_TAG, "Could not save highscore!");
}
}
public void finishLevel() {
setText("LEVEL ENDED");
stopDetector();
restartButton.setVisibility(View.VISIBLE);
Highscores scores = getHighscores();
Highscore score = currentScenario.getHighscore();
if(score.score == currentScenario.getMaxScore()) {
// Maximum SCORE!!
// You nailed it!
} if(scores.isEmpty()) {
// First play! Nice :-)
}
else if(scores.isHighest(score)) {
// HIGHSCORE!!!!!
} else {
// Better luck next time
}
// show the highscores (top 3?)...
scores.add(score);
saveHighscores(scores);
}
}

View file

@ -26,23 +26,66 @@ public class Highscores extends ArrayList<Highscore>{
return gson.toJson(this);
}
/**
* Check if given highscore is highest score in this set.
* @param score
* @return
*/
public boolean isHighest(Highscore score) {
for(Highscore s: this) {
if(s == score) // to allow comparison of items that are already in the set
continue; // skip if it wants to compare with self.
if(s.score > score.score) {
return false;
}
}
return true;
}
/**
* Get the n highest scores
*/
public ArrayList<Highscore> getTopN(int n) {
public Highscores getTopN(int n) {
Collections.sort(this, new Comparator<Highscore>() {
@Override
public int compare(Highscore score1, Highscore score2)
{
return score1.score < score2.score ? -1 : score1.score == score2.score ? 0 : 1;
// return highest first
return score1.score > score2.score ? -1 : score1.score == score2.score ? 0 : 1;
}
});
Highscores scores = new Highscores();
for (int i = 0; i < n; i++) {
scores.add(this.get(i));
}
return scores;
}
/**
* Get the n highest scores
*/
public Highscores getLast(Integer n) {
Collections.sort(this, new Comparator<Highscore>() {
@Override
public int compare(Highscore score1, Highscore score2)
{
// return newest first
return score1.time.before(score2.time) ? -1 : score1.time == score2.time ? 0 : 1;
}
});
if(n == null) { // no limit
return this;
}
// public ArrayList<Highscore> getLastN(int n) {
//
// }
Highscores scores = new Highscores();
for (int i = 0; i < n; i++) {
scores.add(this.get(i));
}
return scores;
}
}

View file

@ -261,6 +261,18 @@ abstract public class Scenario {
return runningTime > duration;
}
public Highscore getHighscore() {
if(!isFinished()) {
return null;
}
return new Highscore("Levelname!", getTotalScore());
}
public int getMaxScore() {
return targets.size() * 100;
}
// TODO: create a 'tick' that checks all current values with requirements and increases the timer etc
// TODO: ... if scenario is running. This internal times makes it easier to pause etc.
}