Highscores now work after level end

This commit is contained in:
Ruben 2016-08-23 00:35:33 +01:00
parent d3cf4fa73b
commit 5793fa5d30
8 changed files with 267 additions and 58 deletions

View file

@ -345,10 +345,10 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL
public void finishLevel() { public void finishLevel() {
setText("LEVEL ENDED"); setText("LEVEL ENDED");
stopDetector(); stopDetector();
restartButton.setVisibility(View.VISIBLE); // restartButton.setVisibility(View.VISIBLE);
if(!currentScenario.isFinalLevel()) { // if(!currentScenario.isFinalLevel()) {
nextLvlButton.setVisibility(View.VISIBLE); // nextLvlButton.setVisibility(View.VISIBLE);
} // }
PlayerInfo playerInfo = player.getPlayerInfo(); PlayerInfo playerInfo = player.getPlayerInfo();
ScoreList scores = playerInfo.getScoresForLevel(currentScenario.id); ScoreList scores = playerInfo.getScoresForLevel(currentScenario.id);
@ -372,23 +372,28 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL
// ... used by 'continue' button in main menu // ... used by 'continue' button in main menu
Scenario nextScenario = new Scenario(currentScenario.getNextLevelId(), this); Scenario nextScenario = new Scenario(currentScenario.getNextLevelId(), this);
if(nextScenario.isHigherThen(playerInfo.reachedLevelId)) { if(nextScenario.isHigherThen(playerInfo.reachedLevelId)) {
playerInfo.reachedLevelId = currentScenario.id; playerInfo.reachedLevelId = nextScenario.id;
} }
scores.add(score); scores.add(score);
player.savePlayerInfo(playerInfo); player.savePlayerInfo(playerInfo);
String highscoreText = "TOPSCORES\n"; // String highscoreText = "TOPSCORES\n";
//
// ScoreList topscores = scores.getTopN(3); // TODO: only highscores for current lEVEL!!!!!!!
// int i = 0;
// for(Score 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);
ScoreList topscores = scores.getTopN(3); // TODO: only highscores for current lEVEL!!!!!!! finish();
int i = 0; Intent intent = new Intent(this, HighscoreActivity.class);
for(Score topscore: topscores) { intent.putExtra(HighscoreActivity.INTENT_EXTRA_SCORE_ID, score.id.toString());
i++; startActivity(intent);
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

@ -14,17 +14,26 @@ import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.WindowManager; import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import java.util.UUID;
public class HighscoreActivity extends AppCompatActivity { public class HighscoreActivity extends AppCompatActivity {
public final static String INTENT_EXTRA_SCORE_ID = "com.rubenvandeven.emotionhero.SCORE_ID";
/** /**
* The {@link android.support.v4.view.PagerAdapter} that will provide * The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a * fragments for each of the sections. We use a
@ -42,6 +51,13 @@ public class HighscoreActivity extends AppCompatActivity {
Player player; Player player;
/**
* If the intent is loaded from the game, it should show a specific highscorelist
* with the given score highligted
*/
Score score;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -50,6 +66,18 @@ public class HighscoreActivity extends AppCompatActivity {
player = Player.getInstance(getApplicationContext()); player = Player.getInstance(getApplicationContext());
String scoreIdString = getIntent().getStringExtra(INTENT_EXTRA_SCORE_ID);
if(scoreIdString != null) {
UUID score_id = UUID.fromString(scoreIdString);
score = player.getPlayerInfo().getScore(score_id);
if(score == null) {
Log.e("Highscore", "CANNOT FIND SCORE!! " + scoreIdString);
}
}
Log.i("Highscore", ""+player.getPlayerInfo().reachedLevelId);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@ -62,25 +90,32 @@ public class HighscoreActivity extends AppCompatActivity {
mViewPager = (ViewPager) findViewById(R.id.container); mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter); mViewPager.setAdapter(mSectionsPagerAdapter);
// if score is set, set current page.
if(score != null) {
int levelpage = Scenario.SCENARIOS.indexOf(score.lvl_id);
mViewPager.setCurrentItem(levelpage);
}
// start the current level when pressing the big red button // start the current level when pressing the big red button
// ... but only if having access. // ... but only if having access.
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); // FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() { // fab.setOnClickListener(new View.OnClickListener() {
@Override // @Override
public void onClick(View view) { // public void onClick(View view) {
int lvl_id = mSectionsPagerAdapter.getLevelIdForPosition(mViewPager.getCurrentItem()); // int lvl_id = mSectionsPagerAdapter.getLevelIdForPosition(mViewPager.getCurrentItem());
if(!player.getPlayerInfo().hasAccessToLevel(lvl_id)) { // if(!player.getPlayerInfo().hasAccessToLevel(lvl_id)) {
Snackbar.make(view, "No access to this level yet... complete the other levels first" , Snackbar.LENGTH_LONG) // Snackbar.make(view, "No access to this level yet... complete the other levels first" , Snackbar.LENGTH_LONG)
.setAction("Action", null).show(); // .setAction("Action", null).show();
} else { // } else {
Intent intent = new Intent(HighscoreActivity.this, GamingActivity.class); // Intent intent = new Intent(HighscoreActivity.this, GamingActivity.class);
intent.putExtra(GamingActivity.INTENT_EXTRA_SCENARIO, lvl_id); // intent.putExtra(GamingActivity.INTENT_EXTRA_SCENARIO, lvl_id);
finish(); // finish();
startActivity(intent); // startActivity(intent);
} // }
} // }
}); // });
} }
@ -93,6 +128,7 @@ public class HighscoreActivity extends AppCompatActivity {
* fragment. * fragment.
*/ */
private static final String ARG_LVL_ID = "LVL_ID"; private static final String ARG_LVL_ID = "LVL_ID";
private static final String ARG_SCORE_ID = "SCORE_ID";
public HighscoreFragment() { public HighscoreFragment() {
} }
@ -101,10 +137,11 @@ public class HighscoreActivity extends AppCompatActivity {
* Returns a new instance of this fragment for the given section * Returns a new instance of this fragment for the given section
* number. * number.
*/ */
public static HighscoreFragment newInstance(int lvl_id) { public static HighscoreFragment newInstance(int lvl_id, String scoreIdString) {
HighscoreFragment fragment = new HighscoreFragment(); HighscoreFragment fragment = new HighscoreFragment();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putInt(ARG_LVL_ID, lvl_id); args.putInt(ARG_LVL_ID, lvl_id);
args.putString(ARG_SCORE_ID, scoreIdString);
fragment.setArguments(args); fragment.setArguments(args);
return fragment; return fragment;
} }
@ -112,14 +149,33 @@ public class HighscoreActivity extends AppCompatActivity {
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_highscore, container, false); View rootView = inflater.inflate(R.layout.fragment_highscore, container, false);
// TextView textView = (TextView) rootView.findViewById(R.id.section_label);
Button startLvlButton = (Button) rootView.findViewById(R.id.startLvlButton);
Button nextLvlButton = (Button) rootView.findViewById(R.id.nextLvlButton);
int lvl_id = getArguments().getInt(ARG_LVL_ID);
Score score = null;
String scoreIdString = getArguments().getString(ARG_SCORE_ID);
final Scenario scenario = new Scenario(lvl_id, null);
Player player = Player.getInstance(getContext()); Player player = Player.getInstance(getContext());
ScoreList scores = player.getPlayerInfo().getScoresForLevel(getArguments().getInt(ARG_LVL_ID)); ScoreList scores = player.getPlayerInfo().getScoresForLevel(lvl_id);
// String highscoreText = ""; if(scoreIdString != null) {
UUID scoreId = UUID.fromString(scoreIdString);
score = player.getPlayerInfo().getScore(scoreId);
}
ScoreList topscores = scores.getTopN(8); // TODO: only highscores for current lEVEL!!!!!!!
TextView levelName = (TextView) rootView.findViewById(R.id.levelName);
levelName.setText(scenario.toString());
ScoreList topscores = scores.getTopN(5);
LinearLayout topscoreList = (LinearLayout) rootView.findViewById(R.id.topscoreList); LinearLayout topscoreList = (LinearLayout) rootView.findViewById(R.id.topscoreList);
@ -133,21 +189,81 @@ public class HighscoreActivity extends AppCompatActivity {
topscoreList.addView(noScoreItemText); topscoreList.addView(noScoreItemText);
} }
boolean foundScore = false;
int i = 0; int i = 0;
for(Score topscore: topscores) { for(Score topscore: topscores) {
i++; i++;
String highscoreText = String.format("%1$d. %2$.2f\n", i, topscore.score); //make line by line elements String highscoreText = String.format("%1$d. %2$.2f", i, topscore.score); //make line by line elements
TextView scoreItem = new TextView(getContext());
scoreItem.setText(highscoreText);
scoreItem.setTextColor(ContextCompat.getColor(getContext(), R.color.highscore));
float multiplier = 1 + (5-i)/5f;
scoreItem.setTextSize(getResources().getDimension(R.dimen.highscore_textsize) * multiplier);
scoreItem.setTypeface(Typeface.DEFAULT_BOLD);
topscoreList.addView(scoreItem);
if(score != null && topscore.id == score.id) {
foundScore = true;
setBlinking(scoreItem);
}
}
// if score is set.. but not a highscore...
if(score != null && foundScore == false) {
String highscoreText = String.format("\n... %1$.2f", score.score); //make line by line elements
TextView scoreItem = new TextView(getContext()); TextView scoreItem = new TextView(getContext());
scoreItem.setText(highscoreText); scoreItem.setText(highscoreText);
scoreItem.setTextColor(ContextCompat.getColor(getContext(), R.color.highscore)); scoreItem.setTextColor(ContextCompat.getColor(getContext(), R.color.highscore));
scoreItem.setTextSize(getResources().getDimension(R.dimen.highscore_textsize)); scoreItem.setTextSize(getResources().getDimension(R.dimen.highscore_textsize));
scoreItem.setTypeface(Typeface.DEFAULT_BOLD); scoreItem.setTypeface(Typeface.DEFAULT_BOLD);
topscoreList.addView(scoreItem); topscoreList.addView(scoreItem);
setBlinking(scoreItem);
} }
if(player.getPlayerInfo().hasAccessToLevel(lvl_id)) {
if(score != null) { // if we come from the level itself.. show retry!
startLvlButton.setText("Retry");
}
startLvlButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), GamingActivity.class);
intent.putExtra(GamingActivity.INTENT_EXTRA_SCENARIO, scenario.id);
getActivity().finish();
startActivity(intent);
}
});
} else {
startLvlButton.setVisibility(View.INVISIBLE);
}
if(scenario.isFinalLevel()) {
nextLvlButton.setVisibility(View.INVISIBLE);
} else {
nextLvlButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int next_lvl_id = scenario.getNextLevelId();
int next_page_id = Scenario.SCENARIOS.indexOf(next_lvl_id);
((HighscoreActivity)getActivity()).getViewPager().setCurrentItem(next_page_id, true);
}
});
}
// textView.setText(highscoreText); // textView.setText(highscoreText);
// textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_LVL_ID))); // textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_LVL_ID)));
return rootView; return rootView;
} }
public static void setBlinking(View viewToBlink) {
Animation anim = new AlphaAnimation(1.0f, 0.0f);
anim.setDuration(500); //You can manage the blinking time with this parameter
// anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
viewToBlink.startAnimation(anim);
}
} }
/** /**
@ -165,7 +281,12 @@ public class HighscoreActivity extends AppCompatActivity {
// getItem is called to instantiate the fragment for the given page. // getItem is called to instantiate the fragment for the given page.
// Return a HighscoreFragment (defined as a static inner class below). // Return a HighscoreFragment (defined as a static inner class below).
// position is the used to get lvl_id. // position is the used to get lvl_id.
return HighscoreFragment.newInstance(getLevelIdForPosition(position)); int lvl_id = getLevelIdForPosition(position);
String scoreString = null;
if(score != null && lvl_id == score.lvl_id) {
scoreString = score.id.toString();
}
return HighscoreFragment.newInstance(lvl_id, scoreString);
} }
public int getLevelIdForPosition(int position) { public int getLevelIdForPosition(int position) {
@ -191,4 +312,16 @@ public class HighscoreActivity extends AppCompatActivity {
return null; return null;
} }
} }
/**
* A little hack: http://stackoverflow.com/a/14777003
*/
public ViewPager getViewPager() {
if (null == mViewPager) {
mViewPager = (ViewPager) findViewById(R.id.container);
}
return mViewPager;
}
} }

