我可以从类的构造函数中的函数中获得随机伤害滚动吗
本文关键字:随机 伤害 滚动 函数 构造函数 我可以 | 更新日期: 2023-09-27 18:01:07
我有一个关于在构造函数中使用函数的问题。我正在创建一个简单的清单,它由两个脚本组成: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();
}
}
部分脚本/类骰子滚动骰子:
// 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.运行也是如此
一切都很好,只有一件事:我不能每个人都受到随机伤害我访问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;
}
}
}