v0.4.0: Refactored item status handler, now independent of sprite sheet

This commit is contained in:
Evan Debenham 2016-05-13 15:26:05 -04:00
parent c6233b274c
commit 07ec295889
4 changed files with 123 additions and 132 deletions

View File

@ -20,108 +20,90 @@
*/ */
package com.shatteredpixel.shatteredpixeldungeon.items; package com.shatteredpixel.shatteredpixeldungeon.items;
import com.shatteredpixel.shatteredpixeldungeon.Dungeon;
import com.watabou.utils.Bundle; import com.watabou.utils.Bundle;
import com.watabou.utils.Random; import com.watabou.utils.Random;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
public class ItemStatusHandler<T extends Item> { public class ItemStatusHandler<T extends Item> {
private Class<? extends T>[] items; private Class<? extends T>[] items;
private HashMap<Class<? extends T>, String> itemLabels;
private HashMap<Class<? extends T>, Integer> images; private HashMap<String, Integer> labelImages;
private HashMap<Class<? extends T>, String> labels;
private HashSet<Class<? extends T>> known; private HashSet<Class<? extends T>> known;
public ItemStatusHandler( Class<? extends T>[] items, String[] allLabels, Integer[] allImages ) { public ItemStatusHandler( Class<? extends T>[] items, HashMap<String, Integer> labelImages ) {
this.items = items; this.items = items;
this.images = new HashMap<Class<? extends T>, Integer>(); this.itemLabels = new HashMap<>();
this.labels = new HashMap<Class<? extends T>, String>(); this.labelImages = new HashMap<>(labelImages);
known = new HashSet<Class<? extends T>>(); known = new HashSet<Class<? extends T>>();
ArrayList<String> labelsLeft = new ArrayList<String>( Arrays.asList( allLabels ) ); ArrayList<String> labelsLeft = new ArrayList<String>( labelImages.keySet() );
ArrayList<Integer> imagesLeft = new ArrayList<Integer>( Arrays.asList( allImages ) );
for (int i=0; i < items.length; i++) { for (int i=0; i < items.length; i++) {
Class<? extends T> item = (Class<? extends T>)(items[i]); Class<? extends T> item = items[i];
int index = Random.Int( labelsLeft.size() ); int index = Random.Int( labelsLeft.size() );
labels.put( item, labelsLeft.get( index ) ); itemLabels.put( item, labelsLeft.get( index ) );
labelsLeft.remove( index ); labelsLeft.remove( index );
images.put( item, imagesLeft.get( index ) );
imagesLeft.remove( index );
} }
} }
public ItemStatusHandler( Class<? extends T>[] items, String[] labels, Integer[] images, Bundle bundle ) { public ItemStatusHandler( Class<? extends T>[] items, HashMap<String, Integer> labelImages, Bundle bundle ) {
this.items = items; this.items = items;
this.images = new HashMap<Class<? extends T>, Integer>(); this.itemLabels = new HashMap<>();
this.labels = new HashMap<Class<? extends T>, String>(); this.labelImages = new HashMap<>(labelImages);
known = new HashSet<Class<? extends T>>(); known = new HashSet<>();
restore( bundle, labels, images ); ArrayList<String> allLabels = new ArrayList<String>( labelImages.keySet() );
restore(bundle, allLabels);
} }
private static final String PFX_IMAGE = "_image";
private static final String PFX_LABEL = "_label"; private static final String PFX_LABEL = "_label";
private static final String PFX_KNOWN = "_known"; private static final String PFX_KNOWN = "_known";
public void save( Bundle bundle ) { public void save( Bundle bundle ) {
for (int i=0; i < items.length; i++) { for (int i=0; i < items.length; i++) {
String itemName = items[i].toString(); String itemName = items[i].toString();
bundle.put( itemName + PFX_IMAGE, images.get( items[i] ) ); bundle.put( itemName + PFX_LABEL, itemLabels.get( items[i] ) );
bundle.put( itemName + PFX_LABEL, labels.get( items[i] ) );
bundle.put( itemName + PFX_KNOWN, known.contains( items[i] ) ); bundle.put( itemName + PFX_KNOWN, known.contains( items[i] ) );
} }
} }
private void restore( Bundle bundle, String[] allLabels, Integer[] allImages ) { private void restore( Bundle bundle, ArrayList<String> labelsLeft ) {
ArrayList<String> labelsLeft = new ArrayList<String>( Arrays.asList( allLabels ) );
ArrayList<Integer> imagesLeft = new ArrayList<Integer>( Arrays.asList( allImages ) );
for (int i=0; i < items.length; i++) { for (int i=0; i < items.length; i++) {
Class<? extends T> item = (Class<? extends T>)(items[i]); Class<? extends T> item = items[i];
String itemName = item.toString(); String itemName = item.toString();
if (bundle.contains( itemName + PFX_LABEL ) && Dungeon.version > 4) { if (bundle.contains( itemName + PFX_LABEL )) {
String label = bundle.getString( itemName + PFX_LABEL ); String label = bundle.getString( itemName + PFX_LABEL );
labels.put( item, label ); itemLabels.put( item, label );
labelsLeft.remove( label ); labelsLeft.remove( label );
Integer image = bundle.getInt( itemName + PFX_IMAGE );
images.put( item, image );
imagesLeft.remove( image );
if (bundle.getBoolean( itemName + PFX_KNOWN )) { if (bundle.getBoolean( itemName + PFX_KNOWN )) {
known.add( item ); 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 { } else {
int index = Random.Int( labelsLeft.size() ); int index = Random.Int( labelsLeft.size() );
labels.put( item, labelsLeft.get( index ) ); itemLabels.put( item, labelsLeft.get( index ) );
labelsLeft.remove( index ); labelsLeft.remove( index );
images.put( item, imagesLeft.get( index ) );
imagesLeft.remove( index );
if (bundle.contains( itemName + PFX_KNOWN ) && bundle.getBoolean( itemName + PFX_KNOWN )) { if (bundle.contains( itemName + PFX_KNOWN ) && bundle.getBoolean( itemName + PFX_KNOWN )) {
known.add( item ); known.add( item );
} }
@ -130,11 +112,11 @@ public class ItemStatusHandler<T extends Item> {
} }
public int image( T item ) { public int image( T item ) {
return images.get( item.getClass() ); return labelImages.get(label(item));
} }
public String label( T item ) { public String label( T item ) {
return labels.get( item.getClass() ); return itemLabels.get(item.getClass());
} }
public boolean isKnown( T item ) { public boolean isKnown( T item ) {

View File

@ -44,46 +44,48 @@ import com.watabou.noosa.audio.Sample;
import com.watabou.utils.Bundle; import com.watabou.utils.Bundle;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
public class Potion extends Item { 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; private static final float TIME_TO_DRINK = 1f;
protected Integer initials; protected Integer initials;
private static final Class<?>[] potions = { private static final Class<?>[] potions = {
PotionOfHealing.class, PotionOfHealing.class,
PotionOfExperience.class, PotionOfExperience.class,
PotionOfToxicGas.class, PotionOfToxicGas.class,
PotionOfLiquidFlame.class, PotionOfLiquidFlame.class,
PotionOfStrength.class, PotionOfStrength.class,
PotionOfParalyticGas.class, PotionOfParalyticGas.class,
PotionOfLevitation.class, PotionOfLevitation.class,
PotionOfMindVision.class, PotionOfMindVision.class,
PotionOfPurity.class, PotionOfPurity.class,
PotionOfInvisibility.class, PotionOfInvisibility.class,
PotionOfMight.class, PotionOfMight.class,
PotionOfFrost.class PotionOfFrost.class
};
private static final HashMap<String, Integer> colors = new HashMap<String, Integer>() {
{
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<Potion> handler; private static ItemStatusHandler<Potion> handler;
@ -98,7 +100,7 @@ public class Potion extends Item {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void initColors() { public static void initColors() {
handler = new ItemStatusHandler<>( (Class<? extends Potion>[])potions, colors, images ); handler = new ItemStatusHandler<>( (Class<? extends Potion>[])potions, colors );
} }
public static void save( Bundle bundle ) { public static void save( Bundle bundle ) {
@ -107,7 +109,7 @@ public class Potion extends Item {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void restore( Bundle bundle ) { public static void restore( Bundle bundle ) {
handler = new ItemStatusHandler<>( (Class<? extends Potion>[])potions, colors, images, bundle ); handler = new ItemStatusHandler<>( (Class<? extends Potion>[])potions, colors, bundle );
} }
public Potion() { public Potion() {

View File

@ -35,6 +35,8 @@ import com.shatteredpixel.shatteredpixeldungeon.utils.GLog;
import com.watabou.utils.Bundle; import com.watabou.utils.Bundle;
import com.watabou.utils.Random; import com.watabou.utils.Random;
import java.util.HashMap;
public class Ring extends KindofMisc { public class Ring extends KindofMisc {
private static final int TICKS_TO_KNOW = 200; private static final int TICKS_TO_KNOW = 200;
@ -54,21 +56,23 @@ public class Ring extends KindofMisc {
RingOfTenacity.class, RingOfTenacity.class,
RingOfWealth.class, RingOfWealth.class,
}; };
private static final String[] gems =
{"diamond", "opal", "garnet", "ruby", "amethyst", "topaz", "onyx", "tourmaline", "emerald", "sapphire", "quartz", "agate"}; private static final HashMap<String, Integer> gems = new HashMap<String, Integer>() {
private static final Integer[] images = { {
ItemSpriteSheet.RING_DIAMOND, put("garnet",ItemSpriteSheet.RING_GARNET);
ItemSpriteSheet.RING_OPAL, put("ruby",ItemSpriteSheet.RING_RUBY);
ItemSpriteSheet.RING_GARNET, put("topaz",ItemSpriteSheet.RING_TOPAZ);
ItemSpriteSheet.RING_RUBY, put("emerald",ItemSpriteSheet.RING_EMERALD);
ItemSpriteSheet.RING_AMETHYST, put("onyx",ItemSpriteSheet.RING_ONYX);
ItemSpriteSheet.RING_TOPAZ, put("opal",ItemSpriteSheet.RING_OPAL);
ItemSpriteSheet.RING_ONYX, put("tourmaline",ItemSpriteSheet.RING_TOURMALINE);
ItemSpriteSheet.RING_TOURMALINE, put("sapphire",ItemSpriteSheet.RING_SAPPHIRE);
ItemSpriteSheet.RING_EMERALD, put("amethyst",ItemSpriteSheet.RING_AMETHYST);
ItemSpriteSheet.RING_SAPPHIRE, put("quartz",ItemSpriteSheet.RING_QUARTZ);
ItemSpriteSheet.RING_QUARTZ, put("agate",ItemSpriteSheet.RING_AGATE);
ItemSpriteSheet.RING_AGATE}; put("diamond",ItemSpriteSheet.RING_DIAMOND);
}
};
private static ItemStatusHandler<Ring> handler; private static ItemStatusHandler<Ring> handler;
@ -78,7 +82,7 @@ public class Ring extends KindofMisc {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void initGems() { public static void initGems() {
handler = new ItemStatusHandler<>( (Class<? extends Ring>[])rings, gems, images ); handler = new ItemStatusHandler<>( (Class<? extends Ring>[])rings, gems );
} }
public static void save( Bundle bundle ) { public static void save( Bundle bundle ) {
@ -87,7 +91,7 @@ public class Ring extends KindofMisc {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void restore( Bundle bundle ) { public static void restore( Bundle bundle ) {
handler = new ItemStatusHandler<>( (Class<? extends Ring>[])rings, gems, images, bundle ); handler = new ItemStatusHandler<>( (Class<? extends Ring>[])rings, gems, bundle );
} }
public Ring() { public Ring() {

View File

@ -33,6 +33,7 @@ import com.shatteredpixel.shatteredpixeldungeon.utils.GLog;
import com.watabou.utils.Bundle; import com.watabou.utils.Bundle;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
public abstract class Scroll extends Item { public abstract class Scroll extends Item {
@ -57,21 +58,23 @@ public abstract class Scroll extends Item {
ScrollOfPsionicBlast.class, ScrollOfPsionicBlast.class,
ScrollOfMirrorImage.class ScrollOfMirrorImage.class
}; };
private static final String[] runes =
{"KAUNAN", "SOWILO", "LAGUZ", "YNGVI", "GYFU", "RAIDO", "ISAZ", "MANNAZ", "NAUDIZ", "BERKANAN", "ODAL", "TIWAZ"}; private static final HashMap<String, Integer> runes = new HashMap<String, Integer>() {
private static final Integer[] images = { {
ItemSpriteSheet.SCROLL_KAUNAN, put("KAUNAN",ItemSpriteSheet.SCROLL_KAUNAN);
ItemSpriteSheet.SCROLL_SOWILO, put("SOWILO",ItemSpriteSheet.SCROLL_SOWILO);
ItemSpriteSheet.SCROLL_LAGUZ, put("LAGUZ",ItemSpriteSheet.SCROLL_LAGUZ);
ItemSpriteSheet.SCROLL_YNGVI, put("YNGVI",ItemSpriteSheet.SCROLL_YNGVI);
ItemSpriteSheet.SCROLL_GYFU, put("GYFU",ItemSpriteSheet.SCROLL_GYFU);
ItemSpriteSheet.SCROLL_RAIDO, put("RAIDO",ItemSpriteSheet.SCROLL_RAIDO);
ItemSpriteSheet.SCROLL_ISAZ, put("ISAZ",ItemSpriteSheet.SCROLL_ISAZ);
ItemSpriteSheet.SCROLL_MANNAZ, put("MANNAZ",ItemSpriteSheet.SCROLL_MANNAZ);
ItemSpriteSheet.SCROLL_NAUDIZ, put("NAUDIZ",ItemSpriteSheet.SCROLL_NAUDIZ);
ItemSpriteSheet.SCROLL_BERKANAN, put("BERKANAN",ItemSpriteSheet.SCROLL_BERKANAN);
ItemSpriteSheet.SCROLL_ODAL, put("ODAL",ItemSpriteSheet.SCROLL_ODAL);
ItemSpriteSheet.SCROLL_TIWAZ}; put("TIWAZ",ItemSpriteSheet.SCROLL_TIWAZ);
}
};
private static ItemStatusHandler<Scroll> handler; private static ItemStatusHandler<Scroll> handler;
@ -86,7 +89,7 @@ public abstract class Scroll extends Item {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void initLabels() { public static void initLabels() {
handler = new ItemStatusHandler<>( (Class<? extends Scroll>[])scrolls, runes, images ); handler = new ItemStatusHandler<>( (Class<? extends Scroll>[])scrolls, runes );
} }
public static void save( Bundle bundle ) { public static void save( Bundle bundle ) {
@ -95,7 +98,7 @@ public abstract class Scroll extends Item {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void restore( Bundle bundle ) { public static void restore( Bundle bundle ) {
handler = new ItemStatusHandler<>( (Class<? extends Scroll>[])scrolls, runes, images, bundle ); handler = new ItemStatusHandler<>( (Class<? extends Scroll>[])scrolls, runes, bundle );
} }
public Scroll() { public Scroll() {