Android game rpg inventory system

前端 未结 5 883
不知归路
不知归路 2021-01-12 04:37

I am using an ArrayList as my \"inventory\". I am having trouble figuring out a way to add multiples of the same item without taking up a spot in the \"inventory\". For exam

5条回答
  •  执念已碎
    2021-01-12 05:06

    Similar to aioobe's solution, you can use TObjectIntHashMap.

    TObjectIntHashMap bag = new TObjectIntHashMap();
    
    // to add `toAdd`
    bag.adjustOrPutValue(item, toAdd, toAdd);
    
    // to get the count.
    int count = bag.get(item);
    
    // to remove some
    int count = bag.get(item);
    if (count < toRemove) throw new IllegalStateException();
    bag.adjustValue(item, -toRemove);
    
    // to removeAll
    int count = bag.remove(item);
    

    You can create a multiples class.

    class MultipleOf {
        int count;
        final T t;
    }
    
    List bag = new ArrayList();
    bag.add(new Sword());
    bag.add(new MultipleOf(5, new Potion());
    

    Or you can use a collection which records multiples by count.

    e.g. a Bag

    Bag bag = new HashBag() or TreeBag();
    bag.add(new Sword());
    bag.add(new Potion(), 5);
    int count = bag.getCount(new Potion());
    

提交回复
热议问题