Пример #1
0
    // Road
    public void BuildRoadBetween(Station a, Station b)
    {
        bool valid =
            a != b &&
            a.RoadList.Count < a.RoadLimits &&
            b.RoadList.Count < b.RoadLimits &&
            Vector2.Distance(a.Pos, b.Pos) < GameMaster.RoadMaxLength;

        if (!valid)
        {
            return;
        }

        // Check overlapping
        foreach (var road in a.RoadList)
        {
            valid &= road.Next(a) != b;
        }
        foreach (var road in b.RoadList)
        {
            valid &= road.Next(b) != a;
        }

        if (valid)
        {
            Vector2 pos = (a.Pos + b.Pos) / 2.0f;
            Debug.Log("Build road between " + a.ID + ", " + b.ID);

            GameObject     newRoad    = Instantiate(RoadPrefab, pos, Quaternion.identity, RoadGroup.transform);
            RoadController controller = newRoad.GetComponent <RoadController>();
            Road           road       = controller.Initialize();
            road.Connect(a, b);

            RoadList.Add(road);

            // Build road line renderer
            LineRenderer lr = newRoad.GetComponent <LineRenderer>();
            lr.useWorldSpace = true;

            Vector2 diffVec = b.Pos - a.Pos;
            if (diffVec.magnitude > 1.0f)
            {
                diffVec -= 0.5f * diffVec.normalized;
            }
            int segments = Mathf.Max(Mathf.CeilToInt(diffVec.magnitude / 0.15f), 18);
            lr.positionCount = segments + 1;
            for (int i = 0; i <= segments; i++)
            {
                Vector3 newPointPos = new Vector3(0.0f, 0.0f, -road.Height * Mathf.Sin(i * Mathf.PI / segments))
                                      + i * (Vector3)diffVec / segments + (Vector3)a.Pos + 0.25f * (Vector3)diffVec.normalized;
                lr.SetPosition(i, newPointPos);
            }

            //TODO: Manage animator
            MoneyLeft -= GameMaster.Instance.RoadCost;
            UpdateNavigation();
        }
        else
        {
            //TODO: Build error
            Debug.LogWarning("Road is invalid");
        }
    }