Add basic support for ammo
This commit is contained in:
@@ -1,5 +1,15 @@
|
||||
package com.bartlomiejpluta.demo.world.item;
|
||||
|
||||
public interface ItemStack extends Item {
|
||||
int count();
|
||||
int getCount();
|
||||
|
||||
void setCount(int count);
|
||||
|
||||
void decrease();
|
||||
|
||||
void increase();
|
||||
|
||||
void increase(int count);
|
||||
|
||||
void decrease(int count);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.bartlomiejpluta.demo.world.item;
|
||||
|
||||
import com.bartlomiejpluta.demo.entity.Creature;
|
||||
|
||||
public interface Useable {
|
||||
void use(Creature creature);
|
||||
|
||||
default String usageName() {
|
||||
return "Use";
|
||||
}
|
||||
}
|
||||
@@ -3,33 +3,76 @@ package com.bartlomiejpluta.demo.world.weapon;
|
||||
import com.bartlomiejpluta.base.api.context.ContextHolder;
|
||||
import com.bartlomiejpluta.base.api.icon.Icon;
|
||||
import com.bartlomiejpluta.base.lib.icon.IconDelegate;
|
||||
import com.bartlomiejpluta.demo.entity.Creature;
|
||||
import com.bartlomiejpluta.demo.world.item.ItemStack;
|
||||
import com.bartlomiejpluta.demo.world.item.Useable;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
public class Ammunition extends IconDelegate implements ItemStack {
|
||||
@ToString
|
||||
public class Ammunition extends IconDelegate implements ItemStack, Useable {
|
||||
|
||||
@Getter
|
||||
private final String id;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private int count;
|
||||
|
||||
@Getter
|
||||
private final String name;
|
||||
|
||||
@Getter
|
||||
private final String appliesTo;
|
||||
|
||||
public Ammunition(@NonNull String id, int count) {
|
||||
this(DB.dao.ammunition.find(id), count);
|
||||
}
|
||||
|
||||
public Ammunition(@NonNull DB.model.AmmunitionModel template, int count) {
|
||||
super(createIcon(template));
|
||||
this.id = template.getId();
|
||||
this.name = template.getName();
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int count() {
|
||||
return count;
|
||||
this.appliesTo = template.getAppliesTo();
|
||||
}
|
||||
|
||||
private static Icon createIcon(DB.model.AmmunitionModel template) {
|
||||
var icons = template.getIcon().split(",");
|
||||
return ContextHolder.INSTANCE.getContext().createIcon(A.iconsets.get(icons[0]).uid, Integer.parseInt(icons[1]), Integer.parseInt(icons[2]));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void decrease() {
|
||||
if (count > 0) {
|
||||
--count;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void increase() {
|
||||
++count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void increase(int count) {
|
||||
this.count += count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void decrease(int count) {
|
||||
this.count = Math.max(0, count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void use(Creature creature) {
|
||||
creature.setAmmunition(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String usageName() {
|
||||
return "Equip";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,6 +59,16 @@ public class MeleeWeapon extends IconDelegate implements Weapon {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void use(Creature creature) {
|
||||
creature.setWeapon(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String usageName() {
|
||||
return "Equip";
|
||||
}
|
||||
|
||||
private static Icon createIcon(DB.model.MeleeWeaponModel template) {
|
||||
var icons = template.getIcon().split(",");
|
||||
return ContextHolder.INSTANCE.getContext().createIcon(A.iconsets.get(icons[0]).uid, Integer.parseInt(icons[1]), Integer.parseInt(icons[2]));
|
||||
|
||||
@@ -28,6 +28,9 @@ public class RangedWeapon extends IconDelegate implements Weapon {
|
||||
@Getter
|
||||
private final String name;
|
||||
|
||||
@Getter
|
||||
private final String type;
|
||||
|
||||
@Getter
|
||||
private final DiceRoller dmgRoller;
|
||||
|
||||
@@ -46,6 +49,7 @@ public class RangedWeapon extends IconDelegate implements Weapon {
|
||||
|
||||
this.context = ContextHolder.INSTANCE.getContext();
|
||||
this.name = template.getName();
|
||||
this.type = template.getType();
|
||||
this.dmgRoller = DiceRoller.of(template.getDamage());
|
||||
this.rangeRoller = DiceRoller.of(template.getRange());
|
||||
this.cooldown = template.getCooldown();
|
||||
@@ -75,12 +79,28 @@ public class RangedWeapon extends IconDelegate implements Weapon {
|
||||
|
||||
@Override
|
||||
public boolean attack(Creature attacker) {
|
||||
var ammunition = attacker.getAmmunition();
|
||||
|
||||
if(ammunition == null || !ammunition.getAppliesTo().equals(type)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var direction = attacker.getFaceDirection();
|
||||
context.playSound(sound);
|
||||
animation.range(rangeRoller.roll()).direction(direction).rotation(direction.xAngle - 180).run(context, attacker.getLayer(), attacker);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void use(Creature creature) {
|
||||
creature.setWeapon(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String usageName() {
|
||||
return "Equip";
|
||||
}
|
||||
|
||||
private static Icon createIcon(DB.model.RangedWeaponModel template) {
|
||||
var icons = template.getIcon().split(",");
|
||||
return ContextHolder.INSTANCE.getContext().createIcon(A.iconsets.get(icons[0]).uid, Integer.parseInt(icons[1]), Integer.parseInt(icons[2]));
|
||||
|
||||
@@ -2,8 +2,9 @@ package com.bartlomiejpluta.demo.world.weapon;
|
||||
|
||||
import com.bartlomiejpluta.demo.entity.Creature;
|
||||
import com.bartlomiejpluta.demo.world.item.Item;
|
||||
import com.bartlomiejpluta.demo.world.item.Useable;
|
||||
|
||||
public interface Weapon extends Item {
|
||||
public interface Weapon extends Item, Useable {
|
||||
|
||||
int getCooldown();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user