我想制作一个小型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;
}
}