v0.8.1: refactored some bag code, bags no longer take inventory space
This commit is contained in:
parent
8578e3c397
commit
128c170301
|
@ -41,8 +41,6 @@ import java.util.Iterator;
|
|||
|
||||
public class Belongings implements Iterable<Item> {
|
||||
|
||||
public static final int BACKPACK_SIZE = 20;
|
||||
|
||||
private Hero owner;
|
||||
|
||||
public Bag backpack;
|
||||
|
@ -55,10 +53,20 @@ public class Belongings implements Iterable<Item> {
|
|||
public Belongings( Hero owner ) {
|
||||
this.owner = owner;
|
||||
|
||||
backpack = new Bag() {{
|
||||
name = Messages.get(Bag.class, "name");
|
||||
size = BACKPACK_SIZE;
|
||||
}};
|
||||
backpack = new Bag() {
|
||||
{
|
||||
name = Messages.get(Bag.class, "name");
|
||||
}
|
||||
public int capacity(){
|
||||
int cap = super.capacity();
|
||||
for (Item item : items){
|
||||
if (item instanceof Bag){
|
||||
cap++;
|
||||
}
|
||||
}
|
||||
return cap;
|
||||
}
|
||||
};
|
||||
backpack.owner = owner;
|
||||
}
|
||||
|
||||
|
|
|
@ -170,20 +170,25 @@ public class Item implements Bundlable {
|
|||
}
|
||||
|
||||
public boolean collect( Bag container ) {
|
||||
|
||||
|
||||
ArrayList<Item> items = container.items;
|
||||
|
||||
if (items.contains( this )) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
for (Item item:items) {
|
||||
if (item instanceof Bag && ((Bag)item).grab( this )) {
|
||||
if (item instanceof Bag && ((Bag)item).canHold( this )) {
|
||||
if (collect( (Bag)item )){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!container.canHold(this)){
|
||||
GLog.n( Messages.get(Item.class, "pack_full", container.name()) );
|
||||
return false;
|
||||
}
|
||||
|
||||
if (items.contains( this )) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (stackable) {
|
||||
for (Item item:items) {
|
||||
|
@ -194,25 +199,17 @@ public class Item implements Bundlable {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (items.size() < container.size) {
|
||||
|
||||
if (Dungeon.hero != null && Dungeon.hero.isAlive()) {
|
||||
Badges.validateItemLevelAquired( this );
|
||||
}
|
||||
|
||||
items.add( this );
|
||||
Dungeon.quickslot.replacePlaceholder(this);
|
||||
updateQuickslot();
|
||||
Collections.sort( items, itemComparator );
|
||||
return true;
|
||||
|
||||
} else {
|
||||
|
||||
GLog.n( Messages.get(Item.class, "pack_full", container.name()) );
|
||||
return false;
|
||||
|
||||
|
||||
if (Dungeon.hero != null && Dungeon.hero.isAlive()) {
|
||||
Badges.validateItemLevelAquired( this );
|
||||
}
|
||||
|
||||
items.add( this );
|
||||
Dungeon.quickslot.replacePlaceholder(this);
|
||||
updateQuickslot();
|
||||
Collections.sort( items, itemComparator );
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public boolean collect() {
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
|
||||
package com.shatteredpixel.shatteredpixeldungeon.items;
|
||||
|
||||
import com.shatteredpixel.shatteredpixeldungeon.Dungeon;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Hero;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.messages.Messages;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.scenes.GameScene;
|
||||
|
@ -50,13 +51,14 @@ public abstract class KindofMisc extends EquipableItem {
|
|||
protected void onSelect(int index) {
|
||||
|
||||
KindofMisc equipped = (index == 0 ? m1 : m2);
|
||||
//temporarily give 1 extra backpack spot to support swapping with a full inventory
|
||||
hero.belongings.backpack.size++;
|
||||
int slot = Dungeon.quickslot.getSlot(KindofMisc.this);
|
||||
detach(hero.belongings.backpack);
|
||||
if (equipped.doUnequip(hero, true, false)) {
|
||||
//fully re-execute rather than just call doEquip as we want to preserve quickslot
|
||||
execute(hero, AC_EQUIP);
|
||||
doEquip(hero);
|
||||
} else {
|
||||
collect();
|
||||
}
|
||||
hero.belongings.backpack.size--;
|
||||
if (slot != -1) Dungeon.quickslot.setSlot(slot, KindofMisc.this);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -49,8 +49,10 @@ public class Bag extends Item implements Iterable<Item> {
|
|||
public Char owner;
|
||||
|
||||
public ArrayList<Item> items = new ArrayList<>();
|
||||
|
||||
public int size = 1;
|
||||
|
||||
public int capacity(){
|
||||
return 20; // default container size
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute( Hero hero, String action ) {
|
||||
|
@ -68,7 +70,7 @@ public class Bag extends Item implements Iterable<Item> {
|
|||
public boolean collect( Bag container ) {
|
||||
|
||||
for (Item item : container.items.toArray( new Item[0] )) {
|
||||
if (grab( item )) {
|
||||
if (canHold( item )) {
|
||||
int slot = Dungeon.quickslot.getSlot(item);
|
||||
item.detachAll(container);
|
||||
if (!item.collect(this)) {
|
||||
|
@ -146,8 +148,17 @@ public class Bag extends Item implements Iterable<Item> {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean grab( Item item ) {
|
||||
|
||||
public boolean canHold( Item item ){
|
||||
if (items.contains(item) || item instanceof Bag || items.size() < capacity()){
|
||||
return true;
|
||||
} else {
|
||||
for (Item i : items) {
|
||||
if (item.isSimilar( i )) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -31,16 +31,18 @@ public class MagicalHolster extends Bag {
|
|||
|
||||
{
|
||||
image = ItemSpriteSheet.HOLSTER;
|
||||
|
||||
size = 20;
|
||||
}
|
||||
|
||||
public static final float HOLSTER_SCALE_FACTOR = 0.85f;
|
||||
public static final float HOLSTER_DURABILITY_FACTOR = 1.2f;
|
||||
|
||||
@Override
|
||||
public boolean grab( Item item ) {
|
||||
return item instanceof Wand || item instanceof MissileWeapon || item instanceof Bomb;
|
||||
public boolean canHold( Item item ) {
|
||||
if (item instanceof Wand || item instanceof MissileWeapon || item instanceof Bomb){
|
||||
return super.canHold(item);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -22,20 +22,25 @@
|
|||
package com.shatteredpixel.shatteredpixeldungeon.items.bags;
|
||||
|
||||
import com.shatteredpixel.shatteredpixeldungeon.items.Item;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.items.bombs.Bomb;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.items.potions.Potion;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.items.wands.Wand;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.items.weapon.missiles.MissileWeapon;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet;
|
||||
|
||||
public class PotionBandolier extends Bag {
|
||||
|
||||
{
|
||||
image = ItemSpriteSheet.BANDOLIER;
|
||||
|
||||
size = 20;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean grab( Item item ) {
|
||||
return item instanceof Potion;
|
||||
public boolean canHold( Item item ) {
|
||||
if (item instanceof Potion){
|
||||
return super.canHold(item);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -22,22 +22,27 @@
|
|||
package com.shatteredpixel.shatteredpixeldungeon.items.bags;
|
||||
|
||||
import com.shatteredpixel.shatteredpixeldungeon.items.Item;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.items.bombs.Bomb;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.items.scrolls.Scroll;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.items.spells.BeaconOfReturning;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.items.spells.Spell;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.items.wands.Wand;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.items.weapon.missiles.MissileWeapon;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet;
|
||||
|
||||
public class ScrollHolder extends Bag {
|
||||
|
||||
{
|
||||
image = ItemSpriteSheet.HOLDER;
|
||||
|
||||
size = 20;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean grab( Item item ) {
|
||||
return item instanceof Scroll || item instanceof Spell;
|
||||
public boolean canHold( Item item ) {
|
||||
if (item instanceof Scroll || item instanceof Spell){
|
||||
return super.canHold(item);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -24,6 +24,8 @@ package com.shatteredpixel.shatteredpixeldungeon.items.bags;
|
|||
import com.shatteredpixel.shatteredpixeldungeon.items.Item;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.items.quest.GooBlob;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.items.quest.MetalShard;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.items.scrolls.Scroll;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.items.spells.Spell;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.items.stones.Runestone;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.plants.Plant;
|
||||
import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet;
|
||||
|
@ -32,14 +34,16 @@ public class VelvetPouch extends Bag {
|
|||
|
||||
{
|
||||
image = ItemSpriteSheet.POUCH;
|
||||
|
||||
size = 20;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean grab( Item item ) {
|
||||
return item instanceof Plant.Seed || item instanceof Runestone
|
||||
|| item instanceof GooBlob || item instanceof MetalShard;
|
||||
public boolean canHold( Item item ) {
|
||||
if (item instanceof Plant.Seed || item instanceof Runestone
|
||||
|| item instanceof GooBlob || item instanceof MetalShard){
|
||||
return super.canHold(item);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -292,7 +292,7 @@ public class ShopRoom extends SpecialRoom {
|
|||
//count up items in the main bag
|
||||
for (Item item : pack.backpack.items) {
|
||||
for (Bag bag : bags.keySet()){
|
||||
if (bag.grab(item)){
|
||||
if (bag.canHold(item)){
|
||||
bags.put(bag, bags.get(bag)+1);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -109,7 +109,6 @@ public class WndBag extends WndTabbed {
|
|||
private String title;
|
||||
|
||||
private int nCols;
|
||||
private int nRows;
|
||||
|
||||
protected int count;
|
||||
protected int col;
|
||||
|
@ -135,15 +134,14 @@ public class WndBag extends WndTabbed {
|
|||
lastBag = bag;
|
||||
|
||||
nCols = PixelScene.landscape() ? COLS_L : COLS_P;
|
||||
nRows = (int)Math.ceil((Belongings.BACKPACK_SIZE + 4) / (float)nCols);
|
||||
|
||||
int slotsWidth = SLOT_WIDTH * nCols + SLOT_MARGIN * (nCols - 1);
|
||||
int slotsHeight = SLOT_HEIGHT * nRows + SLOT_MARGIN * (nRows - 1);
|
||||
|
||||
placeTitle( bag, slotsWidth );
|
||||
|
||||
placeItems( bag );
|
||||
|
||||
int slotsHeight = SLOT_HEIGHT * row + SLOT_MARGIN * (row - 1);
|
||||
|
||||
resize( slotsWidth, slotsHeight + TITLE_HEIGHT );
|
||||
|
||||
Belongings stuff = Dungeon.hero.belongings;
|
||||
|
@ -229,12 +227,16 @@ public class WndBag extends WndTabbed {
|
|||
}
|
||||
|
||||
// Free Space
|
||||
while ((count - 4) < container.size) {
|
||||
while ((count - 4) < container.capacity()) {
|
||||
placeItem( null );
|
||||
}
|
||||
}
|
||||
|
||||
protected void placeItem( final Item item ) {
|
||||
|
||||
count++;
|
||||
|
||||
if (item instanceof Bag) return;
|
||||
|
||||
int x = col * (SLOT_WIDTH + SLOT_MARGIN);
|
||||
int y = TITLE_HEIGHT + row * (SLOT_HEIGHT + SLOT_MARGIN);
|
||||
|
@ -245,8 +247,7 @@ public class WndBag extends WndTabbed {
|
|||
col = 0;
|
||||
row++;
|
||||
}
|
||||
|
||||
count++;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue
Block a user