Я хочу создать небольшую RPG-игру, в которой был бы магазин, а также инвентарь игрока.У меня вопрос или вопрос: как можно структурировать элементы, доступные в игре?Было бы хорошо иметь класс для каждого элемента или просто класс элемента, который может представлять элемент любого типа?В настоящее время у меня есть только класс элементов, который может создавать все типы элементов, однако он не работает так, как мне бы хотелось.
В настоящее время у меня есть класс Item, который используется при создании элемента.Существует также класс Items, который следит за всеми элементами в целом.Например, отображение всех предметов, доступных в магазине, предметов, которые можно сбрасывать, и т. Д.
public class Items {
public static Items _session = null;
private ArrayList<Item> items = new ArrayList<Item>();
private double[] itemWeights = {
0.1,
0.1,
0.1,
0.1
};
private String[] itemNames = {
"Ship Part",
"Health Potion",
"Strength Potion",
"Lucky Potion"
};
private String[] itemDescriptions = {
"Replacing one of the ships missing parts",
"Returning health to a crew member",
"Increasing the max health of a crew memnber",
"Increasing the amount of items found through a single planet search"
};
private String[] itemStats = {
"Ship parts repaired + 1",
"Crew member health + 5% max health",
"Crew member max health + 10",
"Item drop + 1, (does not stack)"
};
private int[] itemCosts = {
10,
10,
10,
10
};
private Items() {
int i = 0;
while(i < itemNames.length) {
Item temp = new Item(itemNames[i], itemWeights[i], itemDescriptions[i], itemStats[i], itemCosts[i]);
items.add(temp);
i += 1;
}
}
public static Items getInstance() {
if(_session == null) {
_session = new Items();
}
return _session;
}
public void presentItems() {
System.out.println(); // Empty line to make content more readable
int currentPosition = 1;
for (Item item: items) {
String temp = String.format("%s. %s", currentPosition, item);
System.out.println(temp);
currentPosition ++;
}
}
public ArrayList<Item> getItems() {
return this.items;
}
public Item getItem(int index) {
return items.get(index);
}
}
public class Item {
private String name;
private double dropChance;
private String description;
private String stats;
private int cost;
private int count = 1;
/**
* Constructor for an Item object
* @param itemName Describes the items name.
* @param itemChance Describes the random chance the item has to drop.
* @param itemDescription Describes what the items does or is used for.
* @param itemStats Describes the exact affects the item will have when used.
*/
public Item(String itemName, double itemChance, String itemDescription, String itemStats, int itemCost) {
name = itemName;
dropChance = itemChance;
description = itemDescription;
stats = itemStats;
cost = itemCost;
}
/**
* Get the name of the current Item object.
* @return Will return a String representing the name of the item.
*/
public String getName() {
return this.name;
}
/**
* Get the drop chance of the current Item object.
* @return Will return a double value representing the drop chance of the item.
*/
public double getDropChance() {
return this.dropChance;
}
/**
* Get the description of the current Item object.
* @return Will return a String representing the description of the item.
*/
public String getDescription() {
return this.description;
}
/**
* Get the stats of the current Item object.
* @return Will return a String representing the stats of the item.
*/
public String getStats() {
return this.stats;
}
/**
* Get the cost of the current Item object.
* @return Will return an Integer value representing the cost of the item.
*/
public int getCost() {
return this.cost;
}
/**
* Get the amount of this item.
* @return An integer representing the amount of this item.
*/
public int getCount() {
return this.count;
}
/**
* Add 1 to the amount of this item.
*/
public void addCount() {
this.count += 1;
}
/**
* Deduct 1 from the amount of this item.
*/
public void deductCount() {
this.count -= 1;
}
/**
* Overrides the java.lang.Object.toString method to return a descriptive String of the object.
*/
@Override
public String toString() {
String temp = String.format("(%sX) %s...can be used for %s...it's affects are: %s...Costs %s Coins", count, name, description, stats, cost);
return temp;
}
}