安卓游戏RPG盘点系统 [英] Android game rpg inventory system

查看:31
本文介绍了安卓游戏RPG盘点系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ArrayList 作为我的库存".我无法找到一种方法来添加相同项目的倍数而不占用库存"中的位置.例如:我在我的库存中添加了一种药水.现在我添加了另一种药水,但这次不是将另一种药水添加到库存中,而是应该显示我有:药水 x 2,同时只在 ArrayList 中占据一个位置.我提出了一些解决方案,但我觉得它们是不好的做法.我尝试的一种解决方案是向项目本身添加一个 AMOUNT 变量并增加它.帮我找到更好的解决方案?

I am using an ArrayList as my "inventory". I am having trouble figuring out a way to add multiples of the same item without taking up a spot in the "inventory". For example: I add a potion to my inventory. Now I add ANOTHER potion but this time instead of adding ANOTHER potion to the inventory, it should instead show that I have: Potions x 2, while only taking up one spot in the ArrayList. I have come up with a few solutions but I feel as if they are bad practices. One solution I tried was to add an AMOUNT variable to the item itself and increment that. Help me find a much better solution?

好的,请忽略以上内容.我已经得到了很好的答案,但令我惊讶的是几乎没有关于角色扮演游戏库存系统的教程.我做了很多谷歌搜索,找不到任何好的例子/教程/源代码.如果有人能指点我一些好的例子/教程/源代码(与什么语言无关,但最好是 java,甚至 c/c++),我将不胜感激,谢谢.哦,还有关于这个主题的任何书籍.

Ok please ignore the above. I have gotten pretty good answers for that but what surprised me was that there were almost no tutorials on role playing game inventory systems. I've done a lot of google searching and cannot find any good examples/tutorials/source code. If anyone could point me to some good examples/tutorials/source code (does not matter what language, but preferable java, or even c/c++) I would appreciate it, thanks. Oh, and any books on the subject matter.

推荐答案

解决这个问题的常用方法(使用标准 API)是使用一个 Map 来映射一个项目到库存中此类项目的数量.

The usual way to solve this (using the standard API) is to use a Map<Item, Integer> that maps an item to the number of of such items in the inventory.

获取某个项目的数量",您只需调用get:

inventory.get(item)

添加一些东西到你所做的清单

if (!inventory.containsKey(item))
    inventory.put(item, 0);

inventory.put(item, inventory.get(item) + 1);

从库存中删除某些东西,例如您可以做的

To remove something from the inventory you could for instance do

if (!inventory.containsKey(item))
    throw new InventoryException("Can't remove something you don't have");

inventory.put(item, inventory.get(item) - 1);

if (inventory.get(item) == 0)
    inventory.remove(item);

如果你在很多地方这样做,这会变得很混乱,所以我建议你将这些方法封装在一个 Inventory 类中.

This can get messy if you do it in many places, so I would recommend you to encapsulate these methods in an Inventory class.

祝你好运!

这篇关于安卓游戏RPG盘点系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