mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-20 12:48:00 +00:00
Add support for negative thresholds
This commit is contained in:
@@ -11,8 +11,8 @@ import forge.util.gui.SOptionPane;
|
||||
public abstract class Achievement {
|
||||
private final String displayName, bronzeDesc, silverDesc, goldDesc;
|
||||
private final int bronzeThreshold, silverThreshold, goldThreshold;
|
||||
private final boolean showBest, showCurrent;
|
||||
private int best, current;
|
||||
private final boolean showBest, showCurrent, checkGreaterThan;
|
||||
protected int best, current;
|
||||
|
||||
public Achievement(String displayName0,
|
||||
boolean showBest0, boolean showCurrent0,
|
||||
@@ -28,6 +28,7 @@ public abstract class Achievement {
|
||||
silverThreshold = silverThreshold0;
|
||||
goldDesc = goldDesc0;
|
||||
goldThreshold = goldThreshold0;
|
||||
checkGreaterThan = goldThreshold0 > silverThreshold0;
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
@@ -46,13 +47,22 @@ public abstract class Achievement {
|
||||
return best;
|
||||
}
|
||||
public boolean earnedGold() {
|
||||
return best >= goldThreshold;
|
||||
if (checkGreaterThan) {
|
||||
return best >= goldThreshold;
|
||||
}
|
||||
return best <= goldThreshold;
|
||||
}
|
||||
public boolean earnedSilver() {
|
||||
return best >= silverThreshold;
|
||||
if (checkGreaterThan) {
|
||||
return best >= silverThreshold;
|
||||
}
|
||||
return best <= silverThreshold;
|
||||
}
|
||||
public boolean earnedBronze() {
|
||||
return best >= bronzeThreshold;
|
||||
if (checkGreaterThan) {
|
||||
return best >= bronzeThreshold;
|
||||
}
|
||||
return best <= bronzeThreshold;
|
||||
}
|
||||
|
||||
protected abstract int evaluate(Player player, Game game, int current);
|
||||
@@ -70,43 +80,49 @@ public abstract class Achievement {
|
||||
|
||||
public void update(IGuiBase gui, Player player) {
|
||||
current = evaluate(player, player.getGame(), current);
|
||||
if (current > best) {
|
||||
int oldBest = best;
|
||||
best = current;
|
||||
if (checkGreaterThan) {
|
||||
if (current <= best) { return; }
|
||||
}
|
||||
else if (current >= best) { return; }
|
||||
|
||||
String type = null;
|
||||
FSkinProp image = null;
|
||||
String desc = null;
|
||||
if (earnedGold()) {
|
||||
if (oldBest < goldThreshold) {
|
||||
type = "Gold";
|
||||
image = FSkinProp.IMG_GOLD_TROPHY;
|
||||
desc = goldDesc;
|
||||
}
|
||||
boolean hadEarnedGold = earnedGold();
|
||||
boolean hadEarnedSilver = earnedSilver();
|
||||
boolean hadEarnedBronze = earnedBronze();
|
||||
|
||||
best = current;
|
||||
|
||||
String type = null;
|
||||
FSkinProp image = null;
|
||||
String desc = null;
|
||||
if (earnedGold()) {
|
||||
if (!hadEarnedGold) {
|
||||
type = "Gold";
|
||||
image = FSkinProp.IMG_GOLD_TROPHY;
|
||||
desc = goldDesc;
|
||||
}
|
||||
else if (earnedSilver()) {
|
||||
if (oldBest < silverThreshold) {
|
||||
type = "Silver";
|
||||
image = FSkinProp.IMG_SILVER_TROPHY;
|
||||
desc = silverDesc;
|
||||
}
|
||||
}
|
||||
else if (earnedSilver()) {
|
||||
if (!hadEarnedSilver) {
|
||||
type = "Silver";
|
||||
image = FSkinProp.IMG_SILVER_TROPHY;
|
||||
desc = silverDesc;
|
||||
}
|
||||
else if (earnedBronze()) {
|
||||
if (oldBest < bronzeThreshold) {
|
||||
type = "Bronze";
|
||||
image = FSkinProp.IMG_BRONZE_TROPHY;
|
||||
desc = bronzeDesc;
|
||||
}
|
||||
}
|
||||
if (type != null) {
|
||||
SOptionPane.showMessageDialog(gui, "You've earned a " + type + " trophy!\n\n" +
|
||||
displayName + " - " + desc, "Achievement Earned", image);
|
||||
}
|
||||
else if (earnedBronze()) {
|
||||
if (!hadEarnedBronze) {
|
||||
type = "Bronze";
|
||||
image = FSkinProp.IMG_BRONZE_TROPHY;
|
||||
desc = bronzeDesc;
|
||||
}
|
||||
}
|
||||
if (type != null) {
|
||||
SOptionPane.showMessageDialog(gui, "You've earned a " + type + " trophy!\n\n" +
|
||||
displayName + " - " + desc, "Achievement Earned", image);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean needSave() {
|
||||
return best > 0 || current > 0;
|
||||
return best != 0 || current != 0;
|
||||
}
|
||||
|
||||
public void saveToXml(Element el) {
|
||||
|
||||
@@ -52,7 +52,7 @@ public abstract class AchievementCollection implements Iterable<Achievement> {
|
||||
add("MatchWinStreak", new MatchWinStreak(10, 25, 50));
|
||||
add("TotalGameWins", new TotalGameWins(250, 500, 1000));
|
||||
add("TotalMatchWins", new TotalMatchWins(100, 250, 500));
|
||||
add("Overkill", new Overkill(25, 50, 100));
|
||||
add("Overkill", new Overkill(-25, -50, -100));
|
||||
add("LifeToSpare", new LifeToSpare(20, 40, 80));
|
||||
add("Hellbent", new Hellbent());
|
||||
}
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
package forge.achievement;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import forge.game.Game;
|
||||
import forge.game.player.Player;
|
||||
|
||||
public class Overkill extends Achievement {
|
||||
public Overkill(int bronze0, int silver0, int gold0) {
|
||||
super("Overkill", true, false,
|
||||
String.format("Win game with opponent at -%d life or less.", bronze0), bronze0,
|
||||
String.format("Win game with opponent at -%d life or less.", silver0), silver0,
|
||||
String.format("Win game with opponent at -%d life or less.", gold0), gold0);
|
||||
String.format("Win game with opponent at %d life or less.", bronze0), bronze0,
|
||||
String.format("Win game with opponent at %d life or less.", silver0), silver0,
|
||||
String.format("Win game with opponent at %d life or less.", gold0), gold0);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -16,17 +18,22 @@ public class Overkill extends Achievement {
|
||||
if (player.getOutcome().hasWon()) {
|
||||
Player opponent = getSingleOpponent(player);
|
||||
if (opponent != null && opponent.getLife() < 0) {
|
||||
return -opponent.getLife();
|
||||
return opponent.getLife();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSubTitle() {
|
||||
if (getBest() > 0) {
|
||||
return "Best: -" + getBest() + " life";
|
||||
public void loadFromXml(Element el) {
|
||||
super.loadFromXml(el);
|
||||
|
||||
//perform conversion to handle data from old format before supporting negative thresholds
|
||||
if (best > 0) {
|
||||
best = -best;
|
||||
}
|
||||
if (current > 0) {
|
||||
current = -current;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user