Changed Equipment related classes, Player class and added tests for new functionality #16

Merged
viud3133 merged 2 commits from equipments into main 2025-10-30 06:39:54 +01:00
5 changed files with 211 additions and 8 deletions

View File

@ -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<Spell> spells = new LinkedList<>();
protected List<String> conditions = new LinkedList<>();
protected List<Equipment> 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<Spell> getSpellBook() {return spells;}
public List<Equipment> getEquipments() {return equipments;}
@Override
public List<String> 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() {

View File

@ -34,4 +34,12 @@ public class AttributeModifier {
public int getMagicDefMod() {
return magicDefMod;
}
@Override
public String toString() {
return "" + maxHpMod + maxMpMod + strMod + magicStrMod + defMod + magicDefMod;
}
}

View File

@ -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;
}
@ -33,10 +38,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;

View File

@ -0,0 +1,8 @@
package Job;
public class Knight extends Job{
public Knight() {
super("Knight");
}
}

View File

@ -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);
}
@ -73,4 +82,53 @@ public class EquipmentTest {
var e = defaultBodyArmour();
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");
}
}