public void AddGate(GateType _type, CellCoordinates _coordinates, ObjectOrientation _orientation, GameObject _selfGameObject, int _value = 0, bool _isSwap = false)
    {
        //Check if an object is already there
        GridObject gridObject = gridManager.GetCell(_coordinates);

        if (gridObject != null)
        {
            ClearCell(_coordinates, _isSwap);
            gridObject = null;
        }

        switch (_type)
        {
        case GateType.Add:
        {
            gridObject = new AddGate(_coordinates, _orientation);
            break;
        }

        case GateType.Subtract:
        {
            gridObject = new SubtractGate(_coordinates, _orientation);
            break;
        }

        case GateType.Multiply:
        {
            gridObject = new MultiplyGate(_coordinates, _orientation);
            break;
        }

        case GateType.Divide:
        {
            gridObject = new DivideGate(_coordinates, _orientation);
            break;
        }

        case GateType.IncrementDecrement:
        {
            gridObject = new IncrementDecrementGate(_coordinates, _orientation, _value);
            break;
        }

        case GateType.Cross:
        {
            gridObject = new CrossGate(_coordinates, _orientation);
            break;
        }

        case GateType.Replicate:
        {
            gridObject = new ReplicateGate(_coordinates, _orientation);
            break;
        }
        }

        gridManager.InsertObject(gridObject);
        placedGridObjects.Add(gridObject, _selfGameObject);

        //Create Visual Wire
        Gate gate  = (Gate)gridObject;
        uint index = 0;

        foreach (CellCoordinates inputCoords in gate.Inputs)
        {
            GridObject inputGridObject = gridManager.GetCell(inputCoords);
            if (inputGridObject != null)
            {
                if (inputGridObject.ObjectType != GridObjectType.Wire)
                {
                    if (IsConnected(false, inputCoords, gridObject))
                    {
                        wireVisualManager.CreateWireAndLink(inputCoords, gate.GetCoordinateForInput(index));
                    }
                }
            }
            index++;
        }

        index = 0;
        foreach (CellCoordinates outputCoords in gate.Outputs)
        {
            GridObject outputGridObject = gridManager.GetCell(outputCoords);
            if (outputGridObject != null)
            {
                if (outputGridObject.ObjectType != GridObjectType.Wire)
                {
                    if (IsConnected(true, outputCoords, gridObject))
                    {
                        wireVisualManager.CreateWireAndLink(_coordinates, outputCoords);
                    }
                }
            }
            index++;
        }

        UpdateGiblets(_coordinates, true);
    }
Esempio n. 2
0
    public bool IsGridObjectValid(Vector2 _position, VisualGate _gate, bool _convertToCanvas)
    {
        GridObject gridObject = null;
        Vector2Int oGrid      = Vector2Int.zero;

        if (GetGridCoordinates(_position, ref oGrid, _convertToCanvas))
        {
            CellCoordinates cell = new CellCoordinates((uint)oGrid.x, (uint)oGrid.y);

            switch (_gate.gateType)
            {
            case GateType.Add:
            {
                gridObject = new AddGate(cell, _gate.objectOrientation);
                break;
            }

            case GateType.Subtract:
            {
                gridObject = new SubtractGate(cell, _gate.objectOrientation);
                break;
            }

            case GateType.Multiply:
            {
                gridObject = new MultiplyGate(cell, _gate.objectOrientation);
                break;
            }

            case GateType.Divide:
            {
                gridObject = new DivideGate(cell, _gate.objectOrientation);
                break;
            }

            case GateType.IncrementDecrement:
            {
                gridObject = new IncrementDecrementGate(cell, _gate.objectOrientation);
                break;
            }

            case GateType.Cross:
            {
                gridObject = new CrossGate(cell, _gate.objectOrientation);
                break;
            }

            case GateType.Replicate:
            {
                gridObject = new ReplicateGate(cell, _gate.objectOrientation);
                break;
            }
            }

            foreach (CellCoordinates coordinates in gridObject.Coordinates)
            {
                if (coordinates.X < 1 || coordinates.Y < 1 || coordinates.X > gridWidth || coordinates.Y > gridHeight)
                {
                    return(false);
                }
            }

            return(true);
        }

        return(false);
    }