Changed Equipment related classes, Player class and added tests for new functionality #16
@ -7,26 +7,36 @@ import Combat.HasHealth;
|
|||||||
import Combat.HasMana;
|
import Combat.HasMana;
|
||||||
import Combat.Spell;
|
import Combat.Spell;
|
||||||
import Inventory.Inventory;
|
import Inventory.Inventory;
|
||||||
|
import Item.Equipment;
|
||||||
import Job.Job;
|
import Job.Job;
|
||||||
import Job.HasJob;
|
import Job.HasJob;
|
||||||
import Inventory.HasInventory;
|
import Inventory.HasInventory;
|
||||||
import Inventory.HasSpellBook;
|
import Inventory.HasSpellBook;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Player extends Entity implements Movable, Actor, HasInventory, HasSpellBook, HasJob, HasHealth, HasMana, HasConditions {
|
public class Player extends Entity implements Movable, Actor, HasInventory, HasSpellBook, HasJob, HasHealth, HasMana, HasConditions {
|
||||||
protected int health;
|
protected int health;
|
||||||
protected int mana;
|
protected int mana;
|
||||||
|
protected int strength;
|
||||||
|
protected int magicStrength;
|
||||||
|
protected int defence;
|
||||||
|
protected int magicDefence;
|
||||||
protected Job job;
|
protected Job job;
|
||||||
protected Position position = new Position(0,0);
|
protected Position position = new Position(0,0);
|
||||||
protected Inventory inventory= new Inventory();
|
protected Inventory inventory= new Inventory();
|
||||||
protected List<Spell> spells = new LinkedList<>();
|
protected List<Spell> spells = new LinkedList<>();
|
||||||
protected List<String> conditions = new LinkedList<>();
|
protected List<String> conditions = new LinkedList<>();
|
||||||
|
protected List<Equipment> equipments = new ArrayList<>();
|
||||||
public Player(String name, Job job) {
|
public Player(String name, Job job) {
|
||||||
super(name);
|
super(name);
|
||||||
this.job = job;
|
this.job = job;
|
||||||
this.position = new Position(0,0);
|
this.position = new Position(0,0);
|
||||||
|
for(int i= 0; i<4; i++){
|
||||||
|
equipments.add(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player(String name) {
|
public Player(String name) {
|
||||||
@ -53,12 +63,26 @@ public class Player extends Entity implements Movable, Actor, HasInventory, HasS
|
|||||||
this.job = job;
|
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() {
|
public Inventory getInventory() {
|
||||||
return inventory;
|
return inventory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Spell> getSpellBook() {return spells;}
|
public List<Spell> getSpellBook() {return spells;}
|
||||||
|
|
||||||
|
public List<Equipment> getEquipments() {return equipments;}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getConditions() {return conditions;}
|
public List<String> getConditions() {return conditions;}
|
||||||
|
|
||||||
@ -89,13 +113,104 @@ public class Player extends Entity implements Movable, Actor, HasInventory, HasS
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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
|
@Override
|
||||||
public void setMana(int mana) {this.mana = mana;}
|
public void setMana(int mana) {this.mana = mana;}
|
||||||
|
|
||||||
@Override
|
@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
|
@Override
|
||||||
public boolean isAlive() {
|
public boolean isAlive() {
|
||||||
|
|||||||
@ -34,4 +34,12 @@ public class AttributeModifier {
|
|||||||
public int getMagicDefMod() {
|
public int getMagicDefMod() {
|
||||||
return magicDefMod;
|
return magicDefMod;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "" + maxHpMod + maxMpMod + strMod + magicStrMod + defMod + magicDefMod;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,15 +1,20 @@
|
|||||||
package Item;
|
package Item;
|
||||||
|
|
||||||
|
|
||||||
|
import Job.Job;
|
||||||
|
|
||||||
public class Equipment implements Item {
|
public class Equipment implements Item {
|
||||||
|
|
||||||
private String id;
|
private String id;
|
||||||
private String name;
|
private String name;
|
||||||
private EquipmentType equipmentType;
|
private EquipmentType equipmentType;
|
||||||
|
private Job equipable;
|
||||||
private int slot;
|
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.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
this.equipable = equipable;
|
||||||
this.equipmentType = equipmentType;
|
this.equipmentType = equipmentType;
|
||||||
this.slot = slot;
|
this.slot = slot;
|
||||||
}
|
}
|
||||||
@ -33,10 +38,19 @@ public class Equipment implements Item {
|
|||||||
return equipmentType;
|
return equipmentType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Job getEquipable() {
|
||||||
|
return equipable;
|
||||||
|
}
|
||||||
|
|
||||||
public int getSlot() {
|
public int getSlot() {
|
||||||
return slot;
|
return slot;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean equals(Equipment e) {
|
||||||
|
return name.equals(e.getName()) && equipmentType.toString().equals(e.getEquipmentType().toString());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return name + equipmentType + slot;
|
return name + equipmentType + slot;
|
||||||
|
|||||||
8
src/main/java/Job/Knight.java
Normal file
8
src/main/java/Job/Knight.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package Job;
|
||||||
|
|
||||||
|
public class Knight extends Job{
|
||||||
|
public Knight() {
|
||||||
|
super("Knight");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,22 +1,31 @@
|
|||||||
|
import Entity.Player;
|
||||||
|
import Item.MageHat;
|
||||||
|
import Job.Wizard;
|
||||||
|
import Job.Knight;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
|
|
||||||
import Item.Equipment;
|
import Item.Equipment;
|
||||||
import Item.BodyArmour;
|
import Item.BodyArmour;
|
||||||
import Item.AttributeModifier;
|
import Item.AttributeModifier;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
public class EquipmentTest {
|
public class EquipmentTest {
|
||||||
|
private Knight knight = new Knight();
|
||||||
|
private Wizard wizard = new Wizard();
|
||||||
|
private Player defaultWizard(){
|
||||||
|
return new Player("name", wizard);
|
||||||
|
}
|
||||||
private Equipment defaultBodyArmour() {
|
private Equipment defaultBodyArmour() {
|
||||||
AttributeModifier a = new AttributeModifier(4,8,7,1,2,8);
|
AttributeModifier a = new AttributeModifier(4,8,7,1,2,8);
|
||||||
BodyArmour b = new BodyArmour("Body Armour", a);
|
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() {
|
private Equipment defaultMageHat() {
|
||||||
AttributeModifier a = new AttributeModifier(4,8,2,10,2,8);
|
AttributeModifier a = new AttributeModifier(4,8,2,10,2,8);
|
||||||
BodyArmour b = new BodyArmour("Mage Hat", a);
|
MageHat b = new MageHat("Mage Hat", a);
|
||||||
return new Equipment("6", "Deathmage Cap", b, 1);
|
return new Equipment("6", "Deathmage Cap", b, wizard, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -73,4 +82,53 @@ public class EquipmentTest {
|
|||||||
var e = defaultBodyArmour();
|
var e = defaultBodyArmour();
|
||||||
assertEquals(16, 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");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user