2020-02-14 01:01:38 +01:00

146 lines
2.8 KiB
Java

package inf101.v18.rogue101.items;
import inf101.v18.rogue101.game.IGame;
import inf101.v18.rogue101.objects.IItem;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Backpack implements IContainer {
/**
* A list containing everything in the backpack.
*/
private final List<IItem> content = new ArrayList<>();
/**
* The maximum amount of items allowed in a single backpack.
*/
private final int MAX_SIZE = 5;
@Override
public int getCurrentHealth() {
return 0;
}
@Override
public int getDefence() {
return 0;
}
@Override
public int getMaxHealth() {
return 0;
}
@Override
public String getName() {
return "Backpack";
}
@Override
public int getSize() {
return 20;
}
@Override
public String getSymbol() {
return "B";
}
@Override
public int handleDamage(IGame game, IItem source, int amount) {
return 0;
}
/**
* Retrieves the current size of the Backpack.
*
* @return The size
*/
public int size() {
return content.size();
}
/**
* Checks if the Backpack is empty
*
* @return True if empty. False otherwise
*/
public boolean isEmpty() {
return content.isEmpty();
}
/**
* Tries to add an item to the Backpack
*
* @param item The item to add
* @return True if the item was added. False if the backpack is full
*/
public boolean add(IItem item) {
if (size() < MAX_SIZE) {
content.add(item);
return true;
} else {
return false;
}
}
@Override
public void remove(int i) {
content.remove(i);
}
/**
* Removes item from the backpack.
*
* @param item The item to remove
*/
public void remove(IItem item) {
content.remove(item);
}
/**
* Gets a T at index i,
*
* @param i The index of an element
* @return An object of type T
*/
public IItem get(int i) {
return content.get(i);
}
@Override
public boolean addItem(IItem item) {
if (content.size() < MAX_SIZE) {
content.add(item);
return true;
} else {
return false;
}
}
/**
* Gets the content List for direct manipulation.
*
* @return A list of T
*/
public List<IItem> getContent() {
return Collections.unmodifiableList(content);
}
@Override
public boolean hasSpace() {
return size() < MAX_SIZE;
}
@Override
public IItem getFirst(Class<?> clazz) {
for (IItem item : content) {
if (clazz.isInstance(item)) {
return item;
}
}
return null;
}
}