View file

@ -10,6 +10,9 @@ import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import static com.google.gson.internal.bind.TypeAdapters.UUID;
/** /**
* Created by ruben on 20/08/16. * Created by ruben on 20/08/16.
@ -76,4 +79,15 @@ public class PlayerInfo {
return Scenario.isHigherLevel(reachedLevelId, lvl_id); return Scenario.isHigherLevel(reachedLevelId, lvl_id);
} }
public Score getScore(UUID score_id) {
for(ScoreList scores: levelScores.values()) {
for(Score score: scores) {
if(score.id != null && score.id.equals(score_id)) {
return score;
}
}
}
return null;
}
} }

View file

@ -356,4 +356,19 @@ public class Scenario {
return SCENARIOS.indexOf(lvl_id) > SCENARIOS.indexOf(then_lvl_id); return SCENARIOS.indexOf(lvl_id) > SCENARIOS.indexOf(then_lvl_id);
} }
public String toString() {
switch(id) {
case LVL_ANGER:
return "I am sooo ANGRY";
case LVL_JOY:
return "Let's be joyfull!";
case LVL_SURPRISE:
return "What a surprise";
case LVL_SADDNESS:
return "Please, don't cry...";
}
return "...";
}
} }

View file

@ -2,13 +2,14 @@ package com.rubenvandeven.emotionhero;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.UUID;
/** /**
* Created by ruben on 19/08/16. * Created by ruben on 19/08/16.
*/ */
public class Score { public class Score {
public int id; public UUID id = UUID.randomUUID();
public int lvl_id; public int lvl_id;
public float score; public float score;
public Date time; public Date time;

View file

@ -10,13 +10,6 @@ import java.util.Comparator;
public class ScoreList extends ArrayList<Score>{ public class ScoreList extends ArrayList<Score>{
public boolean add(Score score) {
boolean ret = super.add(score);
int id = super.indexOf(score);
score.id = id;
return ret;
}
/** /**
* Check if given highscore is highest hit in this set. * Check if given highscore is highest hit in this set.
* @param score * @param score

View file

@ -8,12 +8,18 @@
android:fitsSystemWindows="true" android:fitsSystemWindows="true"
tools:context="com.rubenvandeven.emotionhero.HighscoreActivity"> tools:context="com.rubenvandeven.emotionhero.HighscoreActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout <android.support.design.widget.AppBarLayout
android:id="@+id/appbar" android:id="@+id/appbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top" android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay"> android:theme="@style/AppTheme.AppBarOverlay"
>
<android.support.v7.widget.Toolbar <android.support.v7.widget.Toolbar
android:id="@+id/toolbar" android:id="@+id/toolbar"
@ -28,19 +34,23 @@
</android.support.design.widget.AppBarLayout> </android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager <android.support.v4.view.ViewPager
android:id="@+id/container" android:id="@+id/container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:background="@color/colorPrimary" /> android:background="@color/colorPrimary"
android:layout_below="@+id/appbar" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab" </RelativeLayout>
android:layout_width="wrap_content" <!--<android.support.design.widget.FloatingActionButton-->
android:layout_height="wrap_content" <!--android:id="@+id/fab"-->
android:layout_gravity="end|bottom" <!--android:layout_width="wrap_content"-->
android:layout_margin="@dimen/fab_margin" <!--android:layout_height="wrap_content"-->
app:srcCompat="@android:drawable/ic_media_play" <!--android:layout_gravity="end|bottom"-->
app:backgroundTint="@color/colorScenarioLine" /> <!--android:layout_margin="@dimen/fab_margin"-->
<!--app:srcCompat="@android:drawable/ic_media_play"-->
<!--app:backgroundTint="@color/colorScenarioLine" />-->
</android.support.design.widget.CoordinatorLayout> </android.support.design.widget.CoordinatorLayout>

View file

@ -18,8 +18,46 @@
android:orientation="vertical" android:orientation="vertical"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"></LinearLayout> android:layout_above="@+id/highscoreActions">
<TextView
android:text="..."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/levelName"
android:textSize="15sp"
android:textColor="@color/highscore"
android:textStyle="normal|bold" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/highscoreActions"
android:layout_alignParentBottom="true"
android:gravity="bottom">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/startLvlButton"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next level"
android:id="@+id/nextLvlButton"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout> </RelativeLayout>