安卓游戏RPG库存系统

编程入门 行业动态 更新时间:2024-10-21 10:19:50
本文介绍了安卓游戏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库存系统

本文发布于:2023-11-27 08:07:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637215.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:库存   系统   游戏   RPG

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!