コード例 #1
0
ファイル: GUISystem.cs プロジェクト: bpgabin/red-rover
    void BaseGUI()
    {
        GUI.depth = 3;
        GUI.skin  = Ourskin;
        // Track each interactive button's Rect
        Dictionary <ButtonType, Rect> rects = new Dictionary <ButtonType, Rect>();
        Rect dropRect = new Rect(0, 0, 0, 0);

        List <Rect> actionRects = new List <Rect>();

        // Draw Background
        //GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), Resources.Load("Textures/marsbackground_01") as Texture);
        switch (currentBase.baseNumber)
        {
        case MarsBase.BaseNumber.baseOne:
            GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), Resources.Load("Textures/level1") as Texture);
            break;

        case MarsBase.BaseNumber.baseTwo:
            GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), Resources.Load("Textures/level2") as Texture);
            break;

        case MarsBase.BaseNumber.baseThree:
            GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), Resources.Load("Textures/level3") as Texture);
            break;

        case MarsBase.BaseNumber.baseFour:
            GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), Resources.Load("Textures/level4") as Texture);
            break;

        case MarsBase.BaseNumber.baseFive:
            GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), Resources.Load("Textures/level5") as Texture);
            break;
        }

        // Context Panel
        if (currentBase.selectedRover == null)
        {
            string title = "";
            switch (currentBase.baseNumber)
            {
            case MarsBase.BaseNumber.baseOne:
                title = "alpha";
                break;

            case MarsBase.BaseNumber.baseTwo:
                title = "beta";
                break;

            case MarsBase.BaseNumber.baseThree:
                title = "gamma";
                break;

            case MarsBase.BaseNumber.baseFour:
                title = "delta";
                break;

            case MarsBase.BaseNumber.baseFive:
                title = "epsilon";
                break;
            }
            GUI.Box(new Rect(Screen.width - 260, 10, 250, 520), "mining base " + title);
            rects[ButtonType.rover] = new Rect(Screen.width - 260 + 250 / 2 - 15 + 30, 30 + 264 + 100, 30, 30);

            GUI.skin = null;
            GUI.Box(new Rect(Screen.width - 250, 50, 230, 100), GUIContent.none);
            GUI.Box(new Rect(Screen.width - 250, 170, 230, 130), GUIContent.none);
            GUI.Box(new Rect(Screen.width - 250, 330, 230, 120), GUIContent.none);
            GUI.skin = Ourskin;
            //GUI.Label(new Rect(Screen.width - 250 + 115 - 25, 55, 80, 30), "stats");
            GUI.Label(new Rect(Screen.width - 250 + 40, 58, 220, 30), "IRON: " + colonyManager.iron.ToString());
            GUI.Label(new Rect(Screen.width - 250 + 40, 78, 220, 30), "MONEY: $" + colonyManager.money.ToString());
            GUI.Label(new Rect(Screen.width - 250 + 40, 98, 220, 30), "IRON SOLD: " + colonyManager.ironSoldSinceAudit.ToString());
            GUI.Label(new Rect(Screen.width - 250 + 40, 118, 240, 30), "IRON GOAL: " + colonyManager.auditGoal.ToString());

            if (currentBase.selectedBuilding == null)
            {
                GUI.Label(new Rect(Screen.width - 250 + 30, 360, 300, 60), "buy rover\n$" + colonyManager.costs[ColonyManager.ShopItems.rover].ToString());
                if (colonyManager.money < colonyManager.costs[ColonyManager.ShopItems.rover])
                {
                    GUI.color = new Color(1f, 1f, 1f, 0.5f);
                }
                GUI.DrawTexture(rects[ButtonType.rover], buttonTextures[ButtonType.rover]);
                GUI.color = Color.white;
            }
            else
            {
                if (currentBase.selectedBuilding.buildingType == MarsBase.Building.BuildingType.mine)
                {
                    Texture drillTex;
                    if (currentBase.selectedBuilding.buildingLevel != 3)
                    {
                        drillTex = drillTextures[currentBase.selectedBuilding.buildingLevel + 1];
                    }
                    else
                    {
                        drillTex = drillTextures[currentBase.selectedBuilding.buildingLevel];
                    }
                    Rect bRect = new Rect(Screen.width - 245, 335, drillTex.width, drillTex.height);
                    GUI.DrawTexture(bRect, drillTex);
                }
                else if (currentBase.selectedBuilding.buildingType == MarsBase.Building.BuildingType.processingPlant)
                {
                    Texture refineryTex;
                    if (currentBase.selectedBuilding.buildingLevel != 3)
                    {
                        refineryTex = refineryTextures[currentBase.selectedBuilding.buildingLevel + 1];
                    }
                    else
                    {
                        refineryTex = refineryTextures[currentBase.selectedBuilding.buildingLevel];
                    }
                    Rect bRect = new Rect(Screen.width - 235, 350, refineryTex.width, refineryTex.height);
                    GUI.DrawTexture(bRect, refineryTex);
                }
                int cost = 0;
                if (currentBase.selectedBuilding.buildingLevel == 1)
                {
                    cost = colonyManager.costs[ColonyManager.ShopItems.firstUpgrade];
                }
                else if (currentBase.selectedBuilding.buildingLevel == 2)
                {
                    cost = colonyManager.costs[ColonyManager.ShopItems.secondUpgrade];
                }

                GUI.Label(new Rect(Screen.width - 90, 390, 80, 30), ((cost == 0) ? "" : "$" + cost.ToString()));

                GUI.enabled = (currentBase.selectedBuilding.buildingLevel < 3 && colonyManager.money >= cost);
                if (GUI.Button(new Rect(Screen.width - 110, 420, 84, 24), "Upgrade"))
                {
                    currentBase.selectedBuilding.buildingLevel++;
                }
                GUI.enabled = true;
            }

            GUI.Box(new Rect(Screen.width - 260 + 25, 200, 200, 40), GUIContent.none);
            GUI.Box(new Rect(Screen.width - 260 + 25, 250, 200, 40), GUIContent.none);
            GUI.Label(new Rect(Screen.width - 250 + 115 - 25, 170, 80, 30), "timers");

            GUI.color = Color.Lerp(Color.red, Color.green, (colonyManager.currentSpaceElevatorTime / colonyManager.spaceElevatorTime));
            GUI.Box(new Rect(Screen.width - 260 + 25 + 6, 200 + 6, (colonyManager.currentSpaceElevatorTime / colonyManager.spaceElevatorTime) * 188, 28), GUIContent.none, "ProgressBar");
            GUI.color = Color.white;
            GUI.Label(new Rect(Screen.width - 260 + 76, 208, 125, 40), "trade ship");

            GUI.color = Color.Lerp(Color.red, Color.green, (colonyManager.currentAuditTime / colonyManager.auditTime));
            GUI.Box(new Rect(Screen.width - 260 + 25 + 6, 250 + 6, (colonyManager.currentAuditTime / colonyManager.auditTime) * 188, 28), GUIContent.none, "ProgressBar");
            GUI.color = Color.white;
            GUI.Label(new Rect(Screen.width - 260 + 104, 258, 125, 40), "audit");
        }
        else
        {
            GUI.Box(new Rect(Screen.width - 260, 10, 250, 520), "PROGRAMMING");
            int size = 36, distance = 1, offset = 15, start = 260, yPos = 33;
            rects[ButtonType.arrowUp]    = new Rect(Screen.width - start + offset, yPos, size, size);
            rects[ButtonType.arrowLeft]  = new Rect(Screen.width - start + offset + size * 1 + distance * 1, yPos, size, size);
            rects[ButtonType.arrowRight] = new Rect(Screen.width - start + offset + size * 2 + distance * 2, yPos, size, size);
            rects[ButtonType.grab]       = new Rect(Screen.width - start + offset + size * 3 + distance * 3, yPos, size, size);
            rects[ButtonType.drop]       = new Rect(Screen.width - start + offset + size * 4 + distance * 4, yPos, size, size);
            rects[ButtonType.blank]      = new Rect(Screen.width - start + offset + size * 5 + distance * 5, yPos, size, size);

            foreach (KeyValuePair <ButtonType, Rect> entry in rects)
            {
                GUI.DrawTexture(entry.Value, buttonTextures[entry.Key]);
            }

            dropRect = new Rect(Screen.width - 250, 80, 230, 410);
            GUI.skin = null;
            GUI.Box(dropRect, GUIContent.none);
            GUI.skin = Ourskin;

            GUILayout.BeginArea(new Rect(Screen.width - 235, 495, 200, 40));
            GUILayout.BeginHorizontal();
            Ourskin.GetStyle("Button").fontSize = 14;
            GUI.enabled = (!currentBase.running && !currentBase.crashed);
            if (GUILayout.Button("rotate"))
            {
                switch (currentBase.selectedRover.direction)
                {
                case MarsBase.Direction.north:
                    currentBase.selectedRover.direction      = MarsBase.Direction.west;
                    currentBase.selectedRover.startDirection = MarsBase.Direction.west;
                    break;

                case MarsBase.Direction.east:
                    currentBase.selectedRover.direction      = MarsBase.Direction.north;
                    currentBase.selectedRover.startDirection = MarsBase.Direction.north;
                    break;

                case MarsBase.Direction.west:
                    currentBase.selectedRover.direction      = MarsBase.Direction.south;
                    currentBase.selectedRover.startDirection = MarsBase.Direction.south;
                    break;

                case MarsBase.Direction.south:
                    currentBase.selectedRover.direction      = MarsBase.Direction.east;
                    currentBase.selectedRover.startDirection = MarsBase.Direction.east;
                    break;
                }
                audio.PlayOneShot(buttonPressSound);
            }
            GUI.enabled = currentBase.selectedRover.actionsSize > 0 && !currentBase.running && !currentBase.crashed;
            if (GUILayout.Button("clear"))
            {
                currentBase.selectedRover.ClearActions();
                audio.PlayOneShot(buttonPressSound);
            }
            GUI.enabled = true;
            GUI.enabled = (!currentBase.running && !currentBase.crashed);
            if (GUILayout.Button("sell"))
            {
                colonyManager.SellItem(ColonyManager.SellItems.rover);
                currentBase.RemoveRover(currentBase.selectedRover);
                currentBase.selectedRover = null;
                audio.PlayOneShot(buttonPressSound);
                return;
            }
            GUI.enabled = true;
            Ourskin.GetStyle("Button").fontSize = 0;
            GUILayout.EndHorizontal();
            GUILayout.EndArea();

            // Draw Selected Rover's Actions
            if (currentBase.selectedRover.actionsSize > 0)
            {
                ReadOnlyCollection <MarsBase.Rover.ActionType> actions = currentBase.selectedRover.actions;
                GUILayout.BeginArea(new Rect(Screen.width - 245, 85, 230, 400));
                if (actions.Count > 45)
                {
                    scrollPosition = GUI.BeginScrollView(new Rect(0, 0, 235, 410), scrollPosition, new Rect(0, 0, 215, 410 + ((actions.Count - 41) / 5) * 45));
                }
                int colPos         = 0;
                int rowPos         = 0;
                int actionDistance = 5;
                int actionSize     = 40;

                for (int i = 0; i < actions.Count; i++)
                {
                    Rect newActionRect = new Rect((actionSize + actionDistance) * colPos, (actionSize + actionDistance) * rowPos, actionSize, actionSize);
                    actionRects.Add(new Rect((actionSize + actionDistance) * colPos + (Screen.width - 245), (actionSize + actionDistance) * rowPos + 85, actionSize, actionSize));
                    if (i == currentBase.selectedRover.currentActionIndex)
                    {
                        if (currentBase.selectedRover.crashed)
                        {
                            GUI.color = Color.red;
                        }
                        else if (currentBase.running)
                        {
                            GUI.color = Color.green;
                        }
                    }
                    GUI.DrawTexture(newActionRect, buttonTextures[actionToButton[actions[i]]]);
                    GUI.color = Color.white;
                    colPos++;
                    if (colPos == 5)
                    {
                        colPos = 0;
                        rowPos++;
                    }
                }
                if (actions.Count > 45)
                {
                    GUI.EndScrollView();
                }
                GUILayout.EndArea();
            }
        }

        // Buttons to change current scene or UI
        GUILayout.BeginArea(new Rect(Screen.width - 260, Screen.height - 64, 250, 30));
        GUILayout.BeginHorizontal();
        if (GUILayout.Button("Main Base", GUILayout.Width(120)))
        {
            currentBase = null;
            currentGUI  = ColonyGUI;
            audio.PlayOneShot(buttonPressSound);
            return;
        }
        if (GUILayout.Button("Menu", GUILayout.Width(120)))
        {
            Time.timeScale = 0;
            pausedGUI      = currentGUI;
            currentGUI     = PauseMenuGUI;
            audio.PlayOneShot(buttonPressSound);
        }
        GUILayout.EndHorizontal();
        GUILayout.EndArea();

        GUILayout.BeginArea(new Rect(Screen.width - 260 + 125 - 50, Screen.height - 32, 100, 30));
        if (!currentBase.running && !currentBase.crashed)
        {
            GUI.enabled = !currentBase.crashed;
            if (GUILayout.Button("start"))
            {
                currentBase.StartSim();
                if (!colonyManager.running)
                {
                    colonyManager.StartSim();
                }
                audio.PlayOneShot(buttonPressSound);
            }
            GUI.enabled = true;
        }
        else if (currentBase.running)
        {
            if (GUILayout.Button("stop"))
            {
                currentBase.StopSim();
                currentBase.ResetBoard();
                audio.PlayOneShot(buttonPressSound);
            }
        }
        else
        {
            if (GUILayout.Button("reset"))
            {
                currentBase.ResetBoard();
                audio.PlayOneShot(buttonPressSound);
            }
        }
        GUILayout.EndArea();

        // Draw Board
        DrawBase();

        // Handle Custom Interaction
        if (!currentBase.running)
        {
            bool crashCheck = true;
            if (currentBase.selectedRover != null)
            {
                crashCheck = !currentBase.selectedRover.crashed;
            }
            if (crashCheck)
            {
                if (!dragging && Event.current.type == EventType.mouseDown)
                {
                    // Start to drag buy rover
                    foreach (KeyValuePair <ButtonType, Rect> entry in rects)
                    {
                        if (entry.Value.Contains(Event.current.mousePosition))
                        {
                            if (entry.Key == ButtonType.rover)
                            {
                                if (colonyManager.money >= colonyManager.costs[ColonyManager.ShopItems.rover])
                                {
                                    dragging       = true;
                                    draggingButton = entry.Key;
                                    break;
                                }
                            }
                            else
                            {
                                dragging       = true;
                                draggingButton = entry.Key;
                                break;
                            }
                        }
                    }
                    // Remove Actions
                    if (currentBase.selectedRover != null)
                    {
                        for (int i = 0; i < actionRects.Count; i++)
                        {
                            if (actionRects[i].Contains(Event.current.mousePosition))
                            {
                                currentBase.selectedRover.RemoveAction(i);
                                audio.PlayOneShot(dragClick);
                                break;
                            }
                        }
                    }
                }
                else if (dragging && Event.current.type == EventType.mouseUp)
                {
                    dragging = false;
                    if (currentBase.selectedRover != null)
                    {
                        bool missedActions = true;
                        for (int i = 0; i < actionRects.Count; i++)
                        {
                            if (actionRects[i].Contains(Event.current.mousePosition))
                            {
                                currentBase.selectedRover.AddActionAt(buttonToAction[draggingButton], i);
                                audio.PlayOneShot(dragClick);
                                missedActions = false;
                                break;
                            }
                        }
                        if (missedActions)
                        {
                            if (dropRect.Contains(Event.current.mousePosition))
                            {
                                currentBase.selectedRover.AddAction(buttonToAction[draggingButton]);
                                audio.PlayOneShot(dragClick);
                            }
                        }
                    }
                    else
                    {
                        // Drop the buy rover
                        Vector2 pos = Event.current.mousePosition;

                        int x = Mathf.FloorToInt(pos.x / 30);
                        int y = Mathf.FloorToInt(pos.y / 30) + 1;

                        if (x >= 0 && x < MarsBase.GRID_WIDTH && y >= 0 && y <= MarsBase.GRID_HEIGHT)
                        {
                            if (currentBase.board[x, MarsBase.GRID_HEIGHT - y].tileType == MarsBase.GridTile.TileType.open)
                            {
                                currentBase.BuyPart(draggingButton, x, MarsBase.GRID_HEIGHT - y);
                                // Assume it's a rover for now
                                colonyManager.BuyItem(ColonyManager.ShopItems.rover);
                                audio.PlayOneShot(dragClick);
                            }
                        }
                    }
                }
            }
        }

        // Draw Dragged Object
        if (dragging)
        {
            Vector2 mousePosition = Event.current.mousePosition;
            Rect    origRect      = rects[draggingButton];
            Rect    dragRect      = new Rect(mousePosition.x - origRect.width / 2, mousePosition.y - origRect.height / 2, origRect.width, origRect.height);
            GUI.DrawTexture(dragRect, buttonTextures[draggingButton]);
        }
    }
