2015-01-20 05:56:36 +00:00
|
|
|
package com.shatteredpixel.shatteredpixeldungeon;
|
2015-01-19 17:01:04 +00:00
|
|
|
|
|
|
|
import com.shatteredpixel.shatteredpixeldungeon.items.Item;
|
|
|
|
import com.watabou.utils.Bundlable;
|
|
|
|
import com.watabou.utils.Bundle;
|
2015-01-20 22:26:53 +00:00
|
|
|
import com.watabou.utils.Random;
|
2015-01-19 17:01:04 +00:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by debenhame on 16/01/2015.
|
|
|
|
*/
|
|
|
|
public class QuickSlot {
|
|
|
|
|
|
|
|
/**
|
2015-01-21 16:01:05 +00:00
|
|
|
* Slots contain objects which are also in a player's inventory. The one exception to this is when quantity is 0,
|
|
|
|
* which can happen for a stackable item that has been 'used up', these are refered to a placeholders.
|
2015-01-19 17:01:04 +00:00
|
|
|
*/
|
|
|
|
|
2015-01-21 16:01:05 +00:00
|
|
|
//note that the current max size is coded at 4, due to UI constraints, but it could be much much bigger with no issue.
|
|
|
|
public static int SIZE = 4;
|
|
|
|
private Item[] slots = new Item[SIZE];
|
|
|
|
|
|
|
|
|
|
|
|
//direct array interaction methods, everything should build from these methods.
|
2015-01-19 17:01:04 +00:00
|
|
|
public void setSlot(int slot, Item item){
|
2015-01-21 16:01:05 +00:00
|
|
|
clearItem(item); //we don't want to allow the same item in multiple slots.
|
|
|
|
slots[slot] = item;
|
2015-01-19 17:01:04 +00:00
|
|
|
}
|
|
|
|
|
2015-01-21 16:01:05 +00:00
|
|
|
public void clearSlot(int slot){
|
|
|
|
slots[slot] = null;
|
2015-01-19 17:01:04 +00:00
|
|
|
}
|
|
|
|
|
2015-01-21 16:01:05 +00:00
|
|
|
public void reset(){
|
|
|
|
slots = new Item[SIZE];
|
2015-01-19 17:01:04 +00:00
|
|
|
}
|
|
|
|
|
2015-01-21 16:01:05 +00:00
|
|
|
public Item getItem(int slot){
|
|
|
|
return slots[slot];
|
2015-01-19 17:01:04 +00:00
|
|
|
}
|
|
|
|
|
2015-01-21 16:01:05 +00:00
|
|
|
|
|
|
|
//utility methods, for easier use of the internal array.
|
|
|
|
public int getSlot(Item item) {
|
|
|
|
for (int i = 0; i < SIZE; i++)
|
|
|
|
if (getItem(i) == item)
|
|
|
|
return i;
|
|
|
|
return -1;
|
2015-01-19 17:01:04 +00:00
|
|
|
}
|
|
|
|
|
2015-01-21 16:01:05 +00:00
|
|
|
public Boolean isPlaceholder(int slot){
|
|
|
|
return getItem(slot) != null && getItem(slot).quantity() == 0;
|
2015-01-19 17:01:04 +00:00
|
|
|
}
|
|
|
|
|
2015-01-21 16:01:05 +00:00
|
|
|
public void clearItem(Item item){
|
|
|
|
if (contains(item))
|
|
|
|
clearSlot(getSlot(item));
|
2015-01-19 17:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean contains(Item item){
|
2015-01-21 16:01:05 +00:00
|
|
|
return getSlot(item) != -1;
|
2015-01-19 17:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void replaceSimilar(Item item){
|
2015-01-21 16:01:05 +00:00
|
|
|
for (int i = 0; i < SIZE; i++)
|
|
|
|
if (getItem(i) != null && item.isSimilar(getItem(i)))
|
2015-01-19 17:01:04 +00:00
|
|
|
setSlot( i , item );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void convertToPlaceholder(Item item){
|
|
|
|
Item placeholder = Item.virtual(item.getClass());
|
|
|
|
|
2015-01-21 16:01:05 +00:00
|
|
|
if (placeholder != null && contains(item))
|
|
|
|
for (int i = 0; i < SIZE; i++)
|
|
|
|
if (getItem(i) == item)
|
2015-01-19 17:01:04 +00:00
|
|
|
setSlot( i , placeholder );
|
|
|
|
}
|
|
|
|
|
2015-01-20 22:26:53 +00:00
|
|
|
public Item randomNonePlaceholder(){
|
|
|
|
|
|
|
|
ArrayList<Item> result = new ArrayList<Item>();
|
2015-01-21 16:01:05 +00:00
|
|
|
for (int i = 0; i < SIZE; i ++)
|
|
|
|
if (getItem(i) != null && !isPlaceholder(i))
|
|
|
|
result.add(getItem(i));
|
2015-01-20 22:26:53 +00:00
|
|
|
|
|
|
|
return Random.element(result);
|
|
|
|
}
|
|
|
|
|
2015-01-19 17:01:04 +00:00
|
|
|
private final String PLACEHOLDERS = "placeholders";
|
|
|
|
private final String PLACEMENTS = "placements";
|
|
|
|
|
2015-01-21 16:01:05 +00:00
|
|
|
/**
|
|
|
|
* Placements array is used as order is preserved while bundling, but exact index is not, so if we
|
|
|
|
* bundle both the placeholders (which preserves their order) and an array telling us where the placeholders are,
|
|
|
|
* we can reconstruct them perfectly.
|
|
|
|
*/
|
|
|
|
|
2015-01-19 17:01:04 +00:00
|
|
|
public void storePlaceholders(Bundle bundle){
|
2015-01-21 16:01:05 +00:00
|
|
|
ArrayList<Item> placeholders = new ArrayList<Item>(SIZE);
|
|
|
|
boolean[] placements = new boolean[SIZE];
|
2015-01-19 17:01:04 +00:00
|
|
|
|
2015-01-21 16:01:05 +00:00
|
|
|
for (int i = 0; i < SIZE; i++)
|
2015-01-19 17:01:04 +00:00
|
|
|
if (isPlaceholder(i)) {
|
2015-01-21 16:01:05 +00:00
|
|
|
placeholders.add(getItem(i));
|
2015-01-19 17:01:04 +00:00
|
|
|
placements[i] = true;
|
|
|
|
}
|
|
|
|
bundle.put( PLACEHOLDERS, placeholders );
|
|
|
|
bundle.put( PLACEMENTS, placements );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void restorePlaceholders(Bundle bundle){
|
|
|
|
Collection<Bundlable> placeholders = bundle.getCollection(PLACEHOLDERS);
|
|
|
|
boolean[] placements = bundle.getBooleanArray( PLACEMENTS );
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (Bundlable item : placeholders){
|
2015-01-21 16:03:01 +00:00
|
|
|
while (!placements[i]) i++;
|
2015-01-19 17:01:04 +00:00
|
|
|
setSlot( i, (Item)item );
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|