【发布时间】:2017-07-26 00:20:01
【问题描述】:
目前我有一个像这样工作的库存实施
- 有一个名为Item 的基类,它是库存数组所包含的,然后有类Weapon 和Consumable 继承自Item 和然后类Gun继承自Weapon。但是,我有一个问题,我希望项目具有可更改的属性,这意味着,我希望 Gun 类保存枪中的弹药量,但如果它被更改,那么考虑到它使用的是类的相同实例,那么它将更改库存中所有特定类型枪支的弹药。我听说过ICloneable,但它显然已经贬值,不应该使用。
我的物品脚本:
using UnityEngine;
using System.Collections.Generic;
public class Items : MonoBehaviour
{
static Dictionary<int, Item> items = new Dictionary<int, Item>();
private static bool initialized;
[SerializeField]
Texture2D[] itemIcons;
private void Start()
{
Gun desertEagle = new Gun();
desertEagle.damage = 40;
desertEagle.range = 20;
desertEagle.maxAmmunition = 7;
desertEagle.firemode = FireMode.Semi;
desertEagle.firerate = 1;
desertEagle.name = "Desert Eagle";
desertEagle.description = "Desert Eagle (.50 Cal) is a semi-automatic pistol with an ammo capacity of 7 rounds";
desertEagle.equippable = true;
desertEagle.icon = itemIcons[1];
Consumable donut = new Consumable();
donut.food = 30;
donut.name = "Donut";
donut.description = "A ring full of legendary awesomeness";
donut.equippable = true;
donut.icon = itemIcons[2];
Consumable coffee = new Consumable();
coffee.water = 30;
coffee.stamina = 50;
coffee.name = "Coffee";
coffee.description = "A delicious beverage to help you get up in the morning. Goes well with donuts.";
coffee.equippable = true;
coffee.icon = itemIcons[3];
RegisterItem(1, desertEagle);
RegisterItem(2, donut);
RegisterItem(3, coffee);
initialized = true;
}
public static void RegisterItem(int id, Item item)
{
items.Add(id, item);
}
public static void UnregisterItem(int id)
{
items.Remove(id);
}
public static Item GetItem(int id)
{
return items[id];
}
}
public class ItemStack
{
Item item;
int amount;
int max = 10;
public void Add(int amount)
{
if (item.stackable && this.amount + amount <= max) this.amount += amount;
else if (item.stackable) this.amount = max;
}
public void Remove(int amount)
{
if (item.stackable) this.amount -= amount;
}
}
public class Item
{
public string name = "Item";
public string description = "Your everyday standard item.";
public bool equippable = false;
public bool stackable = true;
public Mesh model;
public Texture2D icon;
}
public class Weapon : Item
{
public float damage = 5;
public float range = 1;
}
public class Gun : Weapon
{
public int ammunition = 1;
public int maxAmmunition = 1;
public FireMode firemode = FireMode.Semi;
public int firerate = 1;
public new float range = 10;
}
public enum FireMode
{
Semi,
Automatic,
Burst
}
public class Consumable : Item
{
public float food;
public float water;
public float health;
public float stamina;
}using UnityEngine;
using System.Collections.Generic;
我的库存代码:
Item[] inventory;
[SerializeField]
int inventorySize = 16;
inventory = new Item[inventorySize];
inventory[0] = Items.GetItem(1);
inventory[1] = Items.GetItem(2);
inventory[12] = Items.GetItem(3);
【问题讨论】:
-
对不起,我无法理解你想要什么。顺便说一句,您可以考虑将您的
Consumable设为接口(如IConsumable),顾名思义。然后你可以有像Coffee和Donut这样的类来实现它。 -
不知道我是否理解您的问题。你想共享弹药吗?如果您有 2 只沙漠之鹰,它们会共享您拥有的弹药。而已?如果是这样,您只需将弹药用作消耗品即可。
-
基本上,如果一个项目的值发生变化,因为该项目的相同实例被用作存储在项目列表中的那些,那么库存中所有该项目的值都会发生变化
-
我想我发现了问题和解决问题的两种方法。已经发布了答案。如果这不是您要找的东西,请告诉我。如果是,请接受答案。
-
ComputerFido 如果我的回答解决了您的问题,请告诉我,如果有,请接受。