/* * Pixel Dungeon * Copyright (C) 2012-2014 Oleg Dolya * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see */ package com.shatteredpixel.shatteredpixeldungeon.items; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import com.watabou.utils.Bundle; import com.watabou.utils.Random; public class ItemStatusHandler { private Class[] items; private HashMap, Integer> images; private HashMap, String> labels; private HashSet> known; public ItemStatusHandler( Class[] items, String[] allLabels, Integer[] allImages ) { this.items = items; this.images = new HashMap, Integer>(); this.labels = new HashMap, String>(); known = new HashSet>(); 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]); int index = Random.Int( labelsLeft.size() ); labels.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 ) { this.items = items; this.images = new HashMap, Integer>(); this.labels = new HashMap, String>(); known = new HashSet>(); restore( bundle, labels, images ); } 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_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 )) { 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 ); if (bundle.getBoolean( itemName + PFX_KNOWN )) { known.add( item ); } } else { int index = Random.Int( labelsLeft.size() ); labels.put( item, labelsLeft.get( index ) ); labelsLeft.remove( index ); images.put( item, imagesLeft.get( index ) ); imagesLeft.remove( index ); } } } public int image( T item ) { return images.get( item.getClass() ); } public String label( T item ) { return labels.get( item.getClass() ); } public boolean isKnown( T item ) { return known.contains( item.getClass() ); } @SuppressWarnings("unchecked") public void know( T item ) { known.add( (Class)item.getClass() ); if (known.size() == items.length - 1) { for (int i=0; i < items.length; i++) { if (!known.contains( items[i] )) { known.add( items[i] ); break; } } } } public HashSet> known() { return known; } public HashSet> unknown() { HashSet> result = new HashSet>(); for (Class i : items) { if (!known.contains( i )) { result.add( i ); } } return result; } }