Example #1
0
    public void GenerateGrid(List <GridTrack> gridTracks, List <GridComponent> gridComponents)
    {
        /*
         * currentGridWidth = layoutList[0].Length;
         * currentGridHeight = layoutList.Count;
         */

        Vector3 averagePosition = new Vector3();

        //Debug.Log(gridTracks.Count + ", " + gridComponents.Count);

        foreach (GridTrack track in gridTracks)
        {
            Vector2 reversedYposition = track.position;
            reversedYposition.y = GameManager.Instance.GetLevelHeight() - reversedYposition.y;

            GameObject gridElementInstance = Instantiate(gridElementPrefab, reversedYposition, Quaternion.identity) as GameObject;

            SpriteRenderer     instanceSpriteRenderer = gridElementInstance.GetComponent <SpriteRenderer>();
            GridObjectBehavior behavior = gridElementInstance.GetComponent <GridObjectBehavior>();

            gridElementInstance.transform.SetParent(gridContainer);

            averagePosition += new Vector3(reversedYposition.x, reversedYposition.y, 0);

            gridElementInstance.name = track.type + "_track";

            behavior.behaviorType = GridObjectBehavior.BehaviorTypes.track;
            behavior.track        = track;

            switch (track.type)
            {
            case 'J':
                // Straight_00
                instanceSpriteRenderer.sprite = gridElementDictionary["straight_road"];
                gridElementInstance.transform.Rotate(0, 0, 90);

                break;

            case 'E':
                // Straight_01
                instanceSpriteRenderer.sprite = gridElementDictionary["straight_road"];
                break;

            case 'N':
                // ThreeWay_00
                instanceSpriteRenderer.sprite = gridElementDictionary["threeway_road"];
                gridElementInstance.transform.Rotate(0, 0, 90);
                break;

            case 'M':
                // ThreeWay_01
                instanceSpriteRenderer.sprite = gridElementDictionary["threeway_road"];
                gridElementInstance.transform.Rotate(0, 0, 180);
                break;

            case 'K':
                // ThreeWay_02
                instanceSpriteRenderer.sprite = gridElementDictionary["threeway_road"];
                gridElementInstance.transform.Rotate(0, 0, -90);
                break;

            case 'G':
                // ThreeWay_03
                instanceSpriteRenderer.sprite = gridElementDictionary["threeway_road"];
                break;

            case 'F':
                // Turn_00
                instanceSpriteRenderer.sprite = gridElementDictionary["corner_road"];
                gridElementInstance.transform.Rotate(0, 0, 90);
                break;

            case 'C':
                // Turn_01
                instanceSpriteRenderer.sprite = gridElementDictionary["corner_road"];

                break;

            case 'I':
                // Turn_02
                instanceSpriteRenderer.sprite = gridElementDictionary["corner_road"];
                gridElementInstance.transform.Rotate(0, 0, 270);
                break;

            case 'L':
                // Turn_03
                instanceSpriteRenderer.sprite = gridElementDictionary["corner_road"];
                gridElementInstance.transform.Rotate(0, 0, 180);
                break;

            case 'O':
                // FourWay
                instanceSpriteRenderer.sprite = gridElementDictionary["fourway_road"];
                gridElementInstance.transform.Rotate(0, 0, 180);
                break;

            /*A - EAST DEAD
             * B - NORTH DEAD
             * D - WEST DEAD
             * H - SOUTH DEAD*/

            ///* TODO: Enable Dead ends
            case 'A':
                instanceSpriteRenderer.sprite = gridElementDictionary["deadEnd_road"];
                gridElementInstance.transform.Rotate(0, 0, 90);
                break;

            case 'B':
                instanceSpriteRenderer.sprite = gridElementDictionary["deadEnd_road"];
                //gridElementInstance.transform.Rotate(0,0,180);
                break;

            case 'D':
                instanceSpriteRenderer.sprite = gridElementDictionary["deadEnd_road"];
                gridElementInstance.transform.Rotate(0, 0, -90);
                break;

            case 'H':
                instanceSpriteRenderer.sprite = gridElementDictionary["deadEnd_road"];
                gridElementInstance.transform.Rotate(0, 0, 180);
                break;

            //*/
            default:
                instanceSpriteRenderer.sprite = null;
                break;
            }
            instanceSpriteRenderer.color = new Color(0.2f, 0.2f, 0.2f, 1f);
            currentLevelObjects.Add(behavior);
        }

        averagePosition = averagePosition / gridTracks.Count;
        worldCamera.transform.position = new Vector3(averagePosition.x, averagePosition.y, -15);
        float levelheight = GameManager.Instance.GetLevelHeight();
        float levelRatio  = GameManager.Instance.GetLevelWidth() / GameManager.Instance.GetLevelHeight();

        if (levelRatio > 1.78f)           // 16:9
        {
            Debug.Log("WIDER LEVEL " + levelRatio);
            levelheight *= (levelRatio / 1.78f);
            levelheight /= 0.9f;             // To make room for the right banner
        }
        else
        {
            levelheight += 1;
        }
        worldCamera.orthographicSize = (levelheight / 2.0f) / 0.78f;     // To make room for the bottom banner

        foreach (GridComponent gridComponent in gridComponents)
        {
            int    color     = gridComponent.configuration.color;
            int    positionX = gridComponent.posX;
            int    positionY = GameManager.Instance.GetLevelHeight() - gridComponent.posY;
            string type      = gridComponent.type;

            GameObject gridElementInstance = Instantiate(gridElementPrefab, new Vector3(positionX, positionY), Quaternion.identity) as GameObject;

            SpriteRenderer instanceSpriteRenderer = gridElementInstance.GetComponent <SpriteRenderer>();
            instanceSpriteRenderer.sortingOrder = Constants.ComponentSortingOrder.basicComponents;
            //instanceSpriteRenderer.color = colorDictionary[color];

            GridObjectBehavior behavior = gridElementInstance.GetComponent <GridObjectBehavior>();
            if (type == "signal")
            {
                Signal_GridObjectBehavior signal_Behavior = gridElementInstance.AddComponent <Signal_GridObjectBehavior>();
                signal_Behavior.highlightObject = behavior.highlightObject;
                signal_Behavior.lineRenderer    = gridElementInstance.GetComponentInChildren <LineRenderer>();
                signal_Behavior.teleportTrail   = behavior.teleportTrail;
                signal_Behavior.lockObject      = behavior.lockObject;
                Destroy(behavior);
                behavior = signal_Behavior;
            }
            else if (type == "thread")
            {
                Thread_GridObjectBehavior thread_Behavior = gridElementInstance.AddComponent <Thread_GridObjectBehavior>();
                thread_Behavior.highlightObject = behavior.highlightObject;
                thread_Behavior.teleportTrail   = behavior.teleportTrail;
                Destroy(behavior);
                behavior = thread_Behavior;
            }
            else if (type == "diverter")
            {
                Diverter_GridObjectBehavior diverter_Behavior = gridElementInstance.AddComponent <Diverter_GridObjectBehavior>();
                diverter_Behavior.highlightObject = behavior.highlightObject;
                diverter_Behavior.teleportTrail   = behavior.teleportTrail;
                Destroy(behavior);
                behavior = diverter_Behavior;
                gridElementInstance.layer = 8;
            }
            else if (type == "delivery")
            {
                Delivery_GridObjectBehavior delivery_Behavior = gridElementInstance.AddComponent <Delivery_GridObjectBehavior>();
                delivery_Behavior.highlightObject = behavior.highlightObject;
                delivery_Behavior.teleportTrail   = behavior.teleportTrail;
                Destroy(behavior);
                behavior = delivery_Behavior;
            }

            gridElementInstance.transform.SetParent(gridContainer);

            gridElementInstance.name = type;
            behavior.behaviorType    = GridObjectBehavior.BehaviorTypes.component;
            behavior.component       = gridComponent;

            instanceSpriteRenderer.sprite = GetSprite(gridComponent);

            currentLevelObjects.Add(behavior);
            gridObjectLevelIDDictionary.Add(gridComponent.id, behavior);
            gridObjectLevelPositionDictionary.Add(behavior.transform.position, behavior);

            behavior.InitializeGridComponentBehavior();
        }
    }
