I have a builder design pattern for Characters and items in my text based role playing game.
the CharacterComponentBuilder:
public class CharacterComponent {
private int id;
private String name;
private int Hp;
private int MaxHp;
private int Defence;
private int Strength;
private int Agility;
private int Intellect;
private Range range;
private Items[] Inventory;
public void setHp(int hp) {
Hp = hp;
}
public int getMaxHp() {
return MaxHp;
}
public void setDefence(int defence) {
Defence = defence;
}
private CharacterComponent(){
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getHp() {
return Hp;
}
public int getStrength() {
return Strength;
}
public int getAgility() {
return Agility;
}
public int getIntellect() {
return Intellect;
}
public int getDefence() {
return Defence;
}
public Range getRange() {
return range;
}
public Items[] getInventory() {
return Inventory;
}
public void setInventory(Items[] inventory) {
Inventory = inventory;
}
public void setmaxHp(int maxHp) {
MaxHp = maxHp;
}
public static class CharacterComponentBuilder {
private CharacterComponent component;
public static CharacterComponentBuilder builder() {
CharacterComponentBuilder builder = new CharacterComponentBuilder();
builder.component = new CharacterComponent();
return builder;
}
public CharacterComponentBuilder id(int id){
this.component.id = id;
return this;
}
public CharacterComponentBuilder name(String name){
this.component.name = name;
return this;
}
public CharacterComponentBuilder Hp(int hp){
this.component.Hp = hp;
return this;
}
public CharacterComponentBuilder MaxHp(int maxhp){
this.component.MaxHp = maxhp;
return this;
}
public CharacterComponentBuilder Strength(int strength){
this.component.Strength = strength;
return this;
}
public CharacterComponentBuilder Agility(int agility){
this.component.Agility = agility;
return this;
}
public CharacterComponentBuilder Intellect(int intellect){
this.component.Intellect = intellect;
return this;
}
public CharacterComponentBuilder Range(Range range) {
this.component.range = range;
return this;
}
public CharacterComponentBuilder Defence(int defence){
this.component.Defence = defence;
return this;
}
public CharacterComponentBuilder Inventory(Items[] inventory){
this.component.Inventory = inventory;
return this;
}
public CharacterComponent build(){
return this.component;
}
}
}
You will notice the Inventory component being an array, this counts as an inventory to the character, in theory the character should equip an item, Which puts it in its inventory (an array) thus gaining and stats and bonuses
And this is the ItemsComponentBuilder:
public class ItemsComponent {
private int hp;
private int maxHp;
private int Strength;
private int Agility;
private int Intellect;
private int Defence;
private Range Range; // Range and Rarity are enums
Rarity Rarity;
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp;
}
public int getMaxHp() {
return maxHp;
}
public void setMaxHp(int maxHp) {
this.maxHp = maxHp;
}
public int getStrength() {
return Strength;
}
public void setStrength(int strength) {
Strength = strength;
}
public int getAgility() {
return Agility;
}
public void setAgility(int agility) {
Agility = agility;
}
public int getIntellect() {
return Intellect;
}
public void setIntellect(int intellect) {
Intellect = intellect;
}
public int getDefence() {
return Defence;
}
public void setDefence(int defence) {
Defence = defence;
}
public Range getRange() {
return Range;
}
public void setRange(Range range) {
Range = range;
}
public Rarity getRarity() {
return Rarity;
}
public void setRarity(Rarity rarity) {
Rarity = rarity;
}
public static class ItemComponentBuilder{
private ItemsComponent component;
public static ItemComponentBuilder builder(){
ItemComponentBuilder builder = new ItemComponentBuilder();
builder.component = new ItemsComponent();
return builder;
}
public ItemComponentBuilder hp(int hp){
this.component.hp = hp;
return this;
}
public ItemComponentBuilder maxHp(int maxhp){
this.component.maxHp = maxhp;
return this;
}
public ItemComponentBuilder Strength(int strength){
this.component.Strength = strength;
return this;
}
public ItemComponentBuilder Agility(int agility){
this.component.Agility = agility;
return this;
}
public ItemComponentBuilder Intellect(int intellect){
this.component.Intellect = intellect;
return this;
}
public ItemComponentBuilder Defence(int defence){
this.component.Defence = defence;
return this;
}
public ItemComponentBuilder Range(Range range){
this.component.Range = range;
return this;
}
public ItemComponentBuilder Rarity(Rarity rarity){
this.component.Rarity = rarity;
return this;
}
public ItemsComponent build(){
return this.component;
}
}
}
these are the Character and Items classes that use these builders:
public class Character {
private CharacterComponent characterComponent;
public Character(CharacterComponent characterComponent) {
this.characterComponent = characterComponent;
}
public CharacterComponent getCharacterComponent() {
return characterComponent;
}
//The Method below shows the warrior (a character) Attack dmg, this should increase or decrease based on the warrior Strength Component
public double WarriorAtkValue(Character character){
double BaseDmg = 20;
double MainStateModifier = 0.3;
return BaseDmg + (character.getCharacterComponent().getStrength() * MainStateModifier);
}
and this is the Items class:
public class Items {
private ItemsComponent itemsComponent;
public Items(ItemsComponent itemComponent) {
this.itemsComponent = itemComponent;
}
public ItemsComponent getItemsComponent() {
return itemsComponent;
}
void EquipItem(Character character, Items item){
//this should equip an items to a character, giving the character its stats (example: ChestPiece has 20 strength, if warrior equips this item, he gains 20+ strength)
}
}
How should I go about:
adding items to a CharacterComponent array (I know there is no x.add method)
making those items stats add to the character?
this is an example code you can run to check yourself:
void main() {
Scanner scanner = new Scanner(System.in);
public Items[] WarriorInventory = new Items[4];
Character Warrior = new Character(CharacterComponent.CharacterComponentBuilder.builder()
.id(11)
.name("Warrior")
.Hp(150)
.MaxHp(150)
.Defence(30)
.Strength(100)
.Range(Range.MELEE)
.Inventory(WarriorInventory)
.build());
System.out.println(Warrior.getCharacterComponent().getName() + " : " + Warrior.getCharacterComponent().getStrength() " <- The Warrior Strength" +
" Should a different value if we modify it by adding items to its inventory");
Items ArmorChestPiece = new Items(ItemsComponent.ItemComponentBuilder.builder()
.hp(20)
.maxHp(20)
.Defence(20)
.Rarity(Rarity.COMMON)
.build());
the enums:
public enum Range {
RANGED,
MELEE
}
public enum Rarity {
COMMON,
RARE,
EPIC,
LEGENDARY,
}
I tried using the following method, but I feel like there is something wrong with it:
public class Character {
void EquipItem(Items item){
Items[] characterInventory = this.getCharacterComponent().getInventory();
for(int arraySlotNumber = 0; characterInventory[arraySlotNumber] != null; arraySlotNumber++){
characterInventory[arraySlotNumber] = item;
}
this.getCharacterComponent().setInventory(characterInventory);
int hp= item.getItemsComponent().getHp();
int maxHp= item.getItemsComponent().getMaxHp();
int strength= item.getItemsComponent().getStrength(); //these are empty
int agility= item.getItemsComponent().getAgility(); // for future
int intellect= item.getItemsComponent().getIntellect(); // items with
int defence= item.getItemsComponent().getDefence(); // diffrent stats
int characterhp= this.getCharacterComponent().getHp();
int charactermaxHp= this.getCharacterComponent().getMaxHp();
int characterstrength= this.getCharacterComponent().getStrength();
int characteragility= this.getCharacterComponent().getAgility();
int characterintellect= this.getCharacterComponent().getIntellect();
int characterdefence= this.getCharacterComponent().getDefence();
this.getCharacterComponent().setHp(characterhp+ hp);
this.getCharacterComponent().setmaxHp(charactermaxHp+maxHp);
}
}
this was in the Character class rather than the Items class please tell me what I'm doing wrong and how can I improve it, and how I can improve this question as well.