首页 > 解决方案 > 求教RPG游戏道具系统(Java)

问题描述

每个人!所以我在空闲时间一直在做一些游戏创意,我目前正在创建 rpg 项目系统。首先,我制作了基类Item。它实现了Comparable,我通过游戏中的名称设置了它的自然顺序。

public abstract class Item implements Comparable<Item> {
    
    protected String inGameName;
    protected String description;
    protected int id;
    protected int value;
    
    public Item(int id, String inGameName, String description, int value) {
        this.id = id;
        this.inGameName = inGameName;
        this.description = description;
        this.value = value;
    }
    
    public String getInGameName() {
        return this.inGameName;
    }
    
    public String getDescription() {
        return this.description;
    }
    
    public int getId() {
        return this.id;
    }
    
    public int getValue() {
        return this.value;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Item))
            return false;
        else {
            Item otherItem = (Item) obj;
            Integer thisId = this.getId();
            Integer otherId = otherItem.getId();
            return thisId.equals(otherId);
        }
    }
    
    @Override
    public int compareTo(Item otherItem) {
        return this.inGameName.compareTo(otherItem.getInGameName());
    }
}

之后,我创建了实现Stackable接口的类StackableItem 。此类表示可堆叠在库存中的物品,例如材料、药水等。

public interface Stackable {
    
    public int getStackLimit();
    public int getStackSize();
    public void setStackSize(int size);
    
}

public abstract class StackableItem extends Item implements Stackable {
    
    private int stackLimit;
    private int stackSize;
    
    public StackableItem(int id, String inGameName, String description, int value, int stackLimit, int stackSize) {
        super(id, inGameName, description, value);
        this.stackLimit = stackLimit;
        this.stackSize = stackSize;
    }
    
    public int getStackLimit() {
        return this.stackLimit;
    }
    
    public int getStackSize() {
        return this.stackSize;
    }
    
    public void setStackSize(int size) {
        this.stackSize = size;
    }
    
}

最后,我创建了实现Consumable接口的ConsumableItem类。

public interface Consumable {
    
    public boolean isItemOnCooldown();
    public int getCooldownTime();
    
}


public class ConsumableItem extends StackableItem implements Consumable {
    
    private boolean onCooldown;
    private int cooldownTime;
    
    public ConsumableItem(int id, String inGameName, String description, int value, int stackLimit, int stackSize, int cooldownTime) {
        super(id, inGameName, description, value, stackLimit, stackSize);
        this.cooldownTime = cooldownTime;
        this.onCooldown = false;
    }
    
    public boolean isItemOnCooldown() {
        return this.onCooldown;
    }
    
    public int getCooldownTime() {
        return this.cooldownTime;
    }
}

显然,缺少一些功能,例如对消费的影响,但这些都是最初的想法。所以现在我有了 ConsumableItem 类,我应该为我想要创建的每个项目创建子类,例如 Small Health Potion、Large Health Potion、Magic Sc​​roll 等,还是最好为每个项目创建具有预定义值的 Enum 并使用它实例化 ConsumableItem 的信息?

标签: java

解决方案


只是提醒一下,这是一个可以提出诸如“为什么这不起作用”之类的问题的空间,而这应该在讨论论坛中进行(规则明确规定不这样做)。只为未来:)

话虽如此,我会去枚举,因为作为一名游戏设计师,你永远不知道你将强制添加多少项目,并且为每个项目创建一个子类会很累。


推荐阅读