コード例 #1
0
ファイル: Town.cs プロジェクト: suspendmode/seaunity
    /// <summary>
    /// Draws the town's resources.
    /// </summary>

    void DrawResources(Town town, Rect rect, Vector2 size, bool surplus)
    {
        // Draw the resource grid
        List <Vector2> grid = UI.DrawGrid(rect, size.x, size.y, 4f, 4f, 4f, 4);

        int  index     = 0;
        Town otherTown = (mSelectedRoute == null) ? null : mSelectedRoute.GetConnectedTown(town);

        foreach (Vector2 pos in grid)
        {
            if (index >= town.mResources.Count)
            {
                break;
            }

            // Run through all resources until we find the one we can draw
            for (; index < town.mResources.Count; ++index)
            {
                ResourceEntry ent = town.mResources[index];

                if ((surplus && ent.production > 0) || (!surplus && ent.production < 0))
                {
                    float alpha    = 1f;
                    Rect  iconRect = new Rect(pos.x, pos.y, size.x, size.y);

                    // Positive resources that the city is producing can be dragged
                    bool canDrag = (ent.production > 0);

                    // Don't allow dragging of resources which the other town doesn't actually need
                    if (mSelectedRoute == null)
                    {
                        canDrag = false;
                    }
                    else if (otherTown != null)
                    {
                        // If the other town doesn't need this resource, don't allow it to be dragged
                        if (otherTown.resources[index].production >= 0)
                        {
                            canDrag = false;
                        }
                    }

                    // If the resource cannot be dragged and it's in the surplus section, make it transparent
                    if (!canDrag && surplus)
                    {
                        alpha = 0.5f;
                    }

                    // If we're currently dragging this resource, make it transparent
                    if (mDragResource == index)
                    {
                        alpha = 0.5f;
                    }

                    // Allow dragging of positive production resources
                    if (canDrag && mSelectedRoute != null && Input.GetMouseButtonDown(0) && UI.ContainsMouse(iconRect))
                    {
                        mDragResource = index;
                        mDragTown     = town;
                    }

                    TownResource tr = TownResources.Instance.Get(index);

                    if (tr.icon != null)
                    {
                        Color prev = GUI.color;
                        GUI.color = new Color(1f, 1f, 1f, prev.a * alpha);
                        UI.DrawTexture(pos.x, pos.y, tr.icon);
                        GUI.color = prev;
                    }
                    Tooltip.AddArea(iconRect, DrawTooltip, tr);

                    // Stockpile amount caption (bottom-right)
                    {
                        int   amount = Mathf.RoundToInt(ent.warehouse);
                        Color color  = Color.white;
                        if (amount > 0)
                        {
                            color = Color.green;
                        }
                        else if (amount < 0)
                        {
                            color = Color.red;
                        }
                        UI.DrawLabel(iconRect, amount.ToString(), Config.Instance.infoStyle, color, Color.black, true);
                    }

                    // Production caption (top-left)
                    {
                        int   amount = ent.production;
                        Color color  = Color.white;
                        if (amount > 0)
                        {
                            color = Color.green;
                        }
                        else if (amount < 0)
                        {
                            color = Color.red;
                        }

                        TextAnchor anchor = Config.Instance.infoStyle.alignment;
                        Config.Instance.infoStyle.alignment = TextAnchor.UpperLeft;
                        UI.DrawLabel(iconRect, (amount < 0) ? amount.ToString() : ("+" + amount),
                                     Config.Instance.descStyle, color, new Color(0f, 0f, 0f, 0.5f), true);
                        Config.Instance.infoStyle.alignment = anchor;
                    }
                    ++index;
                    break;
                }
            }
        }
    }