コード例 #2
0
ファイル: GUISystem.cs プロジェクト: bpgabin/red-rover
    void ColonyGUI()
    {
        GUI.skin = Ourskin;

        List <Rect> baseRects = new List <Rect>();

        // Draw Background
        GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), Resources.Load("Textures/marsbackground_02") as Texture);

        GUI.Box(new Rect(0, 0, 230, 100), GUIContent.none);
        GUI.Label(new Rect(0 + 40, 8, 220, 30), "IRON: " + colonyManager.iron.ToString());
        GUI.Label(new Rect(0 + 40, 28, 220, 30), "MONEY: $" + colonyManager.money.ToString());
        GUI.Label(new Rect(0 + 40, 48, 220, 30), "IRON SOLD: " + colonyManager.ironSoldSinceAudit.ToString());
        GUI.Label(new Rect(0 + 40, 68, 240, 30), "IRON GOAL: " + colonyManager.auditGoal.ToString());

        GUI.Box(new Rect(Screen.width - 230, 0, 230, 130), GUIContent.none);
        GUI.Box(new Rect(Screen.width - 230 + 15, 30, 200, 40), GUIContent.none);
        GUI.Box(new Rect(Screen.width - 230 + 15, 80, 200, 40), GUIContent.none);
        GUI.Label(new Rect(Screen.width - 230 + 115 - 25, 5, 80, 30), "timers");

        GUI.color = Color.Lerp(Color.red, Color.green, (colonyManager.currentSpaceElevatorTime / colonyManager.spaceElevatorTime));
        GUI.Box(new Rect(Screen.width - 230 + 15 + 6, 30 + 6, (colonyManager.currentSpaceElevatorTime / colonyManager.spaceElevatorTime) * 188, 28), GUIContent.none, "ProgressBar");
        GUI.color = Color.white;
        GUI.Label(new Rect(Screen.width - 230 + 66, 38, 125, 40), "trade ship");

        GUI.color = Color.Lerp(Color.red, Color.green, (colonyManager.currentAuditTime / colonyManager.auditTime));
        GUI.Box(new Rect(Screen.width - 230 + 15 + 6, 80 + 6, (colonyManager.currentAuditTime / colonyManager.auditTime) * 188, 28), GUIContent.none, "ProgressBar");
        GUI.color = Color.white;
        GUI.Label(new Rect(Screen.width - 230 + 94, 88, 125, 40), "audit");

        if (GUI.Button(new Rect(Screen.width - 50, Screen.height - 30, 50, 30), "Menu"))
        {
            Time.timeScale = 0;
            pausedGUI      = currentGUI;
            currentGUI     = PauseMenuGUI;
            audio.PlayOneShot(buttonPressSound);
        }

        GUI.DrawTexture(new Rect(0, -120, 960, 540), Resources.Load("Textures/spaceElevator") as Texture);

        Texture drillTexture = Resources.Load("Textures/bucketwheelexcavator_small") as Texture;

        baseRects.Add(new Rect(30, 230, drillTexture.width, drillTexture.height));
        baseRects.Add(new Rect(190, 350, drillTexture.width, drillTexture.height));
        baseRects.Add(new Rect(380, 360, drillTexture.width, drillTexture.height));
        baseRects.Add(new Rect(560, 350, drillTexture.width, drillTexture.height));
        baseRects.Add(new Rect(740, 230, drillTexture.width, drillTexture.height));

        for (int i = 0; i < baseRects.Count; i++)
        {
            if (i < colonyManager.bases.Count)
            {
                if (colonyManager.bases[i].running)
                {
                    GUI.color = Color.green;
                    GUI.Label(new Rect(baseRects[i].x + 60, baseRects[i].y + 100, 200, 80), "running");
                    GUI.DrawTexture(baseRects[i], drillTexture);
                }
                else
                {
                    GUI.color = Color.red;
                    GUI.Label(new Rect(baseRects[i].x + 60, baseRects[i].y + 100, 200, 80), "stopped");
                    GUI.DrawTexture(baseRects[i], drillTexture);
                }
            }
            else if (i == colonyManager.bases.Count)
            {
                GUI.color = Color.white;
                GUI.Label(new Rect(baseRects[i].x + 40, baseRects[i].y + 100, 200, 80), "click to buy\n$" + colonyManager.costs[ColonyManager.ShopItems.miningBase].ToString());
                GUI.color = new Color(1f, 1f, 1f, 0.5f);
                GUI.DrawTexture(baseRects[i], drillTexture);
            }
            else
            {
                GUI.color = new Color(1f, 1f, 1f, 0.5f);
                GUI.DrawTexture(baseRects[i], drillTexture);
            }
        }
        GUI.color = Color.white;

        if (Event.current.type == EventType.mouseDown)
        {
            for (int i = 0; i < baseRects.Count; i++)
            {
                if (baseRects[i].Contains(Event.current.mousePosition))
                {
                    if (i < colonyManager.bases.Count)
                    {
                        currentBase = colonyManager.bases[i];
                        currentGUI  = BaseGUI;
                        audio.PlayOneShot(buttonPressSound);
                    }
                    else if (i == colonyManager.bases.Count)
                    {
                        if (colonyManager.money >= colonyManager.costs[ColonyManager.ShopItems.miningBase])
                        {
                            colonyManager.AddBase();
                            audio.PlayOneShot(buttonPressSound);
                        }
                    }
                }
            }
        }
    }