Refactor stackable items codebase
This commit is contained in:
@@ -55,6 +55,42 @@ public class Player extends Creature {
|
||||
}
|
||||
|
||||
public boolean pushItemToEquipment(@NonNull Item item) {
|
||||
if (item instanceof Ammunition ammo) {
|
||||
if (ammo.getId().equals(getAmmunition().getId())) {
|
||||
getAmmunition().increase(ammo.getCount());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (item instanceof ItemStack stack) {
|
||||
return pushItemStackToEquipment(stack);
|
||||
}
|
||||
|
||||
return pushSingleItemToEquipment(item);
|
||||
}
|
||||
|
||||
private boolean pushItemStackToEquipment(@NonNull ItemStack items) {
|
||||
var availableSlot = -1;
|
||||
for (int i = 0; i < equipment.length; ++i) {
|
||||
if (equipment[i] instanceof ItemStack stack && stack.getId().equals(items.getId())) {
|
||||
stack.increase(items.getCount());
|
||||
return true;
|
||||
}
|
||||
|
||||
if (availableSlot == -1 && equipment[i] == null) {
|
||||
availableSlot = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (availableSlot > -1) {
|
||||
equipment[availableSlot] = items;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean pushSingleItemToEquipment(@NonNull Item item) {
|
||||
for (int i = 0; i < equipment.length; ++i) {
|
||||
if (equipment[i] == null) {
|
||||
equipment[i] = item;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.bartlomiejpluta.demo.world.item;
|
||||
|
||||
public interface ItemStack extends Item {
|
||||
String getId();
|
||||
|
||||
int getCount();
|
||||
|
||||
void setCount(int count);
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.bartlomiejpluta.demo.world.item;
|
||||
|
||||
import com.bartlomiejpluta.demo.entity.Creature;
|
||||
import lombok.NonNull;
|
||||
|
||||
public abstract class UseableStackableItem extends StackableItem implements Useable {
|
||||
|
||||
protected UseableStackableItem(@NonNull String id, int count) {
|
||||
super(id, count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void use(Creature creature) {
|
||||
if (--count == 0) {
|
||||
creature.removeItemFromEquipment(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,16 @@ package com.bartlomiejpluta.demo.world.potion;
|
||||
import DB.dao;
|
||||
import com.bartlomiejpluta.base.util.random.DiceRoller;
|
||||
import com.bartlomiejpluta.demo.entity.Creature;
|
||||
import com.bartlomiejpluta.demo.world.item.StackableItem;
|
||||
import com.bartlomiejpluta.demo.world.item.Useable;
|
||||
import com.bartlomiejpluta.demo.world.item.UseableStackableItem;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
|
||||
public class Medicament extends StackableItem implements Useable {
|
||||
import static java.lang.String.format;
|
||||
|
||||
public class Medicament extends UseableStackableItem implements Useable {
|
||||
@Getter
|
||||
private final String id;
|
||||
|
||||
@Getter
|
||||
private final String name;
|
||||
@@ -24,14 +28,12 @@ public class Medicament extends StackableItem implements Useable {
|
||||
super(template.getIcon(), count);
|
||||
this.name = template.getName();
|
||||
this.roller = DiceRoller.of(template.getHp());
|
||||
this.id = format("med:%s", template.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void use(Creature creature) {
|
||||
if(--count == 0) {
|
||||
creature.removeItemFromEquipment(this);
|
||||
}
|
||||
|
||||
super.use(creature);
|
||||
creature.heal(roller.roll());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.ToString;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
@ToString
|
||||
public class Ammunition extends StackableItem implements Useable {
|
||||
|
||||
@@ -26,7 +28,7 @@ public class Ammunition extends StackableItem implements Useable {
|
||||
public Ammunition(@NonNull DB.model.AmmunitionModel template, int count) {
|
||||
super(template.getIcon(), count);
|
||||
|
||||
this.id = template.getId();
|
||||
this.id = format("ammo:%s", template.getId());
|
||||
this.name = template.getName();
|
||||
this.appliesTo = template.getAppliesTo();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user