v0.4.0: Refactored item status handler, now independent of sprite sheet
This commit is contained in:
@@ -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<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>>();
|
||||
|
||||
restore( bundle, labels, images );
|
||||
|
||||
this.itemLabels = new HashMap<>();
|
||||
this.labelImages = new HashMap<>(labelImages);
|
||||
known = new HashSet<>();
|
||||
|
||||
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 ) );
|
||||
|
||||
for (int i=0; i < items.length; i++) {
|
||||
|
||||
Class<? extends T> item = (Class<? extends T>)(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<String> labelsLeft ) {
|
||||
|
||||
for (int i=0; i < items.length; i++) {
|
||||
|
||||
Class<? extends T> 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<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 ) {
|
||||
|
||||
Reference in New Issue
Block a user