From f33ad67a2f29076145916b90d18373f4cbb98725 Mon Sep 17 00:00:00 2001 From: HellaBased Date: Thu, 30 Oct 2025 06:21:41 +0100 Subject: [PATCH] Changed Equipment related classes. Created a new Job to be able to test certain Equipment functionalities. Modified Player by adding 4 new variables representing stats, added a list representing equipment slots to player, changed constructor of player so the equipment list starts with 4 slots, changed getHealth and getMana methods to account for equipments, added methods to get the added variables, added method to get equipments --- src/main/java/Entity/Player.java | 119 +++++++++++++++++++++- src/main/java/Item/AttributeModifier.java | 8 ++ src/main/java/Item/Equipment.java | 16 ++- src/main/java/Job/Knight.java | 8 ++ src/test/java/EquipmentTest.java | 80 +++++++++++++-- 5 files changed, 217 insertions(+), 14 deletions(-) create mode 100644 src/main/java/Job/Knight.java diff --git a/src/main/java/Entity/Player.java b/src/main/java/Entity/Player.java index c621511..b513741 100644 --- a/src/main/java/Entity/Player.java +++ b/src/main/java/Entity/Player.java @@ -7,26 +7,36 @@ import Combat.HasHealth; import Combat.HasMana; import Combat.Spell; import Inventory.Inventory; +import Item.Equipment; import Job.Job; import Job.HasJob; import Inventory.HasInventory; import Inventory.HasSpellBook; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class Player extends Entity implements Movable, Actor, HasInventory, HasSpellBook, HasJob, HasHealth, HasMana, HasConditions { protected int health; protected int mana; + protected int strength; + protected int magicStrength; + protected int defence; + protected int magicDefence; protected Job job; protected Position position = new Position(0,0); protected Inventory inventory= new Inventory(); protected List spells = new LinkedList<>(); protected List conditions = new LinkedList<>(); + protected List equipments = new ArrayList<>(); public Player(String name, Job job) { super(name); this.job = job; this.position = new Position(0,0); + for(int i= 0; i<4; i++){ + equipments.add(null); + } } public Player(String name) { @@ -53,12 +63,26 @@ public class Player extends Entity implements Movable, Actor, HasInventory, HasS this.job = job; } + public void equip(Equipment equipment) { + if(!equipment.getEquipable().equals(job)){ + throw new IllegalStateException("A player with this job can not equip this equipment"); + } + int i = equipment.getSlot(); + if(!(equipments.get(i)==null)){ + throw new IllegalStateException("Equipment slot already filled"); + } + equipments.set(i, equipment); + + } + public Inventory getInventory() { return inventory; } public List getSpellBook() {return spells;} + public List getEquipments() {return equipments;} + @Override public List getConditions() {return conditions;} @@ -89,13 +113,104 @@ public class Player extends Entity implements Movable, Actor, HasInventory, HasS } @Override - public int getHealth() {return health;} + public int getHealth() { + if(equipments.isEmpty()){ + return health; + } + int totalHealth = health; + for (Equipment e : equipments) { + if (e!=null) { + totalHealth+= e.getEquipmentType().getModifiers().getMaxHpMod(); + } + } + + return totalHealth; + + } @Override public void setMana(int mana) {this.mana = mana;} @Override - public int getMana() {return mana;} + public int getMana() { + if(equipments.isEmpty()){ + return mana; + } + + int totalMana = mana; + + for (Equipment e : equipments) { + if (e!=null) { + totalMana+= e.getEquipmentType().getModifiers().getMaxMpMod(); + } + } + + return totalMana; + } + + public int getStrength() { + if(equipments.isEmpty()){ + return strength; + } + + int totalStrength = strength; + + for (Equipment e : equipments) { + if (e!=null) { + totalStrength+= e.getEquipmentType().getModifiers().getStrMod(); + } + } + + return totalStrength; + } + + public int getMagicStrength() { + if(equipments.isEmpty()){ + return magicStrength; + } + + int totalMagicStrength = magicStrength; + + for (Equipment e : equipments) { + if(e!=null) { + totalMagicStrength+= e.getEquipmentType().getModifiers().getMagicStrMod(); + } + } + + return totalMagicStrength; + } + + public int getDefence() { + if(equipments.isEmpty()){ + return defence; + } + + int totalDefence = defence; + + for (Equipment e : equipments) { + if (e!=null) { + totalDefence+= e.getEquipmentType().getModifiers().getDefMod(); + } + } + + return totalDefence; + } + + public int getMagicDefence() { + if(equipments.isEmpty()){ + return magicDefence; + } + + int totalMagicDefence = magicDefence; + + for (Equipment e : equipments) { + if(e!=null) { + totalMagicDefence+= e.getEquipmentType().getModifiers().getMagicDefMod(); + } + } + + return totalMagicDefence; + } @Override public boolean isAlive() { diff --git a/src/main/java/Item/AttributeModifier.java b/src/main/java/Item/AttributeModifier.java index 6b9f308..7da0303 100644 --- a/src/main/java/Item/AttributeModifier.java +++ b/src/main/java/Item/AttributeModifier.java @@ -34,4 +34,12 @@ public class AttributeModifier { public int getMagicDefMod() { return magicDefMod; } + + + + @Override + public String toString() { + return "" + maxHpMod + maxMpMod + strMod + magicStrMod + defMod + magicDefMod; + } + } \ No newline at end of file diff --git a/src/main/java/Item/Equipment.java b/src/main/java/Item/Equipment.java index a9187ab..354fe73 100644 --- a/src/main/java/Item/Equipment.java +++ b/src/main/java/Item/Equipment.java @@ -1,15 +1,20 @@ package Item; + +import Job.Job; + public class Equipment implements Item { private String id; private String name; private EquipmentType equipmentType; + private Job equipable; private int slot; - public Equipment(String id, String name, EquipmentType equipmentType, int slot) { + public Equipment(String id, String name, EquipmentType equipmentType, Job equipable, int slot) { this.id = id; this.name = name; + this.equipable = equipable; this.equipmentType = equipmentType; this.slot = slot; } @@ -28,10 +33,19 @@ public class Equipment implements Item { return equipmentType; } + public Job getEquipable() { + return equipable; + } + public int getSlot() { return slot; } + + public boolean equals(Equipment e) { + return name.equals(e.getName()) && equipmentType.toString().equals(e.getEquipmentType().toString()); + } + @Override public String toString() { return name + equipmentType + slot; diff --git a/src/main/java/Job/Knight.java b/src/main/java/Job/Knight.java new file mode 100644 index 0000000..da838ef --- /dev/null +++ b/src/main/java/Job/Knight.java @@ -0,0 +1,8 @@ +package Job; + +public class Knight extends Job{ + public Knight() { + super("Knight"); + } + +} diff --git a/src/test/java/EquipmentTest.java b/src/test/java/EquipmentTest.java index 9f317c6..581251f 100644 --- a/src/test/java/EquipmentTest.java +++ b/src/test/java/EquipmentTest.java @@ -1,22 +1,31 @@ +import Entity.Player; +import Item.MageHat; +import Job.Wizard; +import Job.Knight; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; - import Item.Equipment; import Item.BodyArmour; import Item.AttributeModifier; +import static org.junit.jupiter.api.Assertions.*; + public class EquipmentTest { + private Knight knight = new Knight(); + private Wizard wizard = new Wizard(); + private Player defaultWizard(){ + return new Player("name", wizard); + } private Equipment defaultBodyArmour() { AttributeModifier a = new AttributeModifier(4,8,7,1,2,8); BodyArmour b = new BodyArmour("Body Armour", a); - return new Equipment("5", "Thornmail", b, 2); + return new Equipment("5", "Thornmail", b, knight, 2); } private Equipment defaultMageHat() { AttributeModifier a = new AttributeModifier(4,8,2,10,2,8); - BodyArmour b = new BodyArmour("Mage Hat", a); - return new Equipment("6", "Deathmage Cap", b, 1); + MageHat b = new MageHat("Mage Hat", a); + return new Equipment("6", "Deathmage Cap", b, wizard, 1); } @@ -29,7 +38,7 @@ public class EquipmentTest { @Test void setTypeOnCreation() { var e = defaultBodyArmour(); - assertEquals("helmet", e.getEquipmentType().getName(), "Equipment type should have been set"); + assertEquals("Body Armour", e.getEquipmentType().getName(), "Equipment type should have been set"); } @Test @@ -41,19 +50,19 @@ public class EquipmentTest { @Test void setMaxHpModOnCreation() { var e = defaultBodyArmour(); - assertEquals(1, e.getEquipmentType().getModifiers().getMaxHpMod(), "Equipment max hp modifier should have been set"); + assertEquals(4, e.getEquipmentType().getModifiers().getMaxHpMod(), "Equipment max hp modifier should have been set"); } @Test void setMaxMpModOnCreation() { var e = defaultBodyArmour(); - assertEquals(1, e.getEquipmentType().getModifiers().getMaxMpMod(), "Equipment max mp modifier should have been set"); + assertEquals(8, e.getEquipmentType().getModifiers().getMaxMpMod(), "Equipment max mp modifier should have been set"); } @Test void setStrModOnCreation() { var e = defaultBodyArmour(); - assertEquals(1, e.getEquipmentType().getModifiers().getStrMod(), "Equipment strength modifier should have been set"); + assertEquals(7, e.getEquipmentType().getModifiers().getStrMod(), "Equipment strength modifier should have been set"); } @Test @@ -65,12 +74,61 @@ public class EquipmentTest { @Test void setDefModOnCreation() { var e = defaultBodyArmour(); - assertEquals(1, e.getEquipmentType().getModifiers().getDefMod(), "Equipment defence modifier should have been set"); + assertEquals(4, e.getEquipmentType().getModifiers().getDefMod(), "Equipment defence modifier should have been set"); } @Test void setMagicDefModOnCreation() { var e = defaultBodyArmour(); - assertEquals(1, e.getEquipmentType().getModifiers().getMagicDefMod(), "Equipment magic defence modifier should have been set"); + assertEquals(16, e.getEquipmentType().getModifiers().getMagicDefMod(), "Equipment magic defence modifier should have been set"); } + + @Test + void canEquipWithRightJob() { + var p = defaultWizard(); + p.equip(defaultMageHat()); + assertTrue(p.getEquipments().get(defaultMageHat().getSlot()).equals(defaultMageHat()), "Wizard can equip wizard hat"); + } + + @Test + void canNotEquipWithWrongJob() { + var p = defaultWizard(); + IllegalStateException exception = assertThrows( + IllegalStateException.class, + () -> p.equip(defaultBodyArmour()) + ); + assertTrue(exception.getMessage().contains("A player with this job can not equip this equipment")); + } + + @Test + void canNotEquipAlreadyEquippedSlot() { + AttributeModifier a = new AttributeModifier(2,2,2,2,2,8); + MageHat b = new MageHat("Mage Hat", a); + Equipment e = new Equipment("7", "Mage hat2", b, wizard, 1); + var p = defaultWizard(); + p.equip(defaultMageHat()); + IllegalStateException exception = assertThrows( + IllegalStateException.class, + () -> p.equip(e) + ); + assertTrue(exception.getMessage().contains("Equipment slot already filled")); + + } + + @Test + void equipmentModifiesPlayerStats() { + var p = defaultWizard(); + p.setHealth(100); + p.setMana(30); + p.equip(defaultMageHat()); + assertEquals(104, p.getHealth(), "Equipment modifies health"); + assertEquals(38, p.getMana(), "Equipment modifies mana"); + assertEquals(2, p.getStrength(), "Equipment modifies strength"); + assertEquals(20, p.getMagicStrength(), "Equipment modifies magic strength"); + assertEquals(2, p.getDefence(), "Equipment modifies defence"); + assertEquals(16, p.getMagicDefence() , "Equipment modifies magic defence"); + } + + + } -- 2.39.5