【发布时间】:2016-08-05 17:34:33
【问题描述】:
我有一个关于在构造函数中使用函数的问题。 我正在创建一个简单的库存,它由 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 每次都显示相同的数字。 我只是在学习,但找不到如何使其工作的信息。 您有什么想法可以解决吗?
我可以创建一个“if”来检查字符串是否为 dmg_die == "1d8",然后从函数中进行滚动。但是有没有办法在没有这一步的情况下使随机滚动发生?在添加到列表的武器参数中将 dmg_roll 写为 Random.Range 也不会滚动另一个数字,即使我将其放入 Update() 并由 Input.GetKeyDown 运行。
【问题讨论】:
标签: c# class unity3d constructor unity5