From 07ec295889ad54aac88684a2cbaa6b402e12f4f6 Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Fri, 13 May 2016 15:26:05 -0400 Subject: [PATCH] v0.4.0: Refactored item status handler, now independent of sprite sheet --- .../items/ItemStatusHandler.java | 112 ++++++++---------- .../items/potions/Potion.java | 68 +++++------ .../items/rings/Ring.java | 38 +++--- .../items/scrolls/Scroll.java | 37 +++--- 4 files changed, 123 insertions(+), 132 deletions(-) diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/items/ItemStatusHandler.java b/src/com/shatteredpixel/shatteredpixeldungeon/items/ItemStatusHandler.java index 0fd6f8b71..f6aff37f0 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/items/ItemStatusHandler.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/items/ItemStatusHandler.java @@ -20,107 +20,89 @@ */ package com.shatteredpixel.shatteredpixeldungeon.items; -import com.shatteredpixel.shatteredpixeldungeon.Dungeon; import com.watabou.utils.Bundle; import com.watabou.utils.Random; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; public class ItemStatusHandler { private Class[] items; - - private HashMap, Integer> images; - private HashMap, String> labels; + private HashMap, String> itemLabels; + private HashMap labelImages; private HashSet> known; - - public ItemStatusHandler( Class[] items, String[] allLabels, Integer[] allImages ) { - + + public ItemStatusHandler( Class[] items, HashMap labelImages ) { + this.items = items; - - this.images = new HashMap, Integer>(); - this.labels = new HashMap, String>(); + + this.itemLabels = new HashMap<>(); + this.labelImages = new HashMap<>(labelImages); known = new HashSet>(); - - ArrayList labelsLeft = new ArrayList( Arrays.asList( allLabels ) ); - ArrayList imagesLeft = new ArrayList( Arrays.asList( allImages ) ); - + + ArrayList labelsLeft = new ArrayList( labelImages.keySet() ); + for (int i=0; i < items.length; i++) { - - Class item = (Class)(items[i]); - + + Class item = items[i]; + int index = Random.Int( labelsLeft.size() ); - - labels.put( item, labelsLeft.get( index ) ); + + itemLabels.put( item, labelsLeft.get( index ) ); labelsLeft.remove( index ); - - images.put( item, imagesLeft.get( index ) ); - imagesLeft.remove( index ); + } } - - public ItemStatusHandler( Class[] items, String[] labels, Integer[] images, Bundle bundle ) { - + + public ItemStatusHandler( Class[] items, HashMap labelImages, Bundle bundle ) { + this.items = items; - - this.images = new HashMap, Integer>(); - this.labels = new HashMap, String>(); - known = new HashSet>(); - - restore( bundle, labels, images ); + + this.itemLabels = new HashMap<>(); + this.labelImages = new HashMap<>(labelImages); + known = new HashSet<>(); + + ArrayList allLabels = new ArrayList( labelImages.keySet() ); + + restore(bundle, allLabels); } - - private static final String PFX_IMAGE = "_image"; + private static final String PFX_LABEL = "_label"; private static final String PFX_KNOWN = "_known"; public void save( Bundle bundle ) { for (int i=0; i < items.length; i++) { String itemName = items[i].toString(); - bundle.put( itemName + PFX_IMAGE, images.get( items[i] ) ); - bundle.put( itemName + PFX_LABEL, labels.get( items[i] ) ); + bundle.put( itemName + PFX_LABEL, itemLabels.get( items[i] ) ); bundle.put( itemName + PFX_KNOWN, known.contains( items[i] ) ); } } - - private void restore( Bundle bundle, String[] allLabels, Integer[] allImages ) { - - ArrayList labelsLeft = new ArrayList( Arrays.asList( allLabels ) ); - ArrayList imagesLeft = new ArrayList( Arrays.asList( allImages ) ); - - for (int i=0; i < items.length; i++) { - - Class item = (Class)(items[i]); - String itemName = item.toString(); - - if (bundle.contains( itemName + PFX_LABEL ) && Dungeon.version > 4) { - - String label = bundle.getString( itemName + PFX_LABEL ); - labels.put( item, label ); - labelsLeft.remove( label ); - Integer image = bundle.getInt( itemName + PFX_IMAGE ); - images.put( item, image ); - imagesLeft.remove( image ); + private void restore( Bundle bundle, ArrayList labelsLeft ) { + + for (int i=0; i < items.length; i++) { + + Class item = items[i]; + String itemName = item.toString(); + + if (bundle.contains( itemName + PFX_LABEL )) { + + String label = bundle.getString( itemName + PFX_LABEL ); + itemLabels.put( item, label ); + labelsLeft.remove( label ); if (bundle.getBoolean( itemName + PFX_KNOWN )) { known.add( item ); } - //if there's a new item, give it a random image - //or.. if we're loading from an untrusted version, randomize the image to be safe. } else { - + int index = Random.Int( labelsLeft.size() ); - - labels.put( item, labelsLeft.get( index ) ); + + itemLabels.put( item, labelsLeft.get( index ) ); labelsLeft.remove( index ); - - images.put( item, imagesLeft.get( index ) ); - imagesLeft.remove( index ); if (bundle.contains( itemName + PFX_KNOWN ) && bundle.getBoolean( itemName + PFX_KNOWN )) { known.add( item ); @@ -130,11 +112,11 @@ public class ItemStatusHandler { } public int image( T item ) { - return images.get( item.getClass() ); + return labelImages.get(label(item)); } public String label( T item ) { - return labels.get( item.getClass() ); + return itemLabels.get(item.getClass()); } public boolean isKnown( T item ) { diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/items/potions/Potion.java b/src/com/shatteredpixel/shatteredpixeldungeon/items/potions/Potion.java index 9985550a8..c099a18a3 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/items/potions/Potion.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/items/potions/Potion.java @@ -44,46 +44,48 @@ import com.watabou.noosa.audio.Sample; import com.watabou.utils.Bundle; import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; public class Potion extends Item { - - public static final String AC_DRINK = "DRINK"; + + public static final String AC_DRINK = "DRINK"; private static final float TIME_TO_DRINK = 1f; protected Integer initials; - + private static final Class[] potions = { - PotionOfHealing.class, - PotionOfExperience.class, - PotionOfToxicGas.class, - PotionOfLiquidFlame.class, - PotionOfStrength.class, - PotionOfParalyticGas.class, - PotionOfLevitation.class, - PotionOfMindVision.class, - PotionOfPurity.class, - PotionOfInvisibility.class, - PotionOfMight.class, - PotionOfFrost.class + PotionOfHealing.class, + PotionOfExperience.class, + PotionOfToxicGas.class, + PotionOfLiquidFlame.class, + PotionOfStrength.class, + PotionOfParalyticGas.class, + PotionOfLevitation.class, + PotionOfMindVision.class, + PotionOfPurity.class, + PotionOfInvisibility.class, + PotionOfMight.class, + PotionOfFrost.class + }; + + private static final HashMap colors = new HashMap() { + { + put("crimson",ItemSpriteSheet.POTION_CRIMSON); + put("amber",ItemSpriteSheet.POTION_AMBER); + put("golden",ItemSpriteSheet.POTION_GOLDEN); + put("jade",ItemSpriteSheet.POTION_JADE); + put("turquoise",ItemSpriteSheet.POTION_TURQUOISE); + put("azure",ItemSpriteSheet.POTION_AZURE); + put("indigo",ItemSpriteSheet.POTION_INDIGO); + put("magenta",ItemSpriteSheet.POTION_MAGENTA); + put("bistre",ItemSpriteSheet.POTION_BISTRE); + put("charcoal",ItemSpriteSheet.POTION_CHARCOAL); + put("silver",ItemSpriteSheet.POTION_SILVER); + put("ivory",ItemSpriteSheet.POTION_IVORY); + } }; - private static final String[] colors = { - "turquoise", "crimson", "azure", "jade", "golden", "magenta", - "charcoal", "ivory", "amber", "bistre", "indigo", "silver"}; - private static final Integer[] images = { - ItemSpriteSheet.POTION_TURQUOISE, - ItemSpriteSheet.POTION_CRIMSON, - ItemSpriteSheet.POTION_AZURE, - ItemSpriteSheet.POTION_JADE, - ItemSpriteSheet.POTION_GOLDEN, - ItemSpriteSheet.POTION_MAGENTA, - ItemSpriteSheet.POTION_CHARCOAL, - ItemSpriteSheet.POTION_IVORY, - ItemSpriteSheet.POTION_AMBER, - ItemSpriteSheet.POTION_BISTRE, - ItemSpriteSheet.POTION_INDIGO, - ItemSpriteSheet.POTION_SILVER}; private static ItemStatusHandler handler; @@ -98,7 +100,7 @@ public class Potion extends Item { @SuppressWarnings("unchecked") public static void initColors() { - handler = new ItemStatusHandler<>( (Class[])potions, colors, images ); + handler = new ItemStatusHandler<>( (Class[])potions, colors ); } public static void save( Bundle bundle ) { @@ -107,7 +109,7 @@ public class Potion extends Item { @SuppressWarnings("unchecked") public static void restore( Bundle bundle ) { - handler = new ItemStatusHandler<>( (Class[])potions, colors, images, bundle ); + handler = new ItemStatusHandler<>( (Class[])potions, colors, bundle ); } public Potion() { diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/items/rings/Ring.java b/src/com/shatteredpixel/shatteredpixeldungeon/items/rings/Ring.java index e775d00fc..51798d19f 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/items/rings/Ring.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/items/rings/Ring.java @@ -35,6 +35,8 @@ import com.shatteredpixel.shatteredpixeldungeon.utils.GLog; import com.watabou.utils.Bundle; import com.watabou.utils.Random; +import java.util.HashMap; + public class Ring extends KindofMisc { private static final int TICKS_TO_KNOW = 200; @@ -54,21 +56,23 @@ public class Ring extends KindofMisc { RingOfTenacity.class, RingOfWealth.class, }; - private static final String[] gems = - {"diamond", "opal", "garnet", "ruby", "amethyst", "topaz", "onyx", "tourmaline", "emerald", "sapphire", "quartz", "agate"}; - private static final Integer[] images = { - ItemSpriteSheet.RING_DIAMOND, - ItemSpriteSheet.RING_OPAL, - ItemSpriteSheet.RING_GARNET, - ItemSpriteSheet.RING_RUBY, - ItemSpriteSheet.RING_AMETHYST, - ItemSpriteSheet.RING_TOPAZ, - ItemSpriteSheet.RING_ONYX, - ItemSpriteSheet.RING_TOURMALINE, - ItemSpriteSheet.RING_EMERALD, - ItemSpriteSheet.RING_SAPPHIRE, - ItemSpriteSheet.RING_QUARTZ, - ItemSpriteSheet.RING_AGATE}; + + private static final HashMap gems = new HashMap() { + { + put("garnet",ItemSpriteSheet.RING_GARNET); + put("ruby",ItemSpriteSheet.RING_RUBY); + put("topaz",ItemSpriteSheet.RING_TOPAZ); + put("emerald",ItemSpriteSheet.RING_EMERALD); + put("onyx",ItemSpriteSheet.RING_ONYX); + put("opal",ItemSpriteSheet.RING_OPAL); + put("tourmaline",ItemSpriteSheet.RING_TOURMALINE); + put("sapphire",ItemSpriteSheet.RING_SAPPHIRE); + put("amethyst",ItemSpriteSheet.RING_AMETHYST); + put("quartz",ItemSpriteSheet.RING_QUARTZ); + put("agate",ItemSpriteSheet.RING_AGATE); + put("diamond",ItemSpriteSheet.RING_DIAMOND); + } + }; private static ItemStatusHandler handler; @@ -78,7 +82,7 @@ public class Ring extends KindofMisc { @SuppressWarnings("unchecked") public static void initGems() { - handler = new ItemStatusHandler<>( (Class[])rings, gems, images ); + handler = new ItemStatusHandler<>( (Class[])rings, gems ); } public static void save( Bundle bundle ) { @@ -87,7 +91,7 @@ public class Ring extends KindofMisc { @SuppressWarnings("unchecked") public static void restore( Bundle bundle ) { - handler = new ItemStatusHandler<>( (Class[])rings, gems, images, bundle ); + handler = new ItemStatusHandler<>( (Class[])rings, gems, bundle ); } public Ring() { diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/Scroll.java b/src/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/Scroll.java index 6c17c876c..cb19e625f 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/Scroll.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/Scroll.java @@ -33,6 +33,7 @@ import com.shatteredpixel.shatteredpixeldungeon.utils.GLog; import com.watabou.utils.Bundle; import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; public abstract class Scroll extends Item { @@ -57,21 +58,23 @@ public abstract class Scroll extends Item { ScrollOfPsionicBlast.class, ScrollOfMirrorImage.class }; - private static final String[] runes = - {"KAUNAN", "SOWILO", "LAGUZ", "YNGVI", "GYFU", "RAIDO", "ISAZ", "MANNAZ", "NAUDIZ", "BERKANAN", "ODAL", "TIWAZ"}; - private static final Integer[] images = { - ItemSpriteSheet.SCROLL_KAUNAN, - ItemSpriteSheet.SCROLL_SOWILO, - ItemSpriteSheet.SCROLL_LAGUZ, - ItemSpriteSheet.SCROLL_YNGVI, - ItemSpriteSheet.SCROLL_GYFU, - ItemSpriteSheet.SCROLL_RAIDO, - ItemSpriteSheet.SCROLL_ISAZ, - ItemSpriteSheet.SCROLL_MANNAZ, - ItemSpriteSheet.SCROLL_NAUDIZ, - ItemSpriteSheet.SCROLL_BERKANAN, - ItemSpriteSheet.SCROLL_ODAL, - ItemSpriteSheet.SCROLL_TIWAZ}; + + private static final HashMap runes = new HashMap() { + { + put("KAUNAN",ItemSpriteSheet.SCROLL_KAUNAN); + put("SOWILO",ItemSpriteSheet.SCROLL_SOWILO); + put("LAGUZ",ItemSpriteSheet.SCROLL_LAGUZ); + put("YNGVI",ItemSpriteSheet.SCROLL_YNGVI); + put("GYFU",ItemSpriteSheet.SCROLL_GYFU); + put("RAIDO",ItemSpriteSheet.SCROLL_RAIDO); + put("ISAZ",ItemSpriteSheet.SCROLL_ISAZ); + put("MANNAZ",ItemSpriteSheet.SCROLL_MANNAZ); + put("NAUDIZ",ItemSpriteSheet.SCROLL_NAUDIZ); + put("BERKANAN",ItemSpriteSheet.SCROLL_BERKANAN); + put("ODAL",ItemSpriteSheet.SCROLL_ODAL); + put("TIWAZ",ItemSpriteSheet.SCROLL_TIWAZ); + } + }; private static ItemStatusHandler handler; @@ -86,7 +89,7 @@ public abstract class Scroll extends Item { @SuppressWarnings("unchecked") public static void initLabels() { - handler = new ItemStatusHandler<>( (Class[])scrolls, runes, images ); + handler = new ItemStatusHandler<>( (Class[])scrolls, runes ); } public static void save( Bundle bundle ) { @@ -95,7 +98,7 @@ public abstract class Scroll extends Item { @SuppressWarnings("unchecked") public static void restore( Bundle bundle ) { - handler = new ItemStatusHandler<>( (Class[])scrolls, runes, images, bundle ); + handler = new ItemStatusHandler<>( (Class[])scrolls, runes, bundle ); } public Scroll() {