emotionhero/app/src/main/java/com/rubenvandeven/emotionhero/Game.java

75 lines
1.9 KiB
Java

package com.rubenvandeven.emotionhero;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* Created by ruben on 02/09/16.
*
* A play of a level by current user at a specific time
*/
public class Game {
public Long id;
public Scenario scenario;
public float score;
public float bonus;
public Date time;
public String remoteId;
public Map<Integer, Hit> hits = new HashMap<>();
public ArrayList<Achievement> achievements = new ArrayList<>();
/**
* How long the face has been 'lost' during playing the level
*/
public float lostFaceTime;
public Game(Long id, int lvl_id, float score, float bonus, Date time, String remoteId) {
this(id, new Scenario(lvl_id, null), score, bonus, time, remoteId);
}
public Game(Long id, Scenario scenario, float score, float bonus, Date time, String remoteId) {
this.id = id;
this.scenario= scenario;
this.score = score;
this.bonus = bonus;
this.time = time == null ? new Date() : time;
this.remoteId = remoteId;
}
public void addHit(Hit hit) {
hits.put(hit.target.index, hit);
score = calculateScore();
bonus = calculateBonus();
}
private float calculateScore() {
float s = 0;
for(Hit hit: hits.values()) {
s += hit.score;
}
return s;
}
private float calculateBonus() {
float s = 0;
for(Hit hit: hits.values()) {
s += hit.bonus;
}
return s;
}
/**
* Calculate which achievements fit the game
*/
public void checkAchievements(Player player) {
for(Achievement achievement: scenario.achievements) {
if(achievement.achievementListener.hasGotAchievement(player, this) && !achievements.contains(achievement)) {
achievements.add(achievement);
}
}
}
}