Beispiel #1
0
    private void PlaceAction()
    {
        if (ic != null && (ic.IcType == ICType.breadboard || ic.IcType == ICType.powerrail))
        {
            PlaceActionBreadBoard();
            return;
        }

        int layer_mask = LayerMask.GetMask("Pin");
        Ray ray        = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out RaycastHit hit, 500, layer_mask) && !EventSystem.current.IsPointerOverGameObject())
        {
            if (ic != null && icModel != null)
            {
                string nodeId = hit.transform.gameObject.name;
                int    index  = CircuitHelper.GetIndexFromNode(nodeId, hit.point);

                int x = Mathf.RoundToInt(hit.point.x);
                int z = Mathf.RoundToInt(hit.point.z);
                icModel.transform.position = new Vector3(x, 0, z);

                if (circuitPool.CanPlace(ic.IcType, ic.Pins, nodeId, index))
                {
                    PlaceComponent(nodeId, index, new Vector3(x, 0, z));
                }
                else
                {
                    Debug.Log("Cannot place item here");
                }
            }
        }
Beispiel #2
0
    private void PlaceAction_Hover()
    {
        if (ic != null && ic.IcType == ICType.wire)
        {
            PlaceActionWire_Hover();
            return;
        }

        if (ic != null && (ic.IcType == ICType.breadboard || ic.IcType == ICType.powerrail))
        {
            PlaceActionBreadboard_Hover();
            return;
        }

        int layer_mask = LayerMask.GetMask("Pin");
        Ray ray        = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out RaycastHit hit, 500, layer_mask) && !EventSystem.current.IsPointerOverGameObject())
        {
            string nodeId = hit.transform.gameObject.name;
            int    index  = CircuitHelper.GetIndexFromNode(nodeId, hit.point);
            int    x      = Mathf.RoundToInt(hit.point.x);
            int    z      = Mathf.RoundToInt(hit.point.z);

            if (ic != null && icModel != null)
            {
                Vector3 location = new Vector3(x, 0, z);

                switch (ic.IcType)
                {
                case ICType.dual:
                    break;

                default:
                    icModel.transform.position = location;
                    icModel.transform.rotation = Quaternion.Euler(0, CircuitHelper.IsNodeRotated(nodeId) ? 90 : 0, 0);
                    break;
                }

                if (circuitPool.CanPlace(ic.IcType, ic.Pins, nodeId, index))
                {
                    if (showsError)
                    {
                        showsError = false;
                        RemoveOutline(icModel.GetComponentsInChildren <Renderer>());
                    }
                }
                else
                {
                    if (!showsError)
                    {
                        showsError = true;
                        AddOutline(icModel.GetComponentsInChildren <Renderer>(), outlineMat);
                    }
                }
            }
        }
Beispiel #3
0
    private void PlaceAction()
    {
        if (ic != null && icModel != null)
        {
            switch (ic.IcType)
            {
            case ICType.breadboard:
            case ICType.powerrail:
                if (raycastManager.GetNodeId(out RaycastHit hit1, out string _, "Ground"))
                {
                    int x = Mathf.RoundToInt(hit1.point.x);
                    int z = Mathf.RoundToInt(hit1.point.z);
                    icModel.transform.position = new Vector3(x, 0, z);

                    string type = (ic.IcType == ICType.breadboard ? "0" : "1");

                    if (fabricator.canPlaceBreadBoard(type, x, z, placeRotated))
                    {
                        fabricator.addBoard(type, x, z, placeRotated);
                    }
                    else
                    {
                        Debug.Log("Cannot place item here");
                    }
                }
                else
                {
                    icModel.transform.position = GameObjectPool;
                }
                break;

            default:
                if (raycastManager.GetNodeId(out RaycastHit hit2, out string nodeId, "Pin"))
                {
                    int index = CircuitHelper.GetIndexFromNode(nodeId, hit2.point);

                    int x = Mathf.RoundToInt(hit2.point.x);
                    int z = Mathf.RoundToInt(hit2.point.z);
                    icModel.transform.position = new Vector3(x, 0, z);

                    if (circuitPool.CanPlace(ic.IcType, ic.Pins, nodeId, index))
                    {
                        PlaceComponent(nodeId, index, new Vector3(x, 0, z));
                    }
                    else
                    {
                        Debug.Log("Cannot place item here");
                    }
                }
                else
                {
                    icModel.transform.position = GameObjectPool;
                }
                break;
            }
        }
    }
Beispiel #4
0
    private void PlaceActionWire_Hover()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out RaycastHit hit) && !EventSystem.current.IsPointerOverGameObject())
        {
            if (hit.transform.gameObject.layer == LayerMask.NameToLayer("UI"))
            {
                icModel.transform.position = GameObjectPool;
                return;
            }

            bool isPin = hit.transform.gameObject.layer == LayerMask.NameToLayer("Pin");

            string nodeId = hit.transform.gameObject.name;
            int    index  = 0;
            int    x      = Mathf.RoundToInt(hit.point.x);
            int    z      = Mathf.RoundToInt(hit.point.z);

            if (isPin)
            {
                index = CircuitHelper.GetIndexFromNode(nodeId, hit.point);
            }

            Vector3 location = new Vector3(x, 0, z);


            if (locationASelected)
            {
                List <Vector3> hoverLinePos = new List <Vector3>(wireNodes);
                //hoverLinePos.Add(hit.point);
                hoverLinePos.Add(new Vector3(Mathf.Round(hit.point.x * 2) / 2, hit.point.y, Mathf.Round(hit.point.z * 2) / 2));
                icModel.GetComponentInChildren <LineRenderer>().positionCount = hoverLinePos.Count;
                icModel.GetComponentInChildren <LineRenderer>().SetPositions(hoverLinePos.ToArray());
            }
            else
            {
                icModel.transform.position = location;
            }

            if (isPin && circuitPool.CanPlace(ic.IcType, ic.Pins, nodeId, index))
            {
                if (showsError)
                {
                    showsError = false;
                    RemoveOutline(icModel.GetComponentsInChildren <Renderer>());
                }
            }
            else
            {
                if (!showsError)
                {
                    showsError = true;
                    AddOutline(icModel.GetComponentsInChildren <Renderer>(), outlineMat);
                }
            }
        }
        else if (icModel != null)
        {
            if (ic.IcType != ICType.wire)
            {
                icModel.transform.position = GameObjectPool;
            }
        }
    }
Beispiel #5
0
    private void LoadComponents(ZipArchive archive)
    {
        int index = GetZipArchiveEntryIndex("components.json", archive);

        if (index == -1)
        {
            return;
        }

        ZipArchiveEntry entry = archive.Entries[index];

        using (StreamReader file = new StreamReader(entry.Open()))
        {
            JsonSerializer serializer = new JsonSerializer
            {
                TypeNameHandling       = TypeNameHandling.Auto,
                ObjectCreationHandling = ObjectCreationHandling.Replace,
                Converters             = { new BitArrayConverter(), new Vector3Converter(), new WireConverter() }
            };
            Dictionary <Guid, IntegratedCircuit> components = (Dictionary <Guid, IntegratedCircuit>)serializer.Deserialize(file, typeof(Dictionary <Guid, IntegratedCircuit>));

            foreach (Guid g in components.Keys)
            {
                IntegratedCircuit ic = components[g];
                GameObject        obj;

                switch (ic.IcType)
                {
                case ICType.solo:

                    obj = cp.PlaceIntegratedCircuit(ic, ic.GetPinNode(0), ic.GetPinNodeIndex(0), true);
                    obj.transform.position = CircuitHelper.GetPositionFromNode(ic.GetPinNode(0), ic.GetPinNodeIndex(0));

                    break;

                case ICType.dual:

                    obj = cp.PlaceIntegratedCircuit(ic, ic.GetPinNode(0), ic.GetPinNodeIndex(0), ic.GetPinNode(1), ic.GetPinNodeIndex(1), true);

                    Vector3 locationA = CircuitHelper.GetPositionFromNode(ic.GetPinNode(0), ic.GetPinNodeIndex(0));
                    Vector3 locationB = CircuitHelper.GetPositionFromNode(ic.GetPinNode(1), ic.GetPinNodeIndex(1));

                    obj.transform.position   = new Vector3((locationA.x + locationB.x) / 2, 0, (locationA.z + locationB.z) / 2);
                    obj.transform.localScale = new Vector3(Vector3.Distance(locationB, locationA) / 2, 1, 1);
                    float angle = CircuitHelper.AngleBetweenVector3(locationB, locationA);
                    angle = locationA.z > locationB.z ? -angle : angle;
                    obj.transform.rotation = Quaternion.Euler(0, angle, 0);

                    break;

                case ICType.ic4:
                    obj = cp.PlaceIntegratedCircuit(ic, ic.GetPinNode(0), ic.GetPinNodeIndex(0), true);
                    obj.transform.position = CircuitHelper.GetPositionFromNode(ic.GetPinNode(0), ic.GetPinNodeIndex(0));
                    break;

                case ICType.ic6:
                    obj = cp.PlaceIntegratedCircuit(ic, ic.GetPinNode(0), ic.GetPinNodeIndex(0), true);
                    obj.transform.position = CircuitHelper.GetPositionFromNode(ic.GetPinNode(0), ic.GetPinNodeIndex(0));
                    break;

                case ICType.wire:

                    obj = cp.PlaceIntegratedCircuit(ic, ic.GetPinNode(0), ic.GetPinNodeIndex(0), ic.GetPinNode(1), ic.GetPinNodeIndex(1), true);

                    Vector3[] vectors = ((Wire)ic).points;

                    LineRenderer lineRenderer = obj.GetComponentInChildren <LineRenderer>();
                    lineRenderer.positionCount = vectors.Length;
                    lineRenderer.SetPositions(vectors);

                    CircuitHelper.CreateColliderChain(obj, vectors);

                    break;

                default:
                    break;
                }

                ic.CustomMethod();
            }
        }
    }