v0.4.0: Refactored item status handler, now independent of sprite sheet
This commit is contained in:
parent
c6233b274c
commit
07ec295889
|
@ -20,108 +20,90 @@
|
|||
*/
|
||||
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<T extends Item> {
|
||||
|
||||
private Class<? extends T>[] items;
|
||||
|
||||
private HashMap<Class<? extends T>, Integer> images;
|
||||
private HashMap<Class<? extends T>, String> labels;
|
||||
private HashMap<Class<? extends T>, String> itemLabels;
|
||||
private HashMap<String, Integer> labelImages;
|
||||
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.images = new HashMap<Class<? extends T>, Integer>();
|
||||
this.labels = new HashMap<Class<? extends T>, String>();
|
||||
this.itemLabels = new HashMap<>();
|
||||
this.labelImages = new HashMap<>(labelImages);
|
||||
known = new HashSet<Class<? extends T>>();
|
||||
|
||||
ArrayList<String> labelsLeft = new ArrayList<String>( Arrays.asList( allLabels ) );
|
||||
ArrayList<Integer> imagesLeft = new ArrayList<Integer>( Arrays.asList( allImages ) );
|
||||
ArrayList<String> labelsLeft = new ArrayList<String>( labelImages.keySet() );
|
||||
|
||||
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() );
|
||||
|
||||
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<? 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.images = new HashMap<Class<? extends T>, Integer>();
|
||||
this.labels = new HashMap<Class<? extends T>, String>();
|
||||
known = new HashSet<Class<? extends T>>();
|
||||
this.itemLabels = new HashMap<>();
|
||||
this.labelImages = new HashMap<>(labelImages);
|
||||
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_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<String> labelsLeft = new ArrayList<String>( Arrays.asList( allLabels ) );
|
||||
ArrayList<Integer> imagesLeft = new ArrayList<Integer>( Arrays.asList( allImages ) );
|
||||
private void restore( Bundle bundle, ArrayList<String> labelsLeft ) {
|
||||
|
||||
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();
|
||||
|
||||
if (bundle.contains( itemName + PFX_LABEL ) && Dungeon.version > 4) {
|
||||
if (bundle.contains( itemName + PFX_LABEL )) {
|
||||
|
||||
String label = bundle.getString( itemName + PFX_LABEL );
|
||||
labels.put( item, label );
|
||||
itemLabels.put( item, label );
|
||||
labelsLeft.remove( label );
|
||||
|
||||
Integer image = bundle.getInt( itemName + PFX_IMAGE );
|
||||
images.put( item, image );
|
||||
imagesLeft.remove( image );
|
||||
|
||||
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<T extends Item> {
|
|||
}
|
||||
|
||||
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 ) {
|
||||
|
|
|
@ -44,6 +44,7 @@ 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 {
|
||||
|
@ -68,22 +69,23 @@ public class Potion extends Item {
|
|||
PotionOfMight.class,
|
||||
PotionOfFrost.class
|
||||
};
|
||||
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 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 ItemStatusHandler<Potion> handler;
|
||||
|
||||
|
@ -98,7 +100,7 @@ public class Potion extends Item {
|
|||
|
||||
@SuppressWarnings("unchecked")
|
||||
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 ) {
|
||||
|
@ -107,7 +109,7 @@ public class Potion extends Item {
|
|||
|
||||
@SuppressWarnings("unchecked")
|
||||
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() {
|
||||
|
|
|
@ -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<String, Integer> gems = new HashMap<String, Integer>() {
|
||||
{
|
||||
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<Ring> handler;
|
||||
|
||||
|
@ -78,7 +82,7 @@ public class Ring extends KindofMisc {
|
|||
|
||||
@SuppressWarnings("unchecked")
|
||||
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 ) {
|
||||
|
@ -87,7 +91,7 @@ public class Ring extends KindofMisc {
|
|||
|
||||
@SuppressWarnings("unchecked")
|
||||
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() {
|
||||
|
|
|
@ -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<String, Integer> runes = new HashMap<String, Integer>() {
|
||||
{
|
||||
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<Scroll> handler;
|
||||
|
||||
|
@ -86,7 +89,7 @@ public abstract class Scroll extends Item {
|
|||
|
||||
@SuppressWarnings("unchecked")
|
||||
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 ) {
|
||||
|
@ -95,7 +98,7 @@ public abstract class Scroll extends Item {
|
|||
|
||||
@SuppressWarnings("unchecked")
|
||||
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() {
|
||||
|
|
Loading…
Reference in New Issue
Block a user