我可以从类的构造函数中获取随机损坏卷吗?

时间:2016-08-05 17:34:33

标签: c# class unity3d constructor unity5

我有一个关于在构造函数中使用函数的问题。 我创建了一个简单的清单,它由2个脚本组成:EquipmentGenerator.cs和CharacterInventory.cs。 后者还加入了掷骰子的剧本。

简而言之,这就是这一行:

Add_to_inventory(new Weapon("Longsword", "Most popular type of sword, weapon of choice for many warriors and mercenaries.", false, 15, "gp", 3, "Longsword", "1d8", "slashing", diceRolls.Rolld8(), new List <string>() {"Versatile (1d10)"}));

我需要能够使用diceRolls.Rolld8(),它在Weapon类构造函数中作为c_dmg_roll,这样每次我得到新结果时(它只是Random.Range)。

EquipmentGenerator包含以这种方式制作的类:

[System.Serializable]
public class Item : IComparable<Item>
{
    public string name, description;
    public bool stackable;
    public int value;
    public string coin_type;
    public int weight;
    //  public string model_path;

    public Item (string c_name, string c_description, bool c_stackable, int c_value, string c_coin_type, int c_weight)
    {
        name = c_name;
        description = c_description;
        stackable = c_stackable;
        value = c_value;
        coin_type = c_coin_type;
        weight = c_weight;
        //  model_path = c_model_path;
        //, string c_model_path)
    }
    public int CompareTo(Item other)
    {
        return String.Compare (name, other.name);
    }
}

[System.Serializable]
public class Weapon : Item, IComparable <Weapon>
{
    public string weapon_prof;
    public string dmg_die;
    public string dmg_type;
    public int dmg_roll;
    public List <string> weapon_properties;

    public Weapon (string c_name, string c_description, bool c_stackable, int c_value, string c_coin_type, int c_weight, string c_weapon_prof, string c_dmg_die, string c_dmg_type, int c_dmg_roll, List <string> c_weapon_properties) : base (c_name, c_description, c_stackable, c_value, c_coin_type, c_weight)
    {
        weapon_prof = c_weapon_prof;
        dmg_die = c_dmg_die;
        dmg_type = c_dmg_type;
        dmg_roll = c_dmg_roll;
        weapon_properties = c_weapon_properties;            
    }
    public int CompareTo(Weapon other)
    { return String.Compare (name, other.name);     }
}

这是CharacterInventory:

public class CharacterInventory : MonoBehaviour
{
    public List<Weapon> char_inv_weapon;
    public List<Armor> char_inv_armor;
    public List<Potion> char_inv_potion;
    public List<Item> char_inv_other;

    DiceRolls diceRolls = new DiceRolls();

    void Start () {
        Add_to_inventory(new Weapon("Longsword", "Most popular type of sword, weapon of choice for many warriors and mercenaries.", false, 15, "gp", 3, "Longsword", "1d8", "slashing", diceRolls.Rolld8(), new List <string>() {"Versatile (1d10)"}));
        Add_to_inventory(new Weapon("Shortsword", "Sharp, lightweight, piercing weapon commonly used by shorter races and rogues.", false, 10, "gp", 2, "Shortsword", "1d6", "piercing", diceRolls.Rolld6(), new List <string>() {"Light", "Finesse"}));
        Add_to_inventory(new Weapon("Quarterstaff", "Long, blunt staff, typically made of wood. Common for wanderers, travellers, seen in use by wizards, druids and monks.", false, 2, "sp", 4, "Quarterstaff", "1d6", "blunt", diceRolls.Rolld6(), new List <string>() {"Versatile (1d8)"}));


        // Debug: Count current inventory items and list them in Console via foreach loop

        Debug.Log ("Current number of weapons in character's inventory: " + char_inv_weapon.Count + ". Check all item details in Unity Editor's Inspector on the right. Below see a list of weapons:");
        foreach (Weapon weapon in char_inv_weapon) {
            Debug.Log ("Weapon name and description plus rolled damage:  " + weapon.name + ":  " + weapon.description + " Rolled damage: " + weapon.dmg_roll);
        }

        Debug.Log ("Current number of weapons in character's inventory: " + char_inv_weapon.Count + ". Check all item details in Unity Editor's Inspector on the right. Below see a second roll of list of weapons:");
        foreach (Weapon weapon in char_inv_weapon) {
            Debug.Log ("Weapon  and description plus damage rolled second time:  " + weapon.name + ":  " + weapon.description + " Rolled damage: " + weapon.dmg_roll);
        }

    }

