Rogue: Savage Rats, a retro-themed dungeon crawler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rogue-savage-rats/src/mightypork/rogue/world/item/Item.java

161 lines
2.8 KiB

package mightypork.rogue.world.item;
import java.io.IOException;
import mightypork.gamecore.logging.Log;
10 years ago
import mightypork.gamecore.util.annot.DefaultImpl;
import mightypork.gamecore.util.ion.IonInput;
10 years ago
import mightypork.gamecore.util.ion.IonObjBlob;
import mightypork.gamecore.util.ion.IonOutput;
10 years ago
import mightypork.gamecore.util.math.constraints.rect.Rect;
10 years ago
public abstract class Item implements IonObjBlob {
10 years ago
private final ItemModel model;
private ItemRenderer renderer;
private int amount = 1;
public Item(ItemModel model)
{
10 years ago
this.model = model;
}
10 years ago
public final void render(Rect rect)
{
10 years ago
if (renderer == null) {
renderer = makeRenderer();
}
renderer.render(rect);
}
10 years ago
protected abstract ItemRenderer makeRenderer();
@Override
10 years ago
@DefaultImpl
public void save(IonOutput out) throws IOException
{
out.writeIntShort(amount);
}
@Override
10 years ago
@DefaultImpl
public void load(IonInput in) throws IOException
{
amount = in.readIntShort();
}
10 years ago
public final ItemModel getModel()
{
10 years ago
return model;
}
@DefaultImpl
protected int getMaxStackSize()
{
return isStackable() ? 65535 : 1;
}
public boolean canStackWith(Item other)
{
return (getModel().id == other.getModel().id) && isStackable();
}
public abstract boolean isStackable();
/**
* Add another item to this item
*
* @param added added item
* @return if items are compatible and the added item was completely moved
* to this item, returns true and the added item should be
* discarded.
*/
public boolean addItem(Item added)
{
if (!canStackWith(added)) return false;
if (added.isEmpty()) return true;
final int room = getMaxStackSize() - this.amount;
final int avail = added.amount;
final int moved = Math.min(room, avail);
this.amount += moved;
added.amount -= moved;
return added.isEmpty();
}
/**
* @return item amount in the stack
*/
public int getAmount()
{
return Math.max(0, amount);
}
/**
* Consume one item.
*
* @return true if the item is fully consumed and should be removed.
*/
public boolean consume()
{
if (isEmpty()) throw new RuntimeException("Item is empty, cannot consume.");
amount--;
return isEmpty();
}
public boolean isEmpty()
{
return getAmount() == 0;
}
public Item split(int removed)
{
if (isEmpty()) throw new RuntimeException("Item is empty, cannot split.");
final int realRemoved = Math.min(removed, amount);
final Item newItm = model.createItem();
newItm.amount = realRemoved;
this.amount -= realRemoved;
return newItm;
}
public abstract int getAttackPoints();
public abstract int getFoodPoints();
public abstract ItemType getType();
@Override
public String toString()
{
return Log.str(getClass()) + " x " + getAmount();
}
}