示例#1
0
    private void PlaceRoad()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (_CurrentRoad == null)
            {
                _CurrentRoad      = IsTargetingRoadEnd();
                _CurrentCrossRoad = IsTargetingCrossRoad();

                if (_CurrentRoad)
                {
                    _RoadEnd = _CurrentRoad.GetClickedRoadEnd(Camera.main.ScreenToWorldPoint(_CurrentMousePos));
                    _CurrentRoad.DisableColliders();
                }
                else if (_CurrentCrossRoad)
                {
                    _CurrentCrossRoad.GetComponent <CircleCollider2D>().enabled = false;
                }
                else
                {
                    GameObject road = Instantiate(_RoadBP[_LaneCount]);
                    _CurrentRoad = road.GetComponentInChildren <Road>();

                    Vector2[] points = new Vector2[2] {
                        Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero
                    };
                    _CurrentRoad.endpoints = points;

                    _RoadEnd = 1;
                }
            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            Road      otherRoad = IsTargetingRoadEnd();
            Crossroad crossroad = IsTargetingCrossRoad();

            if (_CurrentRoad)
            {
                _CurrentRoad.EnableColliders();
            }
            if (_CurrentCrossRoad)
            {
                _CurrentCrossRoad.GetComponent <CircleCollider2D>().enabled = true;
            }

            if (otherRoad && !_CurrentCrossRoad)
            {
                _CurrentRoad.ConnectRoad(otherRoad, otherRoad.GetClickedRoadEnd(Camera.main.ScreenToWorldPoint(_CurrentMousePos)), _RoadEnd);
            }
            if (crossroad && !_CurrentCrossRoad && _CurrentRoad)
            {
                _CurrentRoad.ConnectRoad(crossroad, _RoadEnd);
            }

            _CurrentRoad      = null;
            _CurrentCrossRoad = null;
        }

        if (Input.GetMouseButtonDown(1) && !_CurrentRoad)
        {
            _CurrentRoad = IsTargetingRoadEnd();
            if (!_CurrentRoad)
            {
                _CurrentRoad = IsTargetingRoadMesh();
            }
            if (_CurrentRoad)
            {
                _CurrentRoad.DisconnectEndPoints();
                Destroy(_CurrentRoad.gameObject);
                _CurrentRoad = null;
            }
        }
    }