v0.8.1: refactored some bag code, bags no longer take inventory space

This commit is contained in:
Evan Debenham 2020-06-01 18:06:43 -04:00
parent 8578e3c397
commit 128c170301
10 changed files with 103 additions and 68 deletions

View File

@ -41,8 +41,6 @@ import java.util.Iterator;
public class Belongings implements Iterable<Item> { public class Belongings implements Iterable<Item> {
public static final int BACKPACK_SIZE = 20;
private Hero owner; private Hero owner;
public Bag backpack; public Bag backpack;
@ -55,10 +53,20 @@ public class Belongings implements Iterable<Item> {
public Belongings( Hero owner ) { public Belongings( Hero owner ) {
this.owner = owner; this.owner = owner;
backpack = new Bag() {{ backpack = new Bag() {
name = Messages.get(Bag.class, "name"); {
size = BACKPACK_SIZE; 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; backpack.owner = owner;
} }

View File

@ -173,18 +173,23 @@ public class Item implements Bundlable {
ArrayList<Item> items = container.items; ArrayList<Item> items = container.items;
if (items.contains( this )) {
return true;
}
for (Item item:items) { for (Item item:items) {
if (item instanceof Bag && ((Bag)item).grab( this )) { if (item instanceof Bag && ((Bag)item).canHold( this )) {
if (collect( (Bag)item )){ if (collect( (Bag)item )){
return true; 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) { if (stackable) {
for (Item item:items) { for (Item item:items) {
if (isSimilar( item )) { if (isSimilar( item )) {
@ -195,24 +200,16 @@ public class Item implements Bundlable {
} }
} }
if (items.size() < container.size) { if (Dungeon.hero != null && Dungeon.hero.isAlive()) {
Badges.validateItemLevelAquired( this );
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;
} }
items.add( this );
Dungeon.quickslot.replacePlaceholder(this);
updateQuickslot();
Collections.sort( items, itemComparator );
return true;
} }
public boolean collect() { public boolean collect() {

View File

@ -21,6 +21,7 @@
package com.shatteredpixel.shatteredpixeldungeon.items; package com.shatteredpixel.shatteredpixeldungeon.items;
import com.shatteredpixel.shatteredpixeldungeon.Dungeon;
import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Hero; import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Hero;
import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; import com.shatteredpixel.shatteredpixeldungeon.messages.Messages;
import com.shatteredpixel.shatteredpixeldungeon.scenes.GameScene; import com.shatteredpixel.shatteredpixeldungeon.scenes.GameScene;
@ -50,13 +51,14 @@ public abstract class KindofMisc extends EquipableItem {
protected void onSelect(int index) { protected void onSelect(int index) {
KindofMisc equipped = (index == 0 ? m1 : m2); KindofMisc equipped = (index == 0 ? m1 : m2);
//temporarily give 1 extra backpack spot to support swapping with a full inventory int slot = Dungeon.quickslot.getSlot(KindofMisc.this);
hero.belongings.backpack.size++; detach(hero.belongings.backpack);
if (equipped.doUnequip(hero, true, false)) { if (equipped.doUnequip(hero, true, false)) {
//fully re-execute rather than just call doEquip as we want to preserve quickslot doEquip(hero);
execute(hero, AC_EQUIP); } else {
collect();
} }
hero.belongings.backpack.size--; if (slot != -1) Dungeon.quickslot.setSlot(slot, KindofMisc.this);
} }
}); });

View File

@ -50,7 +50,9 @@ public class Bag extends Item implements Iterable<Item> {
public ArrayList<Item> items = new ArrayList<>(); public ArrayList<Item> items = new ArrayList<>();
public int size = 1; public int capacity(){
return 20; // default container size
}
@Override @Override
public void execute( Hero hero, String action ) { public void execute( Hero hero, String action ) {
@ -68,7 +70,7 @@ public class Bag extends Item implements Iterable<Item> {
public boolean collect( Bag container ) { public boolean collect( Bag container ) {
for (Item item : container.items.toArray( new Item[0] )) { for (Item item : container.items.toArray( new Item[0] )) {
if (grab( item )) { if (canHold( item )) {
int slot = Dungeon.quickslot.getSlot(item); int slot = Dungeon.quickslot.getSlot(item);
item.detachAll(container); item.detachAll(container);
if (!item.collect(this)) { if (!item.collect(this)) {
@ -147,7 +149,16 @@ public class Bag extends Item implements Iterable<Item> {
return false; 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; return false;
} }

View File

@ -31,16 +31,18 @@ public class MagicalHolster extends Bag {
{ {
image = ItemSpriteSheet.HOLSTER; image = ItemSpriteSheet.HOLSTER;
size = 20;
} }
public static final float HOLSTER_SCALE_FACTOR = 0.85f; public static final float HOLSTER_SCALE_FACTOR = 0.85f;
public static final float HOLSTER_DURABILITY_FACTOR = 1.2f; public static final float HOLSTER_DURABILITY_FACTOR = 1.2f;
@Override @Override
public boolean grab( Item item ) { public boolean canHold( Item item ) {
return item instanceof Wand || item instanceof MissileWeapon || item instanceof Bomb; if (item instanceof Wand || item instanceof MissileWeapon || item instanceof Bomb){
return super.canHold(item);
} else {
return false;
}
} }
@Override @Override

View File

@ -22,20 +22,25 @@
package com.shatteredpixel.shatteredpixeldungeon.items.bags; package com.shatteredpixel.shatteredpixeldungeon.items.bags;
import com.shatteredpixel.shatteredpixeldungeon.items.Item; 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.potions.Potion;
import com.shatteredpixel.shatteredpixeldungeon.items.wands.Wand;
import com.shatteredpixel.shatteredpixeldungeon.items.weapon.missiles.MissileWeapon;
import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet;
public class PotionBandolier extends Bag { public class PotionBandolier extends Bag {
{ {
image = ItemSpriteSheet.BANDOLIER; image = ItemSpriteSheet.BANDOLIER;
size = 20;
} }
@Override @Override
public boolean grab( Item item ) { public boolean canHold( Item item ) {
return item instanceof Potion; if (item instanceof Potion){
return super.canHold(item);
} else {
return false;
}
} }
@Override @Override

View File

@ -22,22 +22,27 @@
package com.shatteredpixel.shatteredpixeldungeon.items.bags; package com.shatteredpixel.shatteredpixeldungeon.items.bags;
import com.shatteredpixel.shatteredpixeldungeon.items.Item; 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.scrolls.Scroll;
import com.shatteredpixel.shatteredpixeldungeon.items.spells.BeaconOfReturning; import com.shatteredpixel.shatteredpixeldungeon.items.spells.BeaconOfReturning;
import com.shatteredpixel.shatteredpixeldungeon.items.spells.Spell; 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; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet;
public class ScrollHolder extends Bag { public class ScrollHolder extends Bag {
{ {
image = ItemSpriteSheet.HOLDER; image = ItemSpriteSheet.HOLDER;
size = 20;
} }
@Override @Override
public boolean grab( Item item ) { public boolean canHold( Item item ) {
return item instanceof Scroll || item instanceof Spell; if (item instanceof Scroll || item instanceof Spell){
return super.canHold(item);
} else {
return false;
}
} }
@Override @Override

View File

@ -24,6 +24,8 @@ package com.shatteredpixel.shatteredpixeldungeon.items.bags;
import com.shatteredpixel.shatteredpixeldungeon.items.Item; import com.shatteredpixel.shatteredpixeldungeon.items.Item;
import com.shatteredpixel.shatteredpixeldungeon.items.quest.GooBlob; import com.shatteredpixel.shatteredpixeldungeon.items.quest.GooBlob;
import com.shatteredpixel.shatteredpixeldungeon.items.quest.MetalShard; 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.items.stones.Runestone;
import com.shatteredpixel.shatteredpixeldungeon.plants.Plant; import com.shatteredpixel.shatteredpixeldungeon.plants.Plant;
import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet;
@ -32,14 +34,16 @@ public class VelvetPouch extends Bag {
{ {
image = ItemSpriteSheet.POUCH; image = ItemSpriteSheet.POUCH;
size = 20;
} }
@Override @Override
public boolean grab( Item item ) { public boolean canHold( Item item ) {
return item instanceof Plant.Seed || item instanceof Runestone if (item instanceof Plant.Seed || item instanceof Runestone
|| item instanceof GooBlob || item instanceof MetalShard; || item instanceof GooBlob || item instanceof MetalShard){
return super.canHold(item);
} else {
return false;
}
} }
@Override @Override

View File

@ -292,7 +292,7 @@ public class ShopRoom extends SpecialRoom {
//count up items in the main bag //count up items in the main bag
for (Item item : pack.backpack.items) { for (Item item : pack.backpack.items) {
for (Bag bag : bags.keySet()){ for (Bag bag : bags.keySet()){
if (bag.grab(item)){ if (bag.canHold(item)){
bags.put(bag, bags.get(bag)+1); bags.put(bag, bags.get(bag)+1);
} }
} }

View File

@ -109,7 +109,6 @@ public class WndBag extends WndTabbed {
private String title; private String title;
private int nCols; private int nCols;
private int nRows;
protected int count; protected int count;
protected int col; protected int col;
@ -135,15 +134,14 @@ public class WndBag extends WndTabbed {
lastBag = bag; lastBag = bag;
nCols = PixelScene.landscape() ? COLS_L : COLS_P; 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 slotsWidth = SLOT_WIDTH * nCols + SLOT_MARGIN * (nCols - 1);
int slotsHeight = SLOT_HEIGHT * nRows + SLOT_MARGIN * (nRows - 1);
placeTitle( bag, slotsWidth ); placeTitle( bag, slotsWidth );
placeItems( bag ); placeItems( bag );
int slotsHeight = SLOT_HEIGHT * row + SLOT_MARGIN * (row - 1);
resize( slotsWidth, slotsHeight + TITLE_HEIGHT ); resize( slotsWidth, slotsHeight + TITLE_HEIGHT );
Belongings stuff = Dungeon.hero.belongings; Belongings stuff = Dungeon.hero.belongings;
@ -229,13 +227,17 @@ public class WndBag extends WndTabbed {
} }
// Free Space // Free Space
while ((count - 4) < container.size) { while ((count - 4) < container.capacity()) {
placeItem( null ); placeItem( null );
} }
} }
protected void placeItem( final Item item ) { protected void placeItem( final Item item ) {
count++;
if (item instanceof Bag) return;
int x = col * (SLOT_WIDTH + SLOT_MARGIN); int x = col * (SLOT_WIDTH + SLOT_MARGIN);
int y = TITLE_HEIGHT + row * (SLOT_HEIGHT + SLOT_MARGIN); int y = TITLE_HEIGHT + row * (SLOT_HEIGHT + SLOT_MARGIN);
@ -246,7 +248,6 @@ public class WndBag extends WndTabbed {
row++; row++;
} }
count++;
} }
@Override @Override