Пример #1
0
    // 添加铁轨物件,给火车待行驶的铁轨队列入队
    public bool PutRail(GameObject rail)
    {
        if (TrainDamage)
        {
            return(false);
        }

        if (!RailPut)
        {
            return(false);
        }

        Vector3 new_pos = Rail_preview.position;
        int     x = (int)new_pos.x, z = (int)new_pos.z;

        RailsManager.TurnDirect turnDirect;
        if (railsManager.TryPut(rail, new_pos, !trainManager.MoveToLast, out turnDirect)) //尝试铺设铁轨
        {
            rail.layer            = 0;
            Rail_preview.position = new Vector3(x, -1, z);

            // 根据转向判断是否需要更新队尾元素
            if (turnDirect == RailsManager.TurnDirect.Left || turnDirect == RailsManager.TurnDirect.Right)
            {
                trainManager.UpdateLastRail((TrainManager.TurnDirect)turnDirect);
            }
            // 入队新的铁轨
            trainManager.AddRail(new_pos, TrainManager.TurnDirect.Straight);

            // 除草
            if (obstacles[x, z] == Obstacle.grass)
            {
                Transform grass = GameObject.Find("Environment").transform.Find("Grasses").Find("Grass_" + x + "_" + z);
                Destroy(grass.gameObject);
                obstacles[x, z] = Obstacle.empty;
            }
            obstacles[x, z] = Obstacle.rail;

            int end = railsManager.AdjacentToEnd(new_pos); //判断是否邻近终点铁轨
            if (end != -1)                                 // end为0代表邻近下标为0的终点铁轨,为1代表邻近下标为1的终点铁轨,为-1代表不邻近终点铁轨
            {
                RailsManager.TurnDirect[] turnDirects;
                railsManager.ConnectToEnd(end, out turnDirects);

                Vector3 ep0 = railsManager.EndRail[end].pos, ep1 = railsManager.EndRail[(end + 1) % 2].pos;

                if (turnDirects[0] == RailsManager.TurnDirect.Left || turnDirects[0] == RailsManager.TurnDirect.Right)
                {
                    trainManager.UpdateLastRail((TrainManager.TurnDirect)turnDirects[0]);
                }

                trainManager.AddRail(ep0, (TrainManager.TurnDirect)turnDirects[1]);
                trainManager.AddRail(ep1, TrainManager.TurnDirect.Straight);

                print("All rails have been laid.");
            }

            return(true);
        }
        else
        {
            return(false);
        }
    }