Android:创建、存储和显示类似 RPG 项目的最佳实践

发布于 2024-12-04 00:08:58 字数 331 浏览 3 评论 0原文

我需要一种替代方法来处理游戏内物品。目前,我正在为项目可能具有的每个方面使用变量。

示例:武器具有伤害和暴击率。假设有两种可能的武器:斧头和剑。

String weapon = "Axe";
int damage = 1;
int critical = 5;

现在我需要添加特殊元素,例如统计增益(增加攻击、生命值、法术强度等)。

我是否需要为武器可以增加的每个统计数据添加一个变量?或者更确切地说是一个要增加统计数据的变量和一个表示增加量的变量?然后,有一个 if 语句检查统计数据是否增加?显示它也一样吗?

有什么建议吗?谢谢!

I need an alternative way to handle in-game items. Currently, I'm using a variable for every aspect an item could have.

Example: a weapon has damage and critical hit chance. Suppose there are two types of weapons possible, axes and swords.

String weapon = "Axe";
int damage = 1;
int critical = 5;

Now I need to add special elements, such as stat buffs(increase attack, health, spell power, etc.).

Do I need a variable for each stat a weapon could increase? Or rather a variable with the stat to be increased and a variable for the amount it is increased by? Then, have an if statement check for the stat to increase? And the same for displaying it?

Any tips? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡莣 2024-12-11 00:08:58

我不确定这是否是最好的方法,但是您可以有一个修饰符(生命值,功率等)的列表(或枚举),并为每个项目存储修饰符和数量的地图(也可以为负数,如使用该物品的惩罚)。

您确实应该使用类来定义项目,而不是单个变量:

class Item {
    String name;
}

class Weapon extends Item {
    int damage;
    int critical;
}

...等等。

I'm not sure if this is the best way to do it, but you could have a list (or enum) of modifiers (health, power, etc), and store for each item a map of modifiers and amounts (which could also be negative, as in penalties for using the item).

And you really should use classes for defining the items, instead of individual variables:

class Item {
    String name;
}

class Weapon extends Item {
    int damage;
    int critical;
}

... and so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文