diff --git a/app/src/main/java/com/rubenvandeven/emotionhero/GamingActivity.java b/app/src/main/java/com/rubenvandeven/emotionhero/GamingActivity.java index b04fd44..6973fa9 100644 --- a/app/src/main/java/com/rubenvandeven/emotionhero/GamingActivity.java +++ b/app/src/main/java/com/rubenvandeven/emotionhero/GamingActivity.java @@ -3,6 +3,7 @@ package com.rubenvandeven.emotionhero; import android.Manifest; import android.annotation.TargetApi; import android.content.Context; +import android.content.Intent; import android.content.pm.PackageManager; import android.media.AudioAttributes; import android.media.AudioManager; @@ -27,10 +28,8 @@ 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; @@ -40,6 +39,8 @@ import java.util.List; */ public class GamingActivity extends AppCompatActivity implements Detector.ImageListener, CameraDetector.CameraEventListener, Detector.FaceListener { + public final static String INTENT_EXTRA_SCENARIO = "com.rubenvandeven.emotionhero.LEVEL"; + final static String LOG_TAG = "EmotionHero"; final static String HIGHSCORE_FILENAME = "highscores.json"; @@ -60,6 +61,7 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL boolean has_camera_permission = false; Button restartButton; + Button nextLvlButton; public SoundPool sound; public HashMap soundIds = new HashMap<>(); @@ -75,6 +77,10 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL setContentView(R.layout.activity_gaming); + Intent intent = getIntent(); + // first level is the default to load, if none is given to intent + int scenarioLvlId = intent.getIntExtra(INTENT_EXTRA_SCENARIO, Scenario.SCENARIOS.get(0)); + mContentView = (TextView) findViewById(R.id.fullscreen_content); RelativeLayout videoLayout = (RelativeLayout) findViewById(R.id.video_layout); @@ -86,6 +92,16 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL startActivity(getIntent()); } }); + nextLvlButton = (Button) findViewById(R.id.nextLvlButton); + nextLvlButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + finish(); + Intent intent = getIntent(); // trouble create new intent because of 'this' + intent.putExtra(INTENT_EXTRA_SCENARIO, currentScenario.getNextLevelId()); + startActivity(intent); + } + }); // Set up the user interaction to manually show or hide the system UI. mContentView.setOnClickListener(new View.OnClickListener() { @@ -130,7 +146,8 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL videoLayout.addView(cameraPreview,0); - currentScenario = new ScenarioAnger(this); +// currentScenario = new ScenarioAnger(this); + currentScenario = new Scenario(scenarioLvlId, this); scenarioView = new ScenarioView(this, currentScenario); RelativeLayout.LayoutParams scenarioViewParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); @@ -359,6 +376,10 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL setText("LEVEL ENDED"); stopDetector(); restartButton.setVisibility(View.VISIBLE); + if(!currentScenario.isFinalLevel()) { + nextLvlButton.setVisibility(View.VISIBLE); + } + Highscores scores = getHighscores(); Highscore score = currentScenario.getHighscore(); @@ -378,5 +399,18 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL scores.add(score); saveHighscores(scores); + + String highscoreText = "TOPSCORES\n"; + + Highscores topscores = scores.getTopN(3); // TODO: only highscores for current lEVEL!!!!!!! + int i = 0; + for(Highscore topscore: topscores) { + i++; + highscoreText += String.format("%1$d. %2$.2f\n", i, topscore.score); //make line by line elements + } + + TextView highscoreView = (TextView) findViewById(R.id.highscores); + highscoreView.setText(highscoreText); + highscoreView.setVisibility(View.VISIBLE); } } diff --git a/app/src/main/java/com/rubenvandeven/emotionhero/Highscores.java b/app/src/main/java/com/rubenvandeven/emotionhero/Highscores.java index 5e2565c..fdad375 100644 --- a/app/src/main/java/com/rubenvandeven/emotionhero/Highscores.java +++ b/app/src/main/java/com/rubenvandeven/emotionhero/Highscores.java @@ -56,6 +56,9 @@ public class Highscores extends ArrayList{ } }); + if(n > this.size()) + n = this.size(); + Highscores scores = new Highscores(); for (int i = 0; i < n; i++) { scores.add(this.get(i)); @@ -65,6 +68,7 @@ public class Highscores extends ArrayList{ /** * Get the n highest scores + * @todo only for current level!! */ public Highscores getLast(Integer n) { Collections.sort(this, new Comparator() { @@ -80,6 +84,9 @@ public class Highscores extends ArrayList{ return this; } + if(n > this.size()) + n = this.size(); + Highscores scores = new Highscores(); for (int i = 0; i < n; i++) { scores.add(this.get(i)); diff --git a/app/src/main/java/com/rubenvandeven/emotionhero/Scenario.java b/app/src/main/java/com/rubenvandeven/emotionhero/Scenario.java index 8e4a461..93f4984 100644 --- a/app/src/main/java/com/rubenvandeven/emotionhero/Scenario.java +++ b/app/src/main/java/com/rubenvandeven/emotionhero/Scenario.java @@ -17,10 +17,24 @@ import java.util.TimerTask; * Created by ruben on 16/08/16. */ -abstract public class Scenario { +public class Scenario { + + public int id; public Bitmap currentFrameBitmap; + public static final int LVL_NONE = 0; + public static final int LVL_ANGER = 1; + public static final int LVL_JOY = 2; + public static final int LVL_SURPRISE = 3; + + // the levels in the right order. + public static final ArrayList SCENARIOS = new ArrayList() {{ + add(LVL_ANGER); + add(LVL_JOY); + add(LVL_SURPRISE); + }}; + static int DESIRED_FPS = 25; float duration = 0; @@ -53,8 +67,6 @@ abstract public class Scenario { ArrayList targets = new ArrayList<>(); - abstract void createScenario(); - class Target { public Emotion emotion; public float value; @@ -77,9 +89,21 @@ abstract public class Scenario { /** * Constructor */ - public Scenario(GamingActivity activity) + public Scenario(int lvl_id, GamingActivity activity) { - createScenario(); + // go to first scenario if unexisting is given + if(!SCENARIOS.contains(lvl_id)) { + lvl_id = SCENARIOS.get(0); + } + + this.id = lvl_id; + _activity = activity; + createTargets(); + init(); + } + + public void init() { + timer = new Timer("ScenarioTimer"); TimerTask tickTask; tickTask = new TimerTask() { @@ -92,7 +116,7 @@ abstract public class Scenario { } }; timer.schedule(tickTask, 0, 1000/DESIRED_FPS); - _activity = activity; + } /** @@ -273,6 +297,43 @@ abstract public class Scenario { 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. + public void createTargets() { + switch(id) { + case LVL_ANGER: + setTarget(Emotion.ANGER, 10, 1); + setTarget(Emotion.ANGER, 20, 2); + setTarget(Emotion.ANGER, 40, 3); + setTarget(Emotion.ANGER, 70, 4); + setTarget(Emotion.ANGER, 100, 5); + setTarget(Emotion.JOY, 100, 8); + setTarget(Emotion.ANGER, 100, 10); + setTarget(Emotion.JOY, 100, 15); + setTarget(Emotion.ANGER, 100, 20); + break; + case LVL_JOY: + break; + case LVL_SURPRISE: + setTarget(Emotion.SURPRISE, 20, 1); + setTarget(Emotion.SURPRISE, 50, 2); + setTarget(Emotion.SURPRISE, 80, 3); + setTarget(Emotion.SURPRISE, 100, 4); + break; + } + + } + + public int getNextLevelId() { + int nextIdx = SCENARIOS.indexOf(id) + 1; + if(SCENARIOS.size() <= nextIdx) { + return SCENARIOS.get(0); + } + return SCENARIOS.get(nextIdx); + } + + public boolean isFinalLevel() { + if(SCENARIOS.get(SCENARIOS.size()-1) == id) + return true; + return false; + } + } diff --git a/app/src/main/java/com/rubenvandeven/emotionhero/ScenarioAnger.java b/app/src/main/java/com/rubenvandeven/emotionhero/ScenarioAnger.java deleted file mode 100644 index c879b12..0000000 --- a/app/src/main/java/com/rubenvandeven/emotionhero/ScenarioAnger.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.rubenvandeven.emotionhero; - -import android.util.Log; - -/** - * Created by ruben on 17/08/16. - */ - -public class ScenarioAnger extends Scenario { - - public ScenarioAnger(GamingActivity activity) { - super(activity); - } - - void createScenario() - { - Log.d(GamingActivity.LOG_TAG, "CREATE SCENARIO: anger"); - setTarget(Emotion.ANGER, 10, 1); - setTarget(Emotion.ANGER, 20, 2); - setTarget(Emotion.ANGER, 40, 3); - setTarget(Emotion.ANGER, 70, 4); - setTarget(Emotion.ANGER, 100, 5); - setTarget(Emotion.JOY, 100, 8); - setTarget(Emotion.ANGER, 100, 10); - setTarget(Emotion.JOY, 100, 15); - setTarget(Emotion.ANGER, 100, 20); - } -} diff --git a/app/src/main/res/layout/activity_gaming.xml b/app/src/main/res/layout/activity_gaming.xml index a96c9b6..d181eab 100644 --- a/app/src/main/res/layout/activity_gaming.xml +++ b/app/src/main/res/layout/activity_gaming.xml @@ -35,10 +35,20 @@ + +