From 6162aadf429e9aaa5954be5c5872101a01ee77f7 Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Mon, 10 Jul 2017 22:20:31 -0400 Subject: [PATCH] v0.6.1: refactoring, and added save/load for catalogs --- .../shatteredpixeldungeon/Badges.java | 33 +--- .../journal/Catalogs.java | 166 ++++++++++++------ .../scenes/GameScene.java | 3 + .../scenes/StartScene.java | 2 + 4 files changed, 129 insertions(+), 75 deletions(-) diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/Badges.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/Badges.java index 09e7ab665..94d20242b 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/Badges.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/Badges.java @@ -48,6 +48,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; public class Badges { @@ -464,32 +465,14 @@ public class Badges { } public static void validateItemsIdentified() { - if (!global.contains( Badge.ALL_WEAPONS_IDENTIFIED ) && Catalogs.allWeaponsSeen()){ - displayBadge( Badge.ALL_WEAPONS_IDENTIFIED ); - } - if (!global.contains( Badge.ALL_ARMOR_IDENTIFIED ) && Catalogs.allArmorSeen()){ - displayBadge( Badge.ALL_ARMOR_IDENTIFIED ); - } - - if (!global.contains( Badge.ALL_WANDS_IDENTIFIED ) && Catalogs.allWandsSeen()){ - displayBadge( Badge.ALL_WANDS_IDENTIFIED ); - } - - if (!global.contains( Badge.ALL_RINGS_IDENTIFIED ) && Catalogs.allRingsSeen()){ - displayBadge( Badge.ALL_RINGS_IDENTIFIED ); - } - - if (!global.contains( Badge.ALL_ARTIFACTS_IDENTIFIED ) && Catalogs.allArtifactsSeen()){ - displayBadge( Badge.ALL_ARTIFACTS_IDENTIFIED ); - } - - if (!global.contains( Badge.ALL_POTIONS_IDENTIFIED ) && Catalogs.allPotionsSeen()){ - displayBadge( Badge.ALL_POTIONS_IDENTIFIED ); - } - - if (!global.contains( Badge.ALL_SCROLLS_IDENTIFIED ) && Catalogs.allScrollsSeen()){ - displayBadge( Badge.ALL_SCROLLS_IDENTIFIED ); + for (LinkedHashMap, Boolean> catalog : Catalogs.allCatalogs){ + if (Catalogs.allSeen(catalog.keySet())){ + Badge b = Catalogs.catalogBadges.get(catalog); + if (!global.contains(b)){ + displayBadge(b); + } + } } if (!global.contains( Badge.ALL_ITEMS_IDENTIFIED ) && diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/journal/Catalogs.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/journal/Catalogs.java index 98f68c356..c14c5cb76 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/journal/Catalogs.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/journal/Catalogs.java @@ -22,6 +22,7 @@ package com.shatteredpixel.shatteredpixeldungeon.journal; import com.shatteredpixel.shatteredpixeldungeon.Badges; +import com.shatteredpixel.shatteredpixeldungeon.ShatteredPixelDungeon; import com.shatteredpixel.shatteredpixeldungeon.items.Item; import com.shatteredpixel.shatteredpixeldungeon.items.armor.ClothArmor; import com.shatteredpixel.shatteredpixeldungeon.items.armor.HuntressArmor; @@ -116,10 +117,17 @@ import com.shatteredpixel.shatteredpixeldungeon.items.weapon.melee.WarHammer; import com.shatteredpixel.shatteredpixeldungeon.items.weapon.melee.Whip; import com.shatteredpixel.shatteredpixeldungeon.items.weapon.melee.WornShortsword; import com.shatteredpixel.shatteredpixeldungeon.items.weapon.missiles.Boomerang; +import com.watabou.noosa.Game; +import com.watabou.utils.Bundle; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.LinkedHashMap; +import java.util.List; public class Catalogs { @@ -157,13 +165,6 @@ public class Catalogs { return weapons.keySet(); } - public static boolean allWeaponsSeen(){ - for (Boolean val : weapons.values()){ - if (!val) return false; - } - return true; - } - private static final LinkedHashMap, Boolean> armor = new LinkedHashMap<>(); static { armor.put( ClothArmor.class, false); @@ -181,13 +182,6 @@ public class Catalogs { return armor.keySet(); } - public static boolean allArmorSeen(){ - for (Boolean val : armor.values()){ - if (!val) return false; - } - return true; - } - private static final LinkedHashMap, Boolean> wands = new LinkedHashMap<>(); static { wands.put( WandOfMagicMissile.class, false); @@ -209,13 +203,6 @@ public class Catalogs { return wands.keySet(); } - public static boolean allWandsSeen(){ - for (Boolean val : wands.values()){ - if (!val) return false; - } - return true; - } - private static final LinkedHashMap, Boolean> rings = new LinkedHashMap<>(); static { rings.put( RingOfAccuracy.class, false); @@ -235,13 +222,6 @@ public class Catalogs { return rings.keySet(); } - public static boolean allRingsSeen(){ - for (Boolean val : rings.values()){ - if (!val) return false; - } - return true; - } - private static final LinkedHashMap, Boolean> artifacts = new LinkedHashMap<>(); static{ artifacts.put( AlchemistsToolkit.class, false); @@ -263,13 +243,6 @@ public class Catalogs { return artifacts.keySet(); } - public static boolean allArtifactsSeen(){ - for (Boolean val : artifacts.values()){ - if (!val) return false; - } - return true; - } - private static final LinkedHashMap, Boolean> potions = new LinkedHashMap<>(); static { potions.put( PotionOfHealing.class, false); @@ -290,13 +263,6 @@ public class Catalogs { return potions.keySet(); } - public static boolean allPotionsSeen(){ - for (Boolean val : potions.values()){ - if (!val) return false; - } - return true; - } - private static final LinkedHashMap, Boolean> scrolls = new LinkedHashMap<>(); static { scrolls.put( ScrollOfIdentify.class, false); @@ -317,14 +283,7 @@ public class Catalogs { return scrolls.keySet(); } - public static boolean allScrollsSeen(){ - for (Boolean val : scrolls.values()){ - if (!val) return false; - } - return true; - } - - private static final ArrayList, Boolean>> allCatalogs = new ArrayList<>(); + public static final ArrayList, Boolean>> allCatalogs = new ArrayList<>(); static{ allCatalogs.add(weapons); allCatalogs.add(armor); @@ -335,6 +294,26 @@ public class Catalogs { allCatalogs.add(scrolls); } + public static LinkedHashMap, Boolean>, Badges.Badge> catalogBadges = new LinkedHashMap<>(); + static { + catalogBadges.put(weapons, Badges.Badge.ALL_WEAPONS_IDENTIFIED); + catalogBadges.put(armor, Badges.Badge.ALL_ARMOR_IDENTIFIED); + catalogBadges.put(wands, Badges.Badge.ALL_WANDS_IDENTIFIED); + catalogBadges.put(rings, Badges.Badge.ALL_RINGS_IDENTIFIED); + catalogBadges.put(artifacts, Badges.Badge.ALL_ARTIFACTS_IDENTIFIED); + catalogBadges.put(potions, Badges.Badge.ALL_POTIONS_IDENTIFIED); + catalogBadges.put(scrolls, Badges.Badge.ALL_SCROLLS_IDENTIFIED); + } + + public static boolean allSeen( Collection> items){ + for (Class item : items){ + if (!isSeen(item)){ + return false; + } + } + return true; + } + public static boolean isSeen(Class itemClass){ for (LinkedHashMap, Boolean> catalog : allCatalogs) { if (catalog.containsKey(itemClass)) { @@ -348,9 +327,96 @@ public class Catalogs { for (LinkedHashMap, Boolean> catalog : allCatalogs) { if (catalog.containsKey(itemClass)) { catalog.put(itemClass, true); + saveNeeded = true; } } Badges.validateItemsIdentified(); } + private static boolean saveNeeded = false; + + public static void save(){ + if (!saveNeeded){ + return; + } + + Badges.loadGlobal(); + + Bundle bundle = new Bundle(); + + ArrayList seen = new ArrayList<>(); + + //if we have identified all items of a set, we use the badge to keep track instead. + if (!Badges.global.contains(Badges.Badge.ALL_ITEMS_IDENTIFIED)) { + for (LinkedHashMap, Boolean> cat : allCatalogs) { + if (!Badges.global.contains(catalogBadges.get(cat))) { + for (Class item : cat.keySet()) { + if (cat.get(item)) seen.add(item.getSimpleName()); + } + } + } + } + + bundle.put( "catalogs", seen.toArray(new String[0])); + + try { + OutputStream output = Game.instance.openFileOutput( "journal.dat", Game.MODE_PRIVATE ); + Bundle.write( bundle, output ); + output.close(); + saveNeeded = false; + } catch (IOException e) { + ShatteredPixelDungeon.reportException(e); + } + + } + + private static boolean loaded = false; + + public static void load( ){ + if (loaded){ + return; + } + + Badges.loadGlobal(); + + //logic for if we have all badges + if (Badges.global.contains(Badges.Badge.ALL_ITEMS_IDENTIFIED)){ + for ( LinkedHashMap, Boolean> cat : allCatalogs){ + for (Class item : cat.keySet()){ + cat.put(item, true); + } + } + loaded = true; + return; + } + + //catalog-specific badge logic + for (LinkedHashMap, Boolean> cat : allCatalogs){ + if (Badges.global.contains(catalogBadges.get(cat))){ + for (Class item : cat.keySet()){ + cat.put(item, true); + } + } + } + + //general save/load + try { + InputStream input = Game.instance.openFileInput("journal.dat"); + Bundle bundle = Bundle.read(input); + input.close(); + + List seen = Arrays.asList(bundle.getStringArray("catalogs")); + + for ( LinkedHashMap, Boolean> cat : allCatalogs){ + for (Class item : cat.keySet()){ + cat.put(item, seen.contains( item.getSimpleName() )); + } + } + + loaded = true; + } catch (IOException e){ + + } + } + } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/GameScene.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/GameScene.java index 3e2e41c91..74ab6fe13 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/GameScene.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/GameScene.java @@ -47,6 +47,7 @@ import com.shatteredpixel.shatteredpixeldungeon.items.bags.SeedPouch; import com.shatteredpixel.shatteredpixeldungeon.items.bags.WandHolster; import com.shatteredpixel.shatteredpixeldungeon.items.potions.Potion; import com.shatteredpixel.shatteredpixeldungeon.items.scrolls.ScrollOfTeleportation; +import com.shatteredpixel.shatteredpixeldungeon.journal.Catalogs; import com.shatteredpixel.shatteredpixeldungeon.levels.RegularLevel; import com.shatteredpixel.shatteredpixeldungeon.levels.features.Chasm; import com.shatteredpixel.shatteredpixeldungeon.levels.traps.Trap; @@ -439,6 +440,7 @@ public class GameScene extends PixelScene { scene = null; Badges.saveGlobal(); + Catalogs.save(); super.destroy(); } @@ -448,6 +450,7 @@ public class GameScene extends PixelScene { try { Dungeon.saveAll(); Badges.saveGlobal(); + Catalogs.save(); } catch (IOException e) { ShatteredPixelDungeon.reportException(e); } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/StartScene.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/StartScene.java index 8e7779d02..3ae550bcd 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/StartScene.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/StartScene.java @@ -30,6 +30,7 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.hero.HeroClass; import com.shatteredpixel.shatteredpixeldungeon.effects.BannerSprites; import com.shatteredpixel.shatteredpixeldungeon.effects.BannerSprites.Type; import com.shatteredpixel.shatteredpixeldungeon.effects.Speck; +import com.shatteredpixel.shatteredpixeldungeon.journal.Catalogs; import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; import com.shatteredpixel.shatteredpixeldungeon.ui.Archs; import com.shatteredpixel.shatteredpixeldungeon.ui.ExitButton; @@ -83,6 +84,7 @@ public class StartScene extends PixelScene { super.create(); Badges.loadGlobal(); + Catalogs.load(); uiCamera.visible = false;