Example #2
0
    public void PlaceGridElementAtLocation(Vector2 inputPosition, PlayerInteraction_GamePhaseBehavior.MenuOptions inputGridElement)
    {
        Vector3 testPosition = worldCamera.ScreenToWorldPoint(inputPosition);

        testPosition.z = 0;
        testPosition.x = Mathf.RoundToInt(testPosition.x);
        testPosition.y = Mathf.RoundToInt(testPosition.y);


        GridComponent c = new GridComponent();

        c.configuration = new Configuration();

        string type = "package_exchangepoint_00";

        switch (inputGridElement)
        {
        case PlayerInteraction_GamePhaseBehavior.MenuOptions.semaphore:
            type = "semaphore";
            break;

        case PlayerInteraction_GamePhaseBehavior.MenuOptions.button:
            type = "signal";
            break;
        }

        c.editable = "E";
        c.placedBy = "P";

        playerPlacedElementCount++;
        c.id = 9000 + playerPlacedElementCount;

        c.posX = (int)testPosition.x;
        c.posY = (int)testPosition.y;
        c.type = type;        //inputGridElement.ToString();
        c.configuration.link = -1;

        GameObject     gridElementInstance    = Instantiate(gridElementPrefab, testPosition, Quaternion.identity) as GameObject;
        SpriteRenderer instanceSpriteRenderer = gridElementInstance.GetComponent <SpriteRenderer>();

        instanceSpriteRenderer.sortingOrder = Constants.ComponentSortingOrder.basicComponents;

        instanceSpriteRenderer.sprite = GetSprite(c);

        gridElementInstance.transform.SetParent(gridContainer);

        //instanceSpriteRenderer.color = colorDictionary[color];
        GridObjectBehavior behavior = gridElementInstance.GetComponent <GridObjectBehavior>();

        if (type == "signal")
        {
            Signal_GridObjectBehavior signal_Behavior = gridElementInstance.AddComponent <Signal_GridObjectBehavior>();
            signal_Behavior.highlightObject = behavior.highlightObject;
            signal_Behavior.lineRenderer    = gridElementInstance.GetComponentInChildren <LineRenderer>();
            signal_Behavior.teleportTrail   = behavior.teleportTrail;
            Destroy(behavior);
            behavior = signal_Behavior;
        }

        behavior.component    = c;
        behavior.behaviorType = GridObjectBehavior.BehaviorTypes.component;

        Vector2 reverseYposition = new Vector2(c.posX, GameManager.Instance.GetLevelHeight() - c.posY);

        behavior.UpdateGridObjectPosition(reverseYposition);

        currentLevelObjects.Add(behavior);

        gridObjectLevelIDDictionary.Add(behavior.component.id, behavior);
        gridObjectLevelPositionDictionary.Add(behavior.transform.position, behavior);

        behavior.InitializeGridComponentBehavior();
    }