走铁路的完整路径waypoints

Take the full path waypoints of a railway

我正在开发 Train Valley 游戏,就像 2D 一样。铁路的建设是不同的。我有一个网格,我可以把一条铁轨放在网格中的某个地方,这样铁路就建好了。我面临的主要问题是我不知道如何为火车开辟道路……每个正弦都有一个点,这个点我添加到一个 Vector3 类型列表中,但是如果我跳过一个单元格并且把 sine 放在那里,然后回到跳过的单元格,把 rail 放在那里,然后列表将不再排序。 我附上了代码,但它很多。一般来说,如果有人知道如何构建这条路径,火车一定会 运行 正确。

void Start()
{
    foreach (TrainCell cell in Grid)
        cell.gameObject.AddComponent<TrainCellMouseEvent>().MouseEvent += OnCellClickEvent;
    MouseEventProccessor.Instance.captureMouseMouveEvents = true;
    StartCoroutine(SpawnStations());
}
private void OnCellClickEvent(TrainCell target, MouseEventType type)
{
    if (type == MouseEventType.CLICK)
    {
        if (canDestroy)
        {
            if ((int)target.CurrentChildIndex != 0)
            {
                target.CurrentChild.GetComponent<PolygonCollider2D>().enabled = false;
                target.setCurrentChildIndex(0);
                Instantiate(BoomFX, target.transform.position, Quaternion.identity);
                target.used = false;
                WayPoints.Remove(target.transform.position);
                if (BuildDone != null)
                    BuildDone(price, false);
            }
            foreach (TrainCell cell in Grid)
            {
                if (!cell.underBuilding)
                {
                    if (cell.CurrentChildIndex == 0 && cell.used)
                        cell.used = false;
                }
            }
            return;
        }

        if (rail.SelectedRail == null || target.used || outOfGridBounds)
            return;

        int railIndex = (int)rail.SelectedRail.GetComponent<ObjectSequence>().CurrentChildIndex;
        target.setCurrentChildIndex(railIndex + 1);
        target.CurrentChild.GetComponent<PolygonCollider2D>().enabled = true;
        MouseEventProccessor.Instance.captureMouseMouveEvents = true;
        SpriteRenderer render = target.CurrentChild.GetComponent<SpriteRenderer>();
        render.color = Vector4.one;
        target.used = true;
        if (BuildDone != null)
            BuildDone(price, true);

        if (target.CurrentChild.transform.childCount == 0)
            WayPoints.Add(target.transform.position);
        else
        {
            for (int i = 0; i < target.CurrentChild.transform.childCount; i++)
                WayPoints.Add(target.CurrentChild.transform.GetChild(i).transform.position);
        }
    }
    else if (type == MouseEventType.OVER)
    {
        if (mainLevel.isHammer)
        {
            if (target.CurrentChildIndex != 0)
                target.CurrentChild.GetComponent<SpriteRenderer>().color = Color.red;
            return;
        }

        if (rail.SelectedRail == null || target.used)
            return;

        foreach (TrainCell cell in Grid)
        {
            if (cell.CurrentChildIndex != 0)
            {
                if (cell != target)
                {
                    if (cell.CurrentChild.GetComponent<SpriteRenderer>().color != Color.white && !cell.used && !cell.underBuilding)
                    {
                        if (cell.GetComponentInChildren<ObjectSequenceNumberDisplay>() != null)
                            Destroy(GetComponentInChildren<ObjectSequenceNumberDisplay>().gameObject);
                        cell.setCurrentChildIndex(0);
                    }
                }
            }
        }

        int railIndex = (int)rail.SelectedRail.GetComponent<ObjectSequence>().CurrentChildIndex;
        target.setCurrentChildIndex(railIndex + 1);
        RenderColor(target);
    }
    else if (type == MouseEventType.EXIT)
    {
        if (target.CurrentChildIndex != 0)
            target.CurrentChild.GetComponent<SpriteRenderer>().color = Color.white;
        if (shapePrice != null)
            Destroy(shapePrice.gameObject);
        if (target.used)
            return;
        target.setCurrentChildIndex(0);
    }
}

据我推测,根据您的代码,我将尝试根据以下陈述回答您的问题:

  • 你有一个生成火车的车站
  • 您根据铁路设计定义火车从车站出发的方向(左、右、上、下)

有了这些陈述,我提出以下解决方案:

  • 创建一个方法,以站点为原点,以该点为起始方向。这样你就开始沿着铁路行驶,首先沿着那个方向,然后在 4 个方向上搜索更多 rails,将它们全部添加到 Vector3 数组中。这样你就可以对数组进行排序。

示例:

private void SortedRailway(Vector2 origin, Vector2? direction = null, Vector2? lastDirection = null){
   if(direction.HasValue){
      if(origin + direction is TrainCell){
         WayPoints.add(origin + direction);
         SortedRailway(origin + direction, null, direction);
      }
      //(else will be used if direction (just used for the initial one) is wrong, so error there)
   }else{
      if(origin + Vector2.up is TrainCell && Vector2.up != lastDirection){
         WayPoints.add(origin + Vector2.up);
         SortedRailway(origin + Vector2.up, null, Vector2.down); //lastDirection is always the opposite to the matching one
      }
      //... (Repeat for all 4 directions. If none fits, then we asume is the end of the railway)
   }
}

希望对您有所帮助!