コード例 #1
0
    public bool CheckPartIntersectsWithShell(ShipBuilderPart shipPart)
    {
        // make sure calculations here are only with core1_shell
        // TODO: optimize by storing the sprite in a specific reference
        var oldSprite = shell.sprite;

        shell.sprite = ResourceManager.GetAsset <Sprite>("core1_shell");
        shell.rectTransform.sizeDelta = GetImageBoundsBySprite(shell);

        var shellRect = GetRect(shell.rectTransform);

        var partBounds = GetRect(shipPart.rectTransform);

        if (partBounds.Intersects(shellRect))
        {
            bool z = Mathf.Abs(shipPart.rectTransform.anchoredPosition.x - shell.rectTransform.anchoredPosition.x) <=
                     0.18F * (shipPart.rectTransform.sizeDelta.x + shell.rectTransform.sizeDelta.x) &&
                     Mathf.Abs(shipPart.rectTransform.anchoredPosition.y - shell.rectTransform.anchoredPosition.y) <=
                     0.18F * (shipPart.rectTransform.sizeDelta.y + shell.rectTransform.sizeDelta.y);
            shipPart.isInChain = !z;

            // reset sprite
            shell.sprite = oldSprite;
            shell.rectTransform.sizeDelta = GetImageBoundsBySprite(shell);
            return(z);
        }

        shell.sprite = oldSprite;
        shell.rectTransform.sizeDelta = GetImageBoundsBySprite(shell);
        return(false);
    }
コード例 #2
0
    public static bool CheckPartIntersectsWithBound(ShipBuilderPart part1, ShipBuilderPart part2)
    {
        var pb1 = GetRect(part1.rectTransform);
        var pb2 = GetRect(part2.rectTransform);

        return(pb1.Intersects(pb2));
    }
コード例 #3
0
    public override void OnPointerDown(PointerEventData eventData)
    {
        if (Input.GetKey(KeyCode.LeftShift))
        {
#if UNITY_EDITOR
#endif
        }

        if (count > 0)
        {
            var builderPart = InstantiatePart();
            DecrementCount();
            if (Input.GetKey(KeyCode.LeftShift))
            {
                if (mode == BuilderMode.Yard && cursor.builder.GetMode() == BuilderMode.Trader)
                {
                    cursor.builder.DispatchPart(builderPart, ShipBuilder.TransferMode.Sell);
                    return;
                }
                else if (mode == BuilderMode.Trader)
                {
                    cursor.buildCost += EntityBlueprint.GetPartValue(builderPart.info);
                    cursor.builder.DispatchPart(builderPart, ShipBuilder.TransferMode.Buy);
                    return;
                }
            }

            ShipBuilderPart symmetryPart = count > 0 && cursor.symmetryMode != ShipBuilderCursorScript.SymmetryMode.Off ? InstantiatePart() : null;
            if (symmetryPart)
            {
                //if(cursor.symmetryMode == ShipBuilderCursorScript.SymmetryMode.X)
                symmetryPart.info.mirrored = !builderPart.info.mirrored;
                if (cursor.symmetryMode == ShipBuilderCursorScript.SymmetryMode.Y)
                {
                    symmetryPart.info.rotation = 180;
                }
            }

            cursor.GrabPart(builderPart, symmetryPart);
            if (symmetryPart)
            {
                DecrementCount();
            }

            cursor.buildValue += EntityBlueprint.GetPartValue(part);
            if (symmetryPart)
            {
                cursor.buildValue += EntityBlueprint.GetPartValue(part);
            }

            if (mode == BuilderMode.Trader)
            {
                cursor.buildCost += EntityBlueprint.GetPartValue(part);
                if (symmetryPart)
                {
                    cursor.buildCost += EntityBlueprint.GetPartValue(part);
                }
            }
        }
    }
