implement Flask class

This commit is contained in:
2026-05-10 09:04:13 +03:30
parent d509a8b999
commit 3ab38fddce
6 changed files with 64 additions and 17 deletions
@@ -10,6 +10,7 @@ public class Assassin extends Player {
public Assassin() { public Assassin() {
super("ASSASSIN",100,100, new Dagger(), new AssassinArmor()); super("ASSASSIN",100,100, new Dagger(), new AssassinArmor());
setDefendRate(.7); setDefendRate(.7);
setFlask(10,3,false);
} }
@Override @Override
@@ -10,6 +10,7 @@ public class Knight extends Player {
public Knight() { public Knight() {
super("Knight",100,90, new Sword(), new KnightArmor()); super("Knight",100,90, new Sword(), new KnightArmor());
setDefendRate(.9); setDefendRate(.9);
setFlask(20,3,true);
} }
@Override @Override
@@ -4,6 +4,8 @@ import org.project.entity.Entity;
import org.project.entity.enemies.Goblin; import org.project.entity.enemies.Goblin;
import org.project.entity.enemies.Skeleton; import org.project.entity.enemies.Skeleton;
import org.project.item.armors.Armor; import org.project.item.armors.Armor;
import org.project.item.consumables.Consumable;
import org.project.item.consumables.Flask;
import org.project.item.weapons.Weapon; import org.project.item.weapons.Weapon;
@@ -23,6 +25,7 @@ public abstract class Player implements Entity, CombatOptions {
private boolean hasSkeletonKey = false; private boolean hasSkeletonKey = false;
private boolean hasVampireKey = false; private boolean hasVampireKey = false;
private boolean successfulAction = true; private boolean successfulAction = true;
private Consumable flask;
public Player(String name, int hp, int mp, Weapon weapon, Armor armor) { public Player(String name, int hp, int mp, Weapon weapon, Armor armor) {
this.name = name; this.name = name;
@@ -205,4 +208,12 @@ public abstract class Player implements Entity, CombatOptions {
return successfulAction; return successfulAction;
} }
public void setFlask(int healAmount, int quantity, boolean isMana) {
flask = new Flask(healAmount, quantity, isMana);
}
public Consumable getFlask() {
return flask;
}
} }
@@ -9,6 +9,7 @@ public class Wizard extends Player{
public Wizard() { public Wizard() {
super("Wizard",90,90,new Bow(),new WizardArmor()); super("Wizard",90,90,new Bow(),new WizardArmor());
setDefendRate(.6); setDefendRate(.6);
setFlask(30,3,true);
} }
@Override @Override
@@ -1,8 +1,28 @@
package org.project.item.consumables; package org.project.item.consumables;
// TODO: UPDATE IMPLEMENTATION import org.project.entity.Entity;
public abstract class Consumable { import org.project.item.Item;
/*
TODO: ADD OTHER REQUIRED AND BONUS METHODS public abstract class Consumable implements Item {
*/ private String name;
} private int quantity;
public Consumable(String name, int quantity) {
this.name = name;
this.quantity = quantity;
}
public abstract void use(Entity target); // distinct from Item.use()
public boolean hasCharges() { return quantity > 0; }
protected void consume() { if (quantity > 0) quantity--; }
public int getQuantity() { return quantity; }
@Override
public String getName() { return name; }
@Override
public void use() { System.out.println("Use " + name + " on a target."); }
}
@@ -2,15 +2,28 @@ package org.project.item.consumables;
import org.project.entity.Entity; import org.project.entity.Entity;
// TODO: UPDATE IMPLEMENTATION public class Flask extends Consumable {
public class Flask { private int healAmount;
/* private boolean isMana;
THIS IS AN EXAMPLE OF A CONSUMABLE DESIGN.
*/
// // TODO: UPDATE USE METHOD public Flask(int healAmount, int quantity, boolean isMana) {
// @Override super(isMana ? "Mana Flask" : "Health Flask", quantity);
// public void use(Entity target) { this.healAmount = healAmount;
// target.heal(target.getMaxHP() / 10); }
// }
} @Override
public void use(Entity target) {
if (!hasCharges()) {
System.out.println("Flask is empty!");
return;
}
if (isMana) {
target.setMP(target.getMP() + healAmount);
System.out.println(target.getClass().getSimpleName()
+ " restored " + healAmount + " MP!");
} else {
target.heal(healAmount);
}
consume();
}
}