    // Sorting

    public void Add_to_inventory(Item it)
    {
        if (it is Weapon)
        {
            char_inv_weapon.Add((Weapon)it);
            char_inv_weapon.Sort();
        }
        else if (it is Armor)
        {
            char_inv_armor.Add((Armor)it);
            char_inv_armor.Sort();
        }
        else if (it is Potion)
        {
            char_inv_potion.Add((Potion)it);
            char_inv_potion.Sort();
        }
        else
        {
            char_inv_other.Add(it);
            char_inv_other.Sort();
        }
    }

用于滚动骰子的脚本/类DiceRolls的一部分:

    // define d8 rolls
   public int Rolld8() {
        d8 = Random.Range (1, 9);
        return d8;
    }

除了一件事之外,一切都很好:每次访问weapon.dmg_roll时我都无法获得随机伤害。 Debug.Log每次都显示相同的数字。 我只是在学习,但无法找到如何使其发挥作用的信息。 你有什么想法我该如何解决它?

我可以制作一个&#34; if&#34;这将检查字符串dmg_die ==&#34; 1d8&#34;然后从功能滚动。但有没有办法在没有这一步的情况下实现随机滚动?将dmg_roll作为Random.Range写入添加到列表的武器的参数中也不会滚动另一个数字,即使我将它放在Update()中并由Input.GetKeyDown运行。

1 个答案:

答案 0 :(得分:1)

  

除了一件事之外,一切都很好:我无法获得随机伤害   我访问weapon.dmg_roll。

抽象武器损坏它自己的类,将被实例化一次。在方法调用IWeaponDamageService.Roll(IWeaponType类型)中调用该类,该类执行&#39;命中&#39;通过调用您的其他对象。老实说,我认为你正在寻找命令模式。

http://www.dofactory.com/net/command-design-pattern

在这里编辑:

Debug.Log ("Current number of weapons in character's inventory: " + char_inv_weapon.Count + ". Check all item details in Unity Editor's Inspector on the right. Below see a list of weapons:");
var weaponService = new WeaponService();
            foreach (Weapon weapon in char_inv_weapon) {
                Debug.Log ("Weapon name and description plus rolled damage:  " + weapon.name + ":  " + weapon.description + " Rolled damage: " + weaponService.GetDamageRoll(weapon)
            }
public class WeaponService{

public int GetDamageRoll(object wpn){
if(wpn.name == "LongSword"){
return 49;
}
else{
...
}
}
}

HTH

为D&amp; D编辑#3

这是代码,显示每个武器都有自己的随机。我认为DICEROLLS已经成功了。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var diceRolls = new DiceRolls();
            var inventory = new CharacterInventory();

            inventory.Add_to_inventory(new Weapon("Longsword",
                "Most popular type of sword, weapon of choice for many warriors and mercenaries.", false, 15, "gp", 3,
                "Longsword", "1d8", "slashing", diceRolls.Rolld8(), new List<string>() { "Versatile (1d10)" }));
            inventory.Add_to_inventory(new Weapon("Shortsword",
                "Sharp, lightweight, piercing weapon commonly used by shorter races and rogues.", false, 10, "gp", 2,
                "Shortsword", "1d6", "piercing", diceRolls.Rolld6(), new List<string>() { "Light", "Finesse" }));
            inventory.Add_to_inventory(new Weapon("Quarterstaff",
                "Long, blunt staff, typically made of wood. Common for wanderers, travellers, seen in use by wizards, druids and monks.",
                false, 2, "sp", 4, "Quarterstaff", "1d6", "blunt", diceRolls.Rolld6(),
                new List<string>() { "Versatile (1d8)" }));


            // Debug: Count current inventory items and list them in Console via foreach loop

            Console.WriteLine("Current number of weapons in character's inventory: " + inventory.char_inv_weapon.Count +
                              ". Check all item details in Unity Editor's Inspector on the right. Below see a list of weapons:");
            foreach (Weapon weapon in inventory.char_inv_weapon)
            {
                Console.WriteLine("Weapon name and description plus rolled damage:  " + weapon.name + ":  " +
                                  weapon.description + " Rolled damage: " + weapon.dmg_roll);
            }

            Console.WriteLine("Current number of weapons in character's inventory: " + inventory.char_inv_weapon.Count +
                              ". Check all item details in Unity Editor's Inspector on the right. Below see a second roll of list of weapons:");
            foreach (Weapon weapon in inventory.char_inv_weapon)
            {
                Console.WriteLine("Weapon  and description plus damage rolled second time:  " + weapon.name + ":  " +
                                  weapon.description + " Rolled damage: " + weapon.dmg_roll);
            }
        }
    }



    [System.Serializable]
    public class Item : IComparable<Item>
    {
        public string name, description;
        public bool stackable;
        public int value;
        public string coin_type;
        public int weight;
        //  public string model_path;

        public Item(string c_name, string c_description, bool c_stackable, int c_value, string c_coin_type, int c_weight)
        {
            name = c_name;
            description = c_description;
            stackable = c_stackable;
            value = c_value;
            coin_type = c_coin_type;
            weight = c_weight;
            //  model_path = c_model_path;
            //, string c_model_path)
        }

        public int CompareTo(Item other)
        {
            return String.Compare(name, other.name);
        }
    }

    [System.Serializable]
    public class Weapon : Item, IComparable<Weapon>
    {
        public string weapon_prof;
        public string dmg_die;
        public string dmg_type;
        public int dmg_roll;
        public List<string> weapon_properties;

        public Weapon(string c_name, string c_description, bool c_stackable, int c_value, string c_coin_type,
            int c_weight, string c_weapon_prof, string c_dmg_die, string c_dmg_type, int c_dmg_roll,
            List<string> c_weapon_properties) : base(c_name, c_description, c_stackable, c_value, c_coin_type, c_weight)
        {
            weapon_prof = c_weapon_prof;
            dmg_die = c_dmg_die;
            dmg_type = c_dmg_type;
            dmg_roll = c_dmg_roll;
            weapon_properties = c_weapon_properties;
        }

        public int CompareTo(Weapon other)
        {
            return String.Compare(name, other.name);
        }
    }


    public class CharacterInventory
    {
        public List<Weapon> char_inv_weapon;
        public List<object> char_inv_armor;
        public List<object> char_inv_potion;
        public List<Item> char_inv_other;


        public CharacterInventory()
        {
            char_inv_weapon = new List<Weapon>();
            char_inv_armor = new List<object>();
            char_inv_potion = new List<object>();
            char_inv_other = new List<Item>();
        }

        // Sorting

        public void Add_to_inventory(Item it)
        {
            if (it is Weapon)
            {
                char_inv_weapon.Add((Weapon) it);
                char_inv_weapon.Sort();
            }
            //else if (it is Armor)
            //{
            //    char_inv_armor.Add((Armor) it);
            //    char_inv_armor.Sort();
            //}
            //else if (it is Potion)
            //{
            //    char_inv_potion.Add((Potion) it);
            //    char_inv_potion.Sort();
            //}
            else
            {
                char_inv_other.Add(it);
                char_inv_other.Sort();
            }
        }
    }

    internal class DiceRolls
    {
        public int Rolld8()
        {
            Random rnd1 = new Random();
            var d8 = rnd1.Next(1, 9);
            return d8;
        }

        public int Rolld6()
        {
            Random rnd1 = new Random();
            var d6 = rnd1.Next(1, 7);
            return d6;
        }
    }
}