コード例 #4
0
    public void GrabPart(ShipBuilderPart part, ShipBuilderPart symmetryPart = null)
    {
        lastPart         = null;
        symmetryLastPart = null;
        if (parts.Contains(part))
        {
            parts.Remove(part);
            parts.Add(part);
            part.rectTransform.SetAsLastSibling();
        }

        // code to ensure part does not snap to cursor immediately
        var pos = part.GetComponent <RectTransform>().anchoredPosition;

        // if the initial position is zero that means this is a new part, don't set an offset
        if (pos == Vector2.zero)
        {
            offset = Vector2.zero;
        }
        else
        {
            offset = pos - GetComponent <RectTransform>().anchoredPosition;
        }
        currentPart         = part;
        symmetryCurrentPart = symmetryPart;
    }
コード例 #5
0
    public void UpdateCurrentPart()
    {
        if (currentPart)
        {
            currentPart.SetMaskable(false);
            var     oldLoc = currentPart.info.location;
            Vector2 newLoc;
            currentPart.info.location = newLoc = (GetComponent <RectTransform>().anchoredPosition + offset) / 100;
            if (symmetryCurrentPart)
            {
                symmetryCurrentPart.info.location = GetSymmetrizedVector(currentPart.info.location, symmetryMode);
            }
            if (Input.GetMouseButtonUp(0))
            {
                PlaceCurrentPart();
            }
            if (oldLoc != newLoc || Input.GetMouseButtonUp(0))
            {
                builder.UpdateChain();
            }
        }
        else if (Input.GetMouseButtonDown(0))
        {
            grid2lastPos  = grid.anchoredPosition;
            grid2mousePos = Input.mousePosition;
            lastPart      = null;

            var vecPos = GetComponent <RectTransform>().anchoredPosition;
            var part   = FindPart(vecPos, null);
            // check for symmetry mode and grab parts accordingly
            if (part)
            {
                if (symmetryMode != SymmetryMode.Off)
                {
                    var symmetryPart = FindPart(GetSymmetrizedVector(vecPos, symmetryMode), part);
                    if (symmetryPart == part)
                    {
                        symmetryPart = null;
                    }
                    GrabPart(part, symmetryPart);
                }
                else
                {
                    GrabPart(part);
                }
            }

            if (clickedOnce && !rotateMode && !flipped && !currentPart)
            {
                grid2lastPos = grid.anchoredPosition = Vector2.zero;
            }
            else
            {
                timer       = 0;
                clickedOnce = true;
            }
        }
    }
コード例 #6
0
    bool IsTooClose(ShipBuilderPart otherPart)
    {
        var closeConstant = mode == BuilderMode.Workshop ? -1.2F : -1F;
        var rect1         = ShipBuilder.GetRect(rectTransform);
        var rect2         = ShipBuilder.GetRect(otherPart.rectTransform);

        rect1.Expand(closeConstant * rect1.extents);
        rect2.Expand(closeConstant * rect2.extents);
        return(rect1.Intersects(rect2));
    }
コード例 #7
0
    bool PartIsTooClose(ShipBuilderPart part, ShipBuilderPart otherPart)
    {
        var closeConstant = mode == BuilderMode.Workshop ? -1.2F : -1F;
        var rect1         = ShipBuilder.GetRect(part.rectTransform);
        var rect2         = ShipBuilder.GetRect(otherPart.rectTransform);

        // add small number (0.005) to deal with floating point issues
        rect1.Expand((closeConstant - 0.005F) * rect1.extents);
        rect2.Expand((closeConstant - 0.005F) * rect2.extents);
        return(rect1.Intersects(rect2));
    }
コード例 #8
0
 private void PlaceCurrentPartInGrid()
 {
     lastPart    = currentPart;
     currentPart = null;
     if (lastPart.isInChain && lastPart.validPos)
     {
         lastPart.SetLastValidPos(lastPart.info.location);
     }
     else if (Input.GetKey(KeyCode.LeftShift) || builder.CheckPartIntersectsWithShell(lastPart))
     {
         lastPart.Snapback();
     }
 }
コード例 #9
0
    void UpdateChainHelper(ShipBuilderPart part)
    {
        var x = ShipBuilder.GetRect(part.rectTransform);

        foreach (ShipBuilderPart shipPart in cursorScript.parts)
        {
            if (!shipPart.isInChain)
            {
                var y = ShipBuilder.GetRect(shipPart.rectTransform);
                if (x.Intersects(y))
                {
                    shipPart.isInChain = true;
                    UpdateChainHelper(shipPart);
                }
            }
        }
    }
コード例 #10
0
    public bool CheckPartIntersectsWithShell(ShipBuilderPart shipPart)
    {
        var shellRect = GetRect(shell.rectTransform);

        var partBounds = GetRect(shipPart.rectTransform);

        if (partBounds.Intersects(shellRect))
        {
            bool z = Mathf.Abs(shipPart.rectTransform.anchoredPosition.x - shell.rectTransform.anchoredPosition.x) <=
                     0.18F * (shipPart.rectTransform.sizeDelta.x + shell.rectTransform.sizeDelta.x) &&
                     Mathf.Abs(shipPart.rectTransform.anchoredPosition.y - shell.rectTransform.anchoredPosition.y) <=
                     0.18F * (shipPart.rectTransform.sizeDelta.y + shell.rectTransform.sizeDelta.y);
            shipPart.isInChain = !z;
            return(z);
        }
        return(false);
    }
コード例 #11
0
 // Finds the part which contains the passed vector point
 // symmetry mode enables checks based on symmetryPart - same part ID, ability ID, different mirrored
 private ShipBuilderPart FindPart(Vector2 vector, ShipBuilderPart symmetryPart)
 {
     for (int i = parts.Count - 1; i >= 0; i--)
     {
         Bounds bound = ShipBuilder.GetRect(parts[i].rectTransform);
         bound.extents /= 1.5F;
         var origPos = transform.position;
         transform.position = Input.mousePosition;
         if (bound.Contains(vector) && (!symmetryPart || (parts[i].info.partID == symmetryPart.info.partID &&
                                                          (symmetryMode != SymmetryMode.X || parts[i].info.mirrored != symmetryPart.info.mirrored))))
         {
             transform.position = origPos;
             return(parts[i]);
         }
         transform.position = origPos;
     }
     return(null);
 }
コード例 #12
0
    public void DispatchPart(ShipBuilderPart part, ShipBuilder.TransferMode mode)
    {
        var culledInfo = ShipBuilder.CullSpatialValues(part.info);

        if (!builderPartDict.ContainsKey(culledInfo))
        {
            int size = ResourceManager.GetAsset <PartBlueprint>(part.info.partID).size;
            ShipBuilderInventoryScript builderPartDictInvButton = Instantiate(buttonPrefab,
                                                                              contentsArray[size]).GetComponent <ShipBuilderInventoryScript>();
            builderPartDict.Add(culledInfo, builderPartDictInvButton);
            contentTexts[size].SetActive(true);
            builderPartDict[culledInfo].part   = culledInfo;
            builderPartDict[culledInfo].cursor = cursorScript;
        }
        builderPartDict[culledInfo].IncrementCount();
        cursorScript.buildValue -= EntityBlueprint.GetPartValue(part.info);
        cursorScript.parts.Remove(part);
        Destroy(part.gameObject);
    }
コード例 #13
0
 // TODO: Test this when the Drone Workshop is added into the game
 public bool CheckPartIntersectsWithShell(ShipBuilderPart shipPart)
 {
     /*
      * var shellRect = GetRect(shell.rectTransform);
      *
      * shipPart.isInChain = false;
      * var partBounds = GetRect(shipPart.rectTransform);
      * if(partBounds.Intersects(shellRect)) {
      *      bool z = Mathf.Abs(shipPart.rectTransform.anchoredPosition.x - shell.rectTransform.anchoredPosition.x) <
      *      0.18F*(shipPart.rectTransform.sizeDelta.x + shell.rectTransform.sizeDelta.x) &&
      *      Mathf.Abs(shipPart.rectTransform.anchoredPosition.y - shell.rectTransform.anchoredPosition.y) <
      *      0.18F*(shipPart.rectTransform.sizeDelta.y + shell.rectTransform.sizeDelta.y);
      *      shipPart.isInChain = !z;
      *      return z;
      * }
      * return false;
      */
     Debug.LogError("You need to create this method for the Drone Workshop!");
     return(false);
 }
コード例 #14
0
    // Finds the part which contains the passed vector point
    // symmetry mode enables checks based on symmetryPart - same part ID, ability ID, different mirrored
    public ShipBuilderPart FindPart(Vector2 vector, ShipBuilderPart symmetryPart, bool useBounds = false)
    {
        if (!isMouseOnGrid)
        {
            return(null);
        }

        for (int i = parts.Count - 1; i >= 0; i--)
        {
            var origPos = transform.position;
            if (!useBounds && !PositionsCloseEnough(parts[i].rectTransform.anchoredPosition, vector))
            {
                continue;
            }
            else if (useBounds)
            {
                Bounds bound = ShipBuilder.GetRect(parts[i].rectTransform);
                bound.extents /= 1.5F;

                transform.position = Input.mousePosition;
                if (!bound.Contains(vector))
                {
                    transform.position = origPos;
                    continue;
                }
            }
            if ((!symmetryPart || (parts[i].info.partID == symmetryPart.info.partID &&
                                   (symmetryMode != SymmetryMode.X ||
                                    CheckOrientationCompatibility(parts[i].info, symmetryPart.info)))))
            {
                transform.position = origPos;
                return(parts[i]);
            }

            transform.position = origPos;
        }

        return(null);
    }
コード例 #15
0
    // TODO: Test this when the Drone Workshop is added into the game
    public bool CheckPartIntersectsWithShell(ShipBuilderPart shipPart)
    {
        var shellRect = ShipBuilder.GetRect(shellImage.rectTransform);

        var partBounds = ShipBuilder.GetRect(shipPart.rectTransform);

        if (partBounds.Intersects(shellRect))
        {
            /*
             * bool z = Mathf.Abs(shipPart.rectTransform.anchoredPosition.x - shellImage.rectTransform.anchoredPosition.x) <=
             * 0.18F*(shipPart.rectTransform.sizeDelta.x + shellImage.rectTransform.sizeDelta.x) &&
             * Mathf.Abs(shipPart.rectTransform.anchoredPosition.y - shellImage.rectTransform.anchoredPosition.y) <=
             * 0.18F*(shipPart.rectTransform.sizeDelta.y + shellImage.rectTransform.sizeDelta.y);
             */
            shipPart.isInChain = true; //!z;

            // reset sprite

            //return true;
            return(true); //z;
        }

        return(false);
    }
コード例 #16
0
    public void DispatchPart(ShipBuilderPart part, TransferMode transferMode)
    {
        var culledInfo = CullSpatialValues(part.info);
        Dictionary <EntityBlueprint.PartInfo, ShipBuilderInventoryScript> dict;

        Transform[]  dictContentsArray;
        GameObject[] dictContentTexts;
        switch (transferMode)
        {
        case TransferMode.Sell:
            cursorScript.buildCost -= EntityBlueprint.GetPartValue(part.info);
            dictContentsArray       = traderContentsArray;
            dict             = traderPartDict;
            dictContentTexts = traderContentTexts;
            break;

        case TransferMode.Buy:
            dictContentsArray = contentsArray;
            dict             = partDict;
            dictContentTexts = contentTexts;
            break;

        default:                 // transfer back to original inventory
            if (part.mode == BuilderMode.Trader)
            {
                cursorScript.buildCost -= EntityBlueprint.GetPartValue(part.info);
            }
            dict = (part.mode == BuilderMode.Yard ? partDict : traderPartDict);
            dictContentsArray = (part.mode == BuilderMode.Yard ? contentsArray : traderContentsArray);
            dictContentTexts  = (part.mode == BuilderMode.Yard ? contentTexts : traderContentTexts);
            break;
        }
        if (!dict.ContainsKey(culledInfo))
        {
            int size = ResourceManager.GetAsset <PartBlueprint>(part.info.partID).size;
            ShipBuilderInventoryScript dictInvButton = Instantiate(buttonPrefab,
                                                                   dictContentsArray[size]).GetComponent <ShipBuilderInventoryScript>();
            switch (transferMode)              // set the new button's transfer mode
            {
            case TransferMode.Buy:
                dictInvButton.mode = BuilderMode.Yard;
                break;

            case TransferMode.Sell:
                dictInvButton.mode = BuilderMode.Trader;
                break;

            case TransferMode.Return:
                dictInvButton.mode = part.mode;
                break;
            }
            dict.Add(culledInfo, dictInvButton);
            dictContentTexts[size].SetActive(true);
            dict[culledInfo].part   = culledInfo;
            dict[culledInfo].cursor = cursorScript;
        }
        dict[culledInfo].IncrementCount();
        cursorScript.buildValue -= EntityBlueprint.GetPartValue(part.info);
        cursorScript.parts.Remove(part);
        Destroy(part.gameObject);
    }
コード例 #17
0
    void Update()
    {
        UpdateCompact();

        if (clickedOnce)
        {
            if (timer > 0.2F)
            {
                clickedOnce = false;
            }
            else
            {
                timer += Time.deltaTime;
            }
        }

        int baseMoveSize = cursorMode == BuilderMode.Yard ? 4 : 5;

        builder.UpdateChain();
        if (Input.GetKeyDown(KeyCode.C) && (!searchField.isFocused && !jsonField.isFocused && !WCWorldIO.active))
        {
            if (builder as ShipBuilder == null || (builder as ShipBuilder).Equals(null) || !(builder as ShipBuilder).editorMode)
            {
                ClearAllParts();
            }
        }
        foreach (var part in parts)
        {
            part.boundImage.enabled = Input.GetKey(KeyCode.LeftShift);
        }

        System.Func <Vector3, int, int, Vector3> roundToRatios = (x, y, z) => new Vector3(y * ((int)x.x / (int)y), z * ((int)x.y / (int)z), 0);
        var newOffset = roundToRatios(grid.position, baseMoveSize, baseMoveSize) - grid.position;

        transform.position = roundToRatios(Input.mousePosition, baseMoveSize, baseMoveSize) - newOffset;
        var oldPos = GetComponent <RectTransform>().anchoredPosition;

        GetComponent <RectTransform>().anchoredPosition = new Vector2(Mathf.Round(oldPos.x / 10) * 10, Mathf.Round(oldPos.y / 10) * 10);
        // round to nearest 0.1
        // TODO: Make this stuff less messy. Regardless, consistency achieved!
        if (rotateMode)
        {
            RotateLastPart();
            return;
        }
        if (flipped)
        {
            grid2mousePos = Input.mousePosition;
            flipped       = false;
            return;
        }

        if (currentPart)
        {
            currentPart.SetMaskable(false);
            currentPart.info.location = (GetComponent <RectTransform>().anchoredPosition + offset) / 100;
            if (Input.GetMouseButtonUp(0))
            {
                PlaceCurrentPart();
            }
        }
        else if (Input.GetMouseButtonDown(0))
        {
            grid2lastPos  = grid.anchoredPosition;
            grid2mousePos = Input.mousePosition;
            lastPart      = null;
            for (int i = parts.Count - 1; i >= 0; i--)
            {
                Bounds bound = ShipBuilder.GetRect(parts[i].rectTransform);
                bound.extents /= 1.5F;
                var origPos = transform.position;
                transform.position = Input.mousePosition;
                if (bound.Contains(GetComponent <RectTransform>().anchoredPosition))
                {
                    transform.position = origPos;
                    GrabPart(parts[i]);
                    break;
                }
                transform.position = origPos;
            }
            if (clickedOnce && !rotateMode && !flipped && !currentPart)
            {
                grid2lastPos = grid.anchoredPosition = Vector2.zero;
            }
            else
            {
                timer       = 0;
                clickedOnce = true;
            }
        }

        // drag grid
        Vector2 bounds = grid.sizeDelta / 2 - grid2mask.sizeDelta / 2;

        if (grid.GetComponent <DragDetector>().dragging &&
            Input.GetMouseButton(0) && !rotateMode && !flipped && !currentPart)
        {
            grid.anchoredPosition = grid2lastPos + ((Vector2)Input.mousePosition - grid2mousePos) * 2;
            grid.anchoredPosition = new Vector2(Mathf.Max(-bounds.x, Mathf.Min(bounds.x, grid.anchoredPosition.x)),
                                                Mathf.Max(-bounds.y, Mathf.Min(bounds.y, grid.anchoredPosition.y))
                                                );
        }
    }