Add basic support for ammo
This commit is contained in:
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
Binary file not shown.
@@ -40,8 +40,7 @@ public class WeaponBasedAI implements AI {
|
|||||||
@Override
|
@Override
|
||||||
public void nextActivity(ObjectLayer layer, float dt) {
|
public void nextActivity(ObjectLayer layer, float dt) {
|
||||||
var lastAttacker = enemy.getLastAttacker();
|
var lastAttacker = enemy.getLastAttacker();
|
||||||
if (lastAttacker != null && lastAttacker instanceof Creature) {
|
if (lastAttacker instanceof Creature attacker) {
|
||||||
var attacker = (Creature) lastAttacker;
|
|
||||||
if (attacker.isAlive()) {
|
if (attacker.isAlive()) {
|
||||||
runawayAI.setDanger(attacker);
|
runawayAI.setDanger(attacker);
|
||||||
meleeAI.setTarget(attacker);
|
meleeAI.setTarget(attacker);
|
||||||
@@ -61,7 +60,7 @@ public class WeaponBasedAI implements AI {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rangedWeapon == null || enemy.manhattanDistance(target) == 1) {
|
if (rangedWeapon == null || enemy.manhattanDistance(target) == 1 || enemy.getAmmunition() == null) {
|
||||||
enemy.setWeapon(meleeWeapon);
|
enemy.setWeapon(meleeWeapon);
|
||||||
meleeAI.nextActivity(layer, dt);
|
meleeAI.nextActivity(layer, dt);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package com.bartlomiejpluta.demo.entity;
|
|||||||
|
|
||||||
import com.bartlomiejpluta.base.api.character.Character;
|
import com.bartlomiejpluta.base.api.character.Character;
|
||||||
import com.bartlomiejpluta.demo.world.item.Item;
|
import com.bartlomiejpluta.demo.world.item.Item;
|
||||||
|
import com.bartlomiejpluta.demo.world.weapon.Ammunition;
|
||||||
|
import com.bartlomiejpluta.demo.world.weapon.RangedWeapon;
|
||||||
import com.bartlomiejpluta.demo.world.weapon.Weapon;
|
import com.bartlomiejpluta.demo.world.weapon.Weapon;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
@@ -30,6 +32,10 @@ public abstract class Creature extends NamedCharacter {
|
|||||||
@Setter
|
@Setter
|
||||||
private Weapon weapon;
|
private Weapon weapon;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
private Ammunition ammunition;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private NamedCharacter lastAttacker;
|
private NamedCharacter lastAttacker;
|
||||||
|
|
||||||
@@ -44,6 +50,14 @@ public abstract class Creature extends NamedCharacter {
|
|||||||
|
|
||||||
if (attackCooldown >= weapon.getCooldown()) {
|
if (attackCooldown >= weapon.getCooldown()) {
|
||||||
if (weapon.attack(this)) {
|
if (weapon.attack(this)) {
|
||||||
|
if(weapon instanceof RangedWeapon) {
|
||||||
|
ammunition.decrease();
|
||||||
|
|
||||||
|
if(ammunition.getCount() == 0) {
|
||||||
|
ammunition = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
attackCooldown = 0;
|
attackCooldown = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -62,6 +76,11 @@ public abstract class Creature extends NamedCharacter {
|
|||||||
public void useEquipmentItem(Item item) {
|
public void useEquipmentItem(Item item) {
|
||||||
if (item instanceof Weapon weapon) {
|
if (item instanceof Weapon weapon) {
|
||||||
setWeapon(weapon);
|
setWeapon(weapon);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item instanceof Ammunition ammunition) {
|
||||||
|
setAmmunition(ammunition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.bartlomiejpluta.demo.entity;
|
|||||||
|
|
||||||
import com.bartlomiejpluta.base.api.character.Character;
|
import com.bartlomiejpluta.base.api.character.Character;
|
||||||
import com.bartlomiejpluta.demo.world.item.Item;
|
import com.bartlomiejpluta.demo.world.item.Item;
|
||||||
|
import com.bartlomiejpluta.demo.world.weapon.Ammunition;
|
||||||
import com.bartlomiejpluta.demo.world.weapon.Weapon;
|
import com.bartlomiejpluta.demo.world.weapon.Weapon;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import org.joml.Vector2i;
|
import org.joml.Vector2i;
|
||||||
@@ -63,9 +64,49 @@ public class Player extends Creature {
|
|||||||
item.setCoordinates(getCoordinates());
|
item.setCoordinates(getCoordinates());
|
||||||
getLayer().addEntity(item);
|
getLayer().addEntity(item);
|
||||||
|
|
||||||
if(item == getWeapon()) {
|
if (item == getWeapon()) {
|
||||||
setWeapon(null);
|
setWeapon(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (item == getAmmunition()) {
|
||||||
|
setAmmunition(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setWeapon(Weapon weapon) {
|
||||||
|
var currentWeapon = getWeapon();
|
||||||
|
|
||||||
|
if (weapon != null) {
|
||||||
|
removeItemFromEquipment(weapon);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentWeapon != null) {
|
||||||
|
pushItemToEquipment(currentWeapon);
|
||||||
|
}
|
||||||
|
|
||||||
|
super.setWeapon(weapon);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setAmmunition(Ammunition ammunition) {
|
||||||
|
var currentAmmo = getAmmunition();
|
||||||
|
|
||||||
|
if (currentAmmo != null && currentAmmo.getId().equals(ammunition.getId())) {
|
||||||
|
currentAmmo.increase(ammunition.getCount());
|
||||||
|
removeItemFromEquipment(ammunition);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ammunition != null) {
|
||||||
|
removeItemFromEquipment(ammunition);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentAmmo != null) {
|
||||||
|
pushItemToEquipment(currentAmmo);
|
||||||
|
}
|
||||||
|
|
||||||
|
super.setAmmunition(ammunition);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ import com.bartlomiejpluta.base.lib.gui.VOptionChoice;
|
|||||||
import com.bartlomiejpluta.demo.entity.Player;
|
import com.bartlomiejpluta.demo.entity.Player;
|
||||||
import com.bartlomiejpluta.demo.runner.DemoRunner;
|
import com.bartlomiejpluta.demo.runner.DemoRunner;
|
||||||
import com.bartlomiejpluta.demo.world.item.Item;
|
import com.bartlomiejpluta.demo.world.item.Item;
|
||||||
|
import com.bartlomiejpluta.demo.world.item.Useable;
|
||||||
import com.bartlomiejpluta.demo.world.weapon.MeleeWeapon;
|
import com.bartlomiejpluta.demo.world.weapon.MeleeWeapon;
|
||||||
import com.bartlomiejpluta.demo.world.weapon.RangedWeapon;
|
import com.bartlomiejpluta.demo.world.weapon.RangedWeapon;
|
||||||
import com.bartlomiejpluta.demo.world.weapon.Weapon;
|
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
@@ -31,6 +31,12 @@ public class EquipmentWindow extends DecoratedWindow {
|
|||||||
@Ref("eq")
|
@Ref("eq")
|
||||||
private VGridOptionChoice eqGrid;
|
private VGridOptionChoice eqGrid;
|
||||||
|
|
||||||
|
@Ref("weapon")
|
||||||
|
private ItemIconView weapon;
|
||||||
|
|
||||||
|
@Ref("ammo")
|
||||||
|
private ItemIconView ammo;
|
||||||
|
|
||||||
@Ref("item-name")
|
@Ref("item-name")
|
||||||
private Label nameLbl;
|
private Label nameLbl;
|
||||||
|
|
||||||
@@ -55,6 +61,13 @@ public class EquipmentWindow extends DecoratedWindow {
|
|||||||
cancelBtn.setAction(manager::close);
|
cancelBtn.setAction(manager::close);
|
||||||
eqGrid.setOnSelect(this::updateItemDetails);
|
eqGrid.setOnSelect(this::updateItemDetails);
|
||||||
|
|
||||||
|
updateEquipment();
|
||||||
|
|
||||||
|
eqGrid.select(0, 0);
|
||||||
|
eqGrid.focus();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateEquipment() {
|
||||||
var i = 0;
|
var i = 0;
|
||||||
for (var child : eqGrid.getChildren()) {
|
for (var child : eqGrid.getChildren()) {
|
||||||
var slot = (ItemIconView) child;
|
var slot = (ItemIconView) child;
|
||||||
@@ -62,8 +75,8 @@ public class EquipmentWindow extends DecoratedWindow {
|
|||||||
slot.setAction(handleItem(slot));
|
slot.setAction(handleItem(slot));
|
||||||
}
|
}
|
||||||
|
|
||||||
eqGrid.select(0, 0);
|
weapon.setItem(player.getWeapon());
|
||||||
eqGrid.focus();
|
ammo.setItem(player.getAmmunition());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Consumer<Item> handleItem(ItemIconView slot) {
|
private Consumer<Item> handleItem(ItemIconView slot) {
|
||||||
@@ -74,10 +87,13 @@ public class EquipmentWindow extends DecoratedWindow {
|
|||||||
|
|
||||||
manager.open(eqItemMenuWindow);
|
manager.open(eqItemMenuWindow);
|
||||||
|
|
||||||
useBtn.setAction(() -> {
|
if (item instanceof Useable useable) {
|
||||||
player.useEquipmentItem(item);
|
useBtn.setAction(() -> {
|
||||||
manager.close();
|
useable.use(player);
|
||||||
});
|
updateEquipment();
|
||||||
|
manager.close();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
dropBtn.setAction(() -> {
|
dropBtn.setAction(() -> {
|
||||||
player.dropItemFromEquipment(item);
|
player.dropItemFromEquipment(item);
|
||||||
@@ -110,8 +126,8 @@ public class EquipmentWindow extends DecoratedWindow {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getButtonTitle(Item item) {
|
private String getButtonTitle(Item item) {
|
||||||
if (item instanceof Weapon) {
|
if (item instanceof Useable useable) {
|
||||||
return "Equip";
|
return useable.usageName();
|
||||||
}
|
}
|
||||||
|
|
||||||
return "Use";
|
return "Use";
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ public class ItemIconView extends IconView {
|
|||||||
gui.beginPath();
|
gui.beginPath();
|
||||||
gui.setFontFace(fonts.roboto_regular.uid);
|
gui.setFontFace(fonts.roboto_regular.uid);
|
||||||
gui.setFontSize(17);
|
gui.setFontSize(17);
|
||||||
gui.putText(x + 15, y + 5, String.valueOf(stack.count()));
|
gui.putText(x + 15, y + 5, String.valueOf(stack.getCount()));
|
||||||
gui.setFillColor(textColor);
|
gui.setFillColor(textColor);
|
||||||
gui.fill();
|
gui.fill();
|
||||||
gui.closePath();
|
gui.closePath();
|
||||||
|
|||||||
@@ -1,5 +1,15 @@
|
|||||||
package com.bartlomiejpluta.demo.world.item;
|
package com.bartlomiejpluta.demo.world.item;
|
||||||
|
|
||||||
public interface ItemStack extends 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.context.ContextHolder;
|
||||||
import com.bartlomiejpluta.base.api.icon.Icon;
|
import com.bartlomiejpluta.base.api.icon.Icon;
|
||||||
import com.bartlomiejpluta.base.lib.icon.IconDelegate;
|
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.ItemStack;
|
||||||
|
import com.bartlomiejpluta.demo.world.item.Useable;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NonNull;
|
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;
|
private int count;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private final String appliesTo;
|
||||||
|
|
||||||
public Ammunition(@NonNull String id, int count) {
|
public Ammunition(@NonNull String id, int count) {
|
||||||
this(DB.dao.ammunition.find(id), count);
|
this(DB.dao.ammunition.find(id), count);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Ammunition(@NonNull DB.model.AmmunitionModel template, int count) {
|
public Ammunition(@NonNull DB.model.AmmunitionModel template, int count) {
|
||||||
super(createIcon(template));
|
super(createIcon(template));
|
||||||
|
this.id = template.getId();
|
||||||
this.name = template.getName();
|
this.name = template.getName();
|
||||||
this.count = count;
|
this.count = count;
|
||||||
}
|
this.appliesTo = template.getAppliesTo();
|
||||||
|
|
||||||
@Override
|
|
||||||
public int count() {
|
|
||||||
return count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Icon createIcon(DB.model.AmmunitionModel template) {
|
private static Icon createIcon(DB.model.AmmunitionModel template) {
|
||||||
var icons = template.getIcon().split(",");
|
var icons = template.getIcon().split(",");
|
||||||
return ContextHolder.INSTANCE.getContext().createIcon(A.iconsets.get(icons[0]).uid, Integer.parseInt(icons[1]), Integer.parseInt(icons[2]));
|
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;
|
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) {
|
private static Icon createIcon(DB.model.MeleeWeaponModel template) {
|
||||||
var icons = template.getIcon().split(",");
|
var icons = template.getIcon().split(",");
|
||||||
return ContextHolder.INSTANCE.getContext().createIcon(A.iconsets.get(icons[0]).uid, Integer.parseInt(icons[1]), Integer.parseInt(icons[2]));
|
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
|
@Getter
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private final String type;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final DiceRoller dmgRoller;
|
private final DiceRoller dmgRoller;
|
||||||
|
|
||||||
@@ -46,6 +49,7 @@ public class RangedWeapon extends IconDelegate implements Weapon {
|
|||||||
|
|
||||||
this.context = ContextHolder.INSTANCE.getContext();
|
this.context = ContextHolder.INSTANCE.getContext();
|
||||||
this.name = template.getName();
|
this.name = template.getName();
|
||||||
|
this.type = template.getType();
|
||||||
this.dmgRoller = DiceRoller.of(template.getDamage());
|
this.dmgRoller = DiceRoller.of(template.getDamage());
|
||||||
this.rangeRoller = DiceRoller.of(template.getRange());
|
this.rangeRoller = DiceRoller.of(template.getRange());
|
||||||
this.cooldown = template.getCooldown();
|
this.cooldown = template.getCooldown();
|
||||||
@@ -75,12 +79,28 @@ public class RangedWeapon extends IconDelegate implements Weapon {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean attack(Creature attacker) {
|
public boolean attack(Creature attacker) {
|
||||||
|
var ammunition = attacker.getAmmunition();
|
||||||
|
|
||||||
|
if(ammunition == null || !ammunition.getAppliesTo().equals(type)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
var direction = attacker.getFaceDirection();
|
var direction = attacker.getFaceDirection();
|
||||||
context.playSound(sound);
|
context.playSound(sound);
|
||||||
animation.range(rangeRoller.roll()).direction(direction).rotation(direction.xAngle - 180).run(context, attacker.getLayer(), attacker);
|
animation.range(rangeRoller.roll()).direction(direction).rotation(direction.xAngle - 180).run(context, attacker.getLayer(), attacker);
|
||||||
return true;
|
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) {
|
private static Icon createIcon(DB.model.RangedWeaponModel template) {
|
||||||
var icons = template.getIcon().split(",");
|
var icons = template.getIcon().split(",");
|
||||||
return ContextHolder.INSTANCE.getContext().createIcon(A.iconsets.get(icons[0]).uid, Integer.parseInt(icons[1]), Integer.parseInt(icons[2]));
|
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.entity.Creature;
|
||||||
import com.bartlomiejpluta.demo.world.item.Item;
|
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();
|
int getCooldown();
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,11 @@
|
|||||||
</base:Label>
|
</base:Label>
|
||||||
|
|
||||||
<base:HLayout>
|
<base:HLayout>
|
||||||
|
<base:VGridLayout columns="2" padding="20f" width="250f">
|
||||||
|
<demo:ItemIconView ref="weapon" margin="5f"/>
|
||||||
|
<demo:ItemIconView ref="ammo" margin="5f"/>
|
||||||
|
</base:VGridLayout>
|
||||||
|
|
||||||
<base:VGridOptionChoice ref="eq" columns="4" width="auto" height="auto">
|
<base:VGridOptionChoice ref="eq" columns="4" width="auto" height="auto">
|
||||||
<demo:ItemIconView margin="5f"/>
|
<demo:ItemIconView margin="5f"/>
|
||||||
<demo:ItemIconView margin="5f"/>
|
<demo:ItemIconView margin="5f"/>
|
||||||
|
|||||||
Reference in New Issue
Block a user