private void RotateByType(RoadPictureItem rpi, RoadManager.RoadType type)
    {
        SpriteRenderer spr = rpi.GetComponent <SpriteRenderer>();

        switch (type)
        {
        case RoadManager.RoadType.UpToRight:
        {
            if (spr.flipX)
            {
                spr.flipX = false;
            }
            if (spr.flipY)
            {
                spr.flipY = false;
            }
        }
        break;

        case RoadManager.RoadType.RightToDown:
        {
            if (!spr.flipX)
            {
                spr.flipX = true;
            }
            if (spr.flipY)
            {
                spr.flipY = false;
            }
        }
        break;

        case RoadManager.RoadType.DownToLeft:
        {
            if (!spr.flipX)
            {
                spr.flipX = true;
            }
            if (!spr.flipY)
            {
                spr.flipY = true;
            }
        }
        break;

        case RoadManager.RoadType.LeftToUp:
        {
            if (spr.flipX)
            {
                spr.flipX = false;
            }
            if (!spr.flipY)
            {
                spr.flipY = true;
            }
        }
        break;
        }
    }
    public RoadPictureItem GetSuitablePicture(RoadManager.RoadType type, RoadManager.RoadType prevType, RoadManager.BuildState buildState, Vector3 position, bool isRightDirection)
    {
        RoadPictureItem rpi = GetCachedObject(buildState, isRightDirection, type, prevType);

        if (rpi == null)
        {
            rpi = GetCachedObject(buildState, isRightDirection, type, prevType, true);
        }
        rpi.transform.position = position;
        RotateByType(rpi, type);
        return(rpi);
    }
    private void CreateNextRoad()
    {
        int[] pathVert            = build.GetIndexArr();
        RoadManager.RoadType type = RoadManager.RoadType.m_null;
        Quaternion           angle;

        for (int i = 1; i < pathVert.Length; i++)
        {
            if (i + 1 == pathVert.Length)
            {
                break;
            }
            int     index              = pathVert[i];
            int     index0             = pathVert[i - 1];
            int     index1             = pathVert[i + 1];
            Vector2 pos                = build.GetVert(index).pos;
            Vector2 pos0               = build.GetVert(index0).pos;
            Vector2 pos1               = build.GetVert(index1).pos;
            Vector2 Relative1          = build.GetRealativePosition(pos, pos0);
            Vector2 Relative2          = build.GetRealativePosition(pos, pos1);
            Vector2 ResultantDirection = Relative1 + Relative2;
            type  = roadManager.GetRoadType(ResultantDirection);
            angle = roadManager.GetRoadDirection(ResultantDirection, pos, pos0);
            switch (type)
            {
            case RoadManager.RoadType.roadstraight: roads[i].m_GameObjectPrefabType = PrefavStrRoad; break;

            case RoadManager.RoadType.roadTurn: roads[i].m_GameObjectPrefabType = PrefavTurnRoad; break;
            }
            roads[i].pos = pos;
            roads[i].m_GameObjectPrefabType.transform.position = pos * inGameUnitsSize;
            roads[i].m_GameObjectPrefabType.transform.rotation = angle;
            roads[i].m_GameObjectInstance = Instantiate(roads[i].m_GameObjectPrefabType);
            //Debug.Log(ResultantDirection);
        }
        //Create Finsih Road
        int ind = pathVert.Length - 1;

        roads[ind].pos = build.GetLastVert().pos;
        roads[ind].m_GameObjectPrefabType = PrefavRoadFinish;
        roads[ind].m_GameObjectPrefabType.transform.position = roads[ind].pos * inGameUnitsSize;
        roads[ind].m_GameObjectPrefabType.transform.rotation = roadManager.GetRoadDirection(Vector2.zero, roads[ind].pos, roads[ind - 1].pos);
        FinishRoad = roads[ind].m_GameObjectInstance = Instantiate(roads[ind].m_GameObjectPrefabType);
    }
    private RoadPictureItem GetCachedObject(RoadManager.BuildState buildState, bool isRightDirection, RoadManager.RoadType type, RoadManager.RoadType prevType, bool isCreate = false)
    {
        GameObject      foundedGo = null;
        RoadPictureItem rpi;

        foreach (GameObject go in _cachedObjects)
        {
            if ((!go.activeSelf) || (isCreate))
            {
                rpi = go.GetComponent <RoadPictureItem>();
                if ((buildState == RoadManager.BuildState.BuildFirstFloor) ||
                    (buildState == RoadManager.BuildState.BuildSecondFloor))
                {
                    if (rpi.BuildState == buildState)
                    {
                        if (!go.activeSelf)
                        {
                            foundedGo = go;
                            break;
                        }
                        else
                        {
                            foundedGo = CreateNewPicture(go);
                            break;
                        }
                    }
                }
                else
                if ((rpi.BuildState == RoadManager.BuildState.BuildClimbItem) ||
                    (rpi.BuildState == RoadManager.BuildState.BuildDescentItem))
                {
                    if (((prevType == RoadManager.RoadType.LeftToUp) && (type == RoadManager.RoadType.UpToRight)) ||
                        ((prevType == RoadManager.RoadType.UpToRight) && (type == RoadManager.RoadType.LeftToUp)) ||
                        ((prevType == RoadManager.RoadType.RightToDown) && (type == RoadManager.RoadType.DownToLeft)) ||
                        ((prevType == RoadManager.RoadType.DownToLeft) && (type == RoadManager.RoadType.RightToDown))

                        || ((prevType == RoadManager.RoadType.RightToDown) && (type == RoadManager.RoadType.LeftToUp) && isRightDirection) ||
                        ((prevType == RoadManager.RoadType.LeftToUp) && (type == RoadManager.RoadType.RightToDown) && isRightDirection) ||
                        ((prevType == RoadManager.RoadType.UpToRight) && (type == RoadManager.RoadType.DownToLeft) && !isRightDirection) ||
                        ((prevType == RoadManager.RoadType.DownToLeft) && (type == RoadManager.RoadType.UpToRight) && !isRightDirection)
                        )
                    {
                        if (rpi.BuildState == buildState)
                        {
                            if (!go.activeSelf)
                            {
                                foundedGo = go;
                                break;
                            }
                            else
                            {
                                foundedGo = CreateNewPicture(go);
                                break;
                            }
                        }
                    }
                    else
                    {
                        if (rpi.BuildState != buildState)
                        {
                            if (!go.activeSelf)
                            {
                                foundedGo = go;
                                break;
                            }
                            else
                            {
                                foundedGo = CreateNewPicture(go);
                                break;
                            }
                        }
                    }
                }
            }
        }

        if (foundedGo)
        {
            foundedGo.SetActive(true);
            rpi = foundedGo.GetComponent <RoadPictureItem>();
            return(rpi);
        }

        return(null);
    }
Example #5
0
 public void SetStartRoadType(RoadManager.RoadType type)
 {
     _startType = type;
 }