首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >经典RPG项目处理

经典RPG项目处理
EN

Stack Overflow用户
提问于 2014-01-27 18:17:19
回答 2查看 1.5K关注 0票数 0

如何以一种有组织和易于管理的方式构造项目(因为有不同类型的项目)。

我的想法是有一个ItemManager来加载所有的项目并存储它们。不过,这是我不确定的。只有武器才有最小伤害和最大伤害。只有盔甲才有盔甲。我真的应该把它们都作为物品储存起来吗?

有了遗产,我可以解决这个问题(我想!)

ItemManager.cs

代码语言:javascript
复制
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ItemManager : MonoBehaviour {

    private static List<Item> items;

    public ItemManager () {
        items = new List<Item> ();
    }

    void Awake () {     
        DontDestroyOnLoad (this);

        // load all items from file
    }

    public static int Length {
        get {return items.Count;}
    }

    public static Item Item (int index) {
        return items [index];
    }
}

Item.cs

代码语言:javascript
复制
using UnityEngine; // only used for debug purposes

public class Item : MonoBehaviour { // : MonoBehaviour are for debug purposes

    private string _name;
    private string _desc;

    private int _maxQuantity; // quest items wont have one

    private int _category;
    private bool _dropable; // quest items wont have one
    private int _price; // quest items wont have one

    public Item () {
        _name = string.Empty;
        _desc = string.Empty;

        _quantity = 0;
        _maxQuantity = 0;

        _category = 0;
        _dropable = false;
        _price = 0;
    }

#region Setters and Getters
    public string Name {
        get {return _name;}
        set {_name = value;}
    }

    public string Desc {
        get {return _desc;}
        set {_desc = value;}
    }

    public int MaxQuantity {
        get {return _maxQuantity;}
        set {_maxQuantity = value;}
    }

    public int Category {
        get {return _category;}
        set {_category = value;}
    }

    public bool Dropable {
        get {return _dropable;}
        set {_dropable = value;}
    }

    public int Price {
        get {return _price;}
        set {_price = value;}
    }
#endregion
}

// enum used to identify categories by name
public enum ItemCategory {
    Miscellaneous,
    Quest,
    Consumable,
    Weapon,
    Armor,
    Gem,
    Resource,
};

我一般(试着!)当变量有getter和/或setter时,使用_作为前缀。

存储项目的“实例”将在字符/党派级别上进行。持有物品和数量。

首先,我需要解决许多问题,即如何处理变量的实际存储和访问。

我实际上是如何包装项目存储和加载过程的?

EN

回答 2

Stack Overflow用户

发布于 2014-01-27 18:22:17

只有武器才有最小伤害和最大伤害。只有盔甲才有盔甲。我真的应该把它们都作为物品储存起来吗?

您可以为WeaponsArmors定义另一个类,这些类继承自Item class.Make您的Itemabstract,并在其中定义公共属性。

为了获得和设置一个变量,您不需要这样的属性:

代码语言:javascript
复制
public string Desc {
    get {return _desc;}
    set {_desc = value;}
}

相反,如果在获取和设置值时没有不同的逻辑,则使用自动实现属性:

代码语言:javascript
复制
public string Desc { get; set; }
票数 1
EN

Stack Overflow用户

发布于 2014-01-27 18:29:13

我将使用接口:一个基本接口用于常见的东西,如ID、name等,其他接口用于武器/装甲等。然后,您可以在运行时测试实现了哪些接口,这样就可以根据需要(即显示伤害或装甲值)来执行操作。继承的主要好处是构图的简单性和做鸭类的可能性,例如有一把椅子和一件需要的武器。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21388509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档