2016-08-05 6 views
1

私はコンストラクタで関数を使うことに関して質問があります。 私は、EquipmentGenerator.csとCharacterInventory.csという2つのスクリプトからなる単純なインベントリを作成しています。 後者は、サイコロのためのスクリプトにもアクセスします。私は毎回私は新しい結果を得るようにc_dmg_rollとして武器クラスのコンストラクタであるdiceRolls.Rolld8を()、(を使用できるようにする必要がありますクラスのコンストラクタ内の関数からランダムダメージロールを取得できますか?

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)"})); 

:要するに

は、それはすべて、このラインについてですそれはちょうど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です:転がるサイコロのスクリプト/クラスDiceRollsの

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(); 
     } 
    } 

パート:

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

すべてを1つのことを除いて素晴らしい作品:私はweapon.dにアクセスするたびにランダムなダメージを得ることはできません。 mg_roll。 Debug.Logは毎回同じ番号を表示します。 私は学んでいますが、それを動作させる方法を見つけることができませんでした。 あなたはどのように私はそれを修正することができますアイデアはありますか?

文字列dmg_die == "1d8"かどうかをチェックして関数からロールを作成する "if"を作成できました。しかし、このステップを使わずにランダムなロールを起こす方法はありますか?リストに追加された武器のパラメータにRandom.Rangeとしてdmg_rollを書き込んでも、Update()に入れてInput.GetKeyDownで実行しても、別の番号もローリングされません。

答えて

1

すべての作品は1つのものを除いて素晴らしいです:それぞれランダムなダメージを受けることはありません 私はweapon.dmg_rollにアクセスします。

一度インスタンス化される自分のクラスへの武器のダメージを抽象化します。他のオブジェクトを呼び出すことによって 'ヒット'を実行するIWeaponDamageService.Roll(IWeaponType型)メソッド呼び出しのクラスを呼び出します。正直、あなたは命令パターンを探していると思う。

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

編集#3、D & Dのために!

ここには、各武器が独自のランダム性を持つことを示すコードがあります。私は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; 
     } 
    } 
} 
+0

ありがとうございます。私はあなたが正しいと確信しています。しかし、私はC#のコーディングが本当に新しいです、私はどのように自分のコードを行う方法を理解していない。 ダイスロールはDiceRollsクラスのメソッドです。 "Longsword"のdmg_roll(diceRolls.Rolld8())にどのようにアクセスして初期の結果だけでなく新しいロールを与えるかを例を使って説明できますか? 私は本当にここで紛失しています。このプロジェクトでは約5000行のコードを実行しましたが、これは私にとっては新しくなりました。 Debug.Logで何とか同じスクリプトにアクセスできるはずだと思います。誰が知っているので、後でいつかする必要があるかもしれません。 –

+0

私は返信でコードを投稿できなかったので、オリジナルを修正しました – Programmer

+0

あなたの時間と労力には大変感謝していますが、私はそれを同様のやり方で行うことができます - 例えば "1d8"だから私は兵器を持っています(Weaponクラスのコンストラクタに基づいて)、その "dmg_die"に "1d8"の文字列があれば、Random.Range(1,9)を実行します。それが "1d6"なら、Random.Range(1,7)を実行します。 しかし、私はこれが不安定であると思っただけですか?武器自身のパラメータから "diceRolls.Rolld8()"にアクセスするよりも遅いかもしれません。既にそこにあるのですが、問題は、このパラメータがアクセスされたときに新しい値をロールバックしないということです。私はその価値が変わらない理由を知ることはできません、それは機能です。 –

関連する問題