安卓游戏RPG库存系统 [英] Android game rpg inventory system

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

问题描述

我使用一个ArrayList作为我的存货。 我无法找出一个方式来增加同一项目的倍数无需占用点中的库存。例如:我加了药水给我的库存。现在我添加其他药剂,但这次不是增加另一个药水去库存化,它应该不是表明我:药水×2,而只占用了ArrayList中一个地方。我已经提出了一些解决方案,但我觉得,如果他们是坏的做法。一个解决方案,我想是其量变量添加到项目本身,并增加了。帮我找一个更好的解决方案?

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?

编辑:好的,请忽略上述。我已经得到了pretty的好答案,这一点,但让我吃惊的是,有对角色几乎没有教程玩游戏的库存系统。我已经做了很多谷歌搜索,并不能找到任何很好的例子/教程/源$ C ​​$ C。如果任何人都可以点我一些很好的例子/教程/源$ C ​​$ C(无所谓用什么语言,但preferable的Java,甚至C / C ++)我想AP preciate它,谢谢。哦,对标的物的任何书籍。

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)是使用地图<项目,整数GT; 的项目映射到清单这类物品的数量。

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

To get the "amount" for a certain item, you then just call get:

inventory.get(item)

添加的东西去库存化你

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

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

从清单中移除一些您可以例如做

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);

如果你这样做在很多地方这会导致混乱,所以我建议你封装在一个库存这些方法类。

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天全站免登陆