Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用字典简化方法_C#_Unity3d - Fatal编程技术网

C# 用字典简化方法

C# 用字典简化方法,c#,unity3d,C#,Unity3d,我正在尝试为我的游戏制作一个关卡生成器原型,但是我遇到了一个问题,就是一个刚刚产卵的飞机不能在之前向后产卵到产卵点 下面是我的示例代码: using UnityEngine; using System.Collections.Generic; public class LevelGenerator : MonoBehaviour { private GameObject[] generatedTiles; public int tilesAmount; public static int ti

我正在尝试为我的游戏制作一个关卡生成器原型,但是我遇到了一个问题,就是一个刚刚产卵的飞机不能在之前向后产卵到产卵点

下面是我的示例代码:

using UnityEngine;
using System.Collections.Generic;

public class LevelGenerator : MonoBehaviour
{
private GameObject[] generatedTiles;
public int tilesAmount;
public static int tilesOffset = 10;
//public int tilesSize;

private int factor;

private Dictionary<int, Vector3> factorDict = new Dictionary<int, Vector3>(4)
{
    {1, new Vector3(0, 0, -tilesOffset)},
    {2, new Vector3(-tilesOffset, 0, 0)},
    {3, new Vector3(0, 0, tilesOffset)},
    {4, new Vector3(tilesOffset, 0, 0)}
};

private void Start()
{
    SpawnTiles();
    ArrangeTiles();
}

void SpawnTiles()
{
    generatedTiles = new GameObject[tilesAmount];

    for(int i = 0; i < tilesAmount; i++)
    {
        generatedTiles[i] = GameObject.CreatePrimitive(PrimitiveType.Plane);
    }
}

public void ArrangeTiles()
{
    Vector3 lastPlacedTile = Vector3.zero;

    for(int i = 1; i <= generatedTiles.Length; i++)
    {
        generatedTiles[i].transform.position = lastPlacedTile + DirectionRandomizer(factor);
        lastPlacedTile = generatedTiles[i].transform.position;
    }
}

Vector3 DirectionRandomizer(int factor)
{
    /*
     * Calculating where to place the next plane by taking factor int, factor determine the placement of the next plane by blocking the previously used vector.
     * below are phrohibited vector placement by certain vector
     * 1 == z
     * 2 == x
     * 3 == -z
     * 4 == -x
     */
    if(factor == 0)
    {
        switch (Random.Range(0f, 1f))
        {
            case float n when (n <= 0.25):
                return factorDict[4];
            case float n when (n <= 0.5):
                return factorDict[3];
            case float n when (n <= 0.75):
                return factorDict[2];
            case float n when (n <= 0.1):
                return factorDict[1];
            default:
                return Vector3.zero;
        }
    }
    else if(factor == 1)
    {
        switch (Random.Range(0f, 0.75f))
        {
            case float n when (n <= 0.25):
                factor = 4;
                return factorDict[4];
            case float n when (n <= 0.5):
                factor = 2;
                return factorDict[2];
            case float n when (n <= 0.75):
                factor = 1;
                return factorDict[1];
            default:
                return Vector3.zero;
        }
    }else if(factor == 2)
    {
        switch (Random.Range(0f, 0.75f))
        {
            case float n when (n <= 0.25):
                return new Vector3(0, 0, tilesOffset);
            case float n when (n <= 0.50):
                return new Vector3(-tilesOffset, 0, 0);
            case float n when (n <= 0.75):
                return new Vector3(0, 0, -tilesOffset);
            default:
                return Vector3.zero;
        }
    }else if(factor == 3)
    {
        switch (Random.Range(0f, 0.75f))
        {
            case float n when (n <= 0.25):
                return new Vector3(tilesOffset, 0, 0);
            case float n when (n <= 0.5):
                return new Vector3(0, 0, tilesOffset);
            case float n when (n <= 0.75):
                return new Vector3(-tilesOffset, 0, 0);
            default:
                return Vector3.zero;
        }
    }else
    {
        switch (Random.Range(0f, 0.75f))
        {
            case float n when (n <= 0.25):
                return new Vector3(tilesOffset, 0, 0);
            case float n when (n <= 0.5):
                return new Vector3(0, 0, tilesOffset);
            case float n when (n <= 0.75):
                return new Vector3(0, 0, -tilesOffset);
            default:
                return Vector3.zero;
        }
    }
}
}
使用UnityEngine;
使用System.Collections.Generic;
公共类LevelGenerator:单行为
{
私有游戏对象[]生成文件;
公共int TILESAUNT;
公共静态int tilesOffset=10;
//公共int tilesSize;
私人智力因素;
专用词典factorDictionary=新词典(4)
{
{1,新向量3(0,0,-tileOffset)},
{2,新向量3(-tileOffset,0,0)},
{3,新向量3(0,0,tileOffset)},
{4,新向量3(tileOffset,0,0)}
};
私有void Start()
{
产卵片();
排列瓷砖();
}
void pawntiles()
{
GeneratedTitles=新游戏对象[tilesAmount];
对于(int i=0;i对于(int i=1;i首先,我将使用枚举而不是int:

private enum DirectionFactor {
    Start=0,
    Back=1,
    Left=2,
    Forward=3,
    Right=4
};

private DirectionFactor previousFactor;
然后,我将制作一个
方向因子数组
,您可以随机选择:

private readonly DirectionFactor[] selectableFactors =
       new DirectionFactor[] { DirectionFactor.Back, DirectionFactor.Left,
                               DirectionFactor.Forward, DirectionFactor.Right };
最后,在
DirectionRandomizer
中,随机索引到数组中,排除与输入因子“相反”的因子。当输入不是
Start
时,可以排除int值相差2的因子。使用Linq,这可能如下所示:

using System.Linq;

// ...

Vector3 DirectionRandomizer(DirectionFactor factor)
{
    DirectionFactor[] candidates = selectableFactors;
    if (factor != DirectionFactor.Start)
    {
        // ignore the one that is two away in value
        candidates = selectableFactors.Where(x => 
                Mathf.Abs(previousFactor - x) != 2).ToArray();
    }

    previousFactor = candidates[Random.Range(0, candidates.Length)];
    int factorIndex = (int)previousFactor;
    return factorDict[factorIndex];
}

旁注:您可能还应该让enum为字典编制索引:

private readonly Dictionary<DirectionFactor, Vector3> factorDict = 
        new Dictionary<DirectionFactor, Vector3>(4)
{
    {DirectionFactor.Back, new Vector3(0, 0, -tilesOffset)},
    {DirectionFactor.Left, new Vector3(-tilesOffset, 0, 0)},
    {DirectionFactor.Forward, new Vector3(0, 0, tilesOffset)},
    {DirectionFactor.Right, new Vector3(tilesOffset, 0, 0)}
};

首先,我将使用枚举而不是int:

private enum DirectionFactor {
    Start=0,
    Back=1,
    Left=2,
    Forward=3,
    Right=4
};

private DirectionFactor previousFactor;
然后,我将制作一个
方向因子数组
,您可以随机选择:

private readonly DirectionFactor[] selectableFactors =
       new DirectionFactor[] { DirectionFactor.Back, DirectionFactor.Left,
                               DirectionFactor.Forward, DirectionFactor.Right };
最后,在
DirectionRandomizer
中,随机索引到数组中,排除与输入因子“相反”的因子。当输入不是
Start
时,可以排除int值相差2的因子。使用Linq,这可能如下所示:

using System.Linq;

// ...

Vector3 DirectionRandomizer(DirectionFactor factor)
{
    DirectionFactor[] candidates = selectableFactors;
    if (factor != DirectionFactor.Start)
    {
        // ignore the one that is two away in value
        candidates = selectableFactors.Where(x => 
                Mathf.Abs(previousFactor - x) != 2).ToArray();
    }

    previousFactor = candidates[Random.Range(0, candidates.Length)];
    int factorIndex = (int)previousFactor;
    return factorDict[factorIndex];
}

旁注:您可能还应该让enum为字典编制索引:

private readonly Dictionary<DirectionFactor, Vector3> factorDict = 
        new Dictionary<DirectionFactor, Vector3>(4)
{
    {DirectionFactor.Back, new Vector3(0, 0, -tilesOffset)},
    {DirectionFactor.Left, new Vector3(-tilesOffset, 0, 0)},
    {DirectionFactor.Forward, new Vector3(0, 0, tilesOffset)},
    {DirectionFactor.Right, new Vector3(tilesOffset, 0, 0)}
};

case float n when(n my bad
0.1
应该是
1
case float n when)的意思是什么(n您是否有意将局部变量命名为与类字段相同的
factor
?您设置了局部变量,但似乎您正在尝试设置类字段。我不太清楚您的函数在做什么,但创建一个基于因子的函数来实现它不是很简单吗请记住,但是您正在获取when子句中的值。如果我在您的函数目标下,我可能会进一步帮助您,比如为什么
因子
是0f到1f的范围而不是其余的范围?而且,
因子
2、3和
else
看起来是相同的。如果是这样,为什么不删除
因子
2和3呢注意,它们里面没有使用因子吗?我使用因子变量为特定的开关情况分配特定的数字。但是这会让人痛苦,因为在其他情况下,你必须一遍又一遍地分配变量。这就是我决定使用字典将特定的因子数分配给特定的向量的原因。3它更容易实现,从而使变量因子变得无用。但是我发现自己做的事情和以前一样。我正在寻找更好的方法来实现它。
case float n when(n我的坏
0.1
应该是
1
case float n when(n您是否有意将局部变量命名为与类字段相同的
factor
?您设置了局部变量,但似乎您正在尝试设置类字段。我不太清楚您的函数在做什么,但创建一个基于因子的函数来实现它不是很简单吗请记住,但是您正在获取when子句中的值。如果我在您的函数目标下,我可能会进一步帮助您,比如为什么
因子
是0f到1f的范围而不是其余的范围?而且,
因子
2、3和
else
看起来是相同的。如果是这样,为什么不删除
因子
2和3呢注意,它们里面没有使用因子吗?我使用因子变量为特定的开关情况分配特定的数字。但是这会让人痛苦,因为在其他情况下,你必须一遍又一遍地分配变量。这就是我决定使用字典将特定的因子数分配给特定的向量的原因。3它更容易实现,从而使variabel factor变得无用。然而,我发现自己正在做与以前相同的事情。我正在寻找更好的方法来实现这一点,这正是我所寻找的!我以前听说过enum,但这是我第一次使用它谢谢!正是我所寻找的!我以前听说过enum,但这是我第一次使用它,谢谢!