GamingActivity can now load with various levels by supplying the right intent

This commit is contained in:
Ruben 2016-08-20 21:28:05 +01:00
parent cb046a2e01
commit 8f65131b7a
7 changed files with 132 additions and 39 deletions

View File

@ -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<Integer, Integer> 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);
}
}

View File

@ -56,6 +56,9 @@ public class Highscores extends ArrayList<Highscore>{
}
});
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<Highscore>{
/**
* Get the n highest scores
* @todo only for current level!!
*/
public Highscores getLast(Integer n) {
Collections.sort(this, new Comparator<Highscore>() {
@ -80,6 +84,9 @@ public class Highscores extends ArrayList<Highscore>{
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));

View File

@ -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<Integer> SCENARIOS = new ArrayList<Integer>() {{
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<Target> 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;
}
}

View File

@ -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);
}
}

View File

@ -35,10 +35,20 @@
<FrameLayout
android:id="@+id/ending_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/highscores"
android:textColor="@color/highscore"
android:textSize="30sp"
android:visibility="gone"
/>
<Button
android:text="@string/restart"
android:layout_width="wrap_content"
@ -46,6 +56,13 @@
android:id="@+id/restartButton"
android:layout_gravity="center_vertical|center_horizontal"
android:visibility="gone" />
<Button
android:text="@string/nextLvl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/nextLvlButton"
android:layout_gravity="center_vertical|right"
android:visibility="gone" />
</FrameLayout>

View File

@ -6,6 +6,7 @@
<color name="colorScenarioLine">#FF0000</color>
<color name="black_overlay">#66000000</color>
<color name="highscore">#ffff00</color>
<!--TODO: implement in Emotion.java-->
<color name="emotion_anger">#ff0000</color>

View File

@ -5,4 +5,5 @@
<string name="dummy_content">initialising&#8230;</string>
<string name="camera_required">The camera permission is required. Please start again</string>
<string name="restart">Restart</string>
<string name="nextLvl">Next Level!</string>
</resources>