Exemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        if (justExited > 0.0f)
        {
            justExited -= Time.deltaTime;
        }

        if (IsInWireEditMode())
        {
            Vector2    mousePosition = m_visualGridManager.GetSnapPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
            Vector2Int oGridoob      = Vector2Int.zero;
            Vector2Int oGrid         = Vector2Int.zero;

            bool foundGrid    = m_visualGridManager.GetGridCoordinates(mousePosition, ref oGrid, false, true);                                                // In bounds for dragging
            bool foundGridOOB = m_visualGridManager.GetGridCoordinates(mousePosition, ref oGridoob, false, false) &&
                                oGridoob.x >= 0 && oGridoob.x < m_gridManager.DimensionX + 2 && oGridoob.y >= 0 && oGridoob.y < m_gridManager.DimensionY + 2; // Allow oob for start/end

            if (m_isDragging)
            {
                CellCoordinates cell  = new CellCoordinates();
                bool            found = false;
                if (foundGridOOB)
                {
                    CellCoordinates oobCell = new CellCoordinates((uint)oGridoob.x, (uint)oGridoob.y);
                    GridObject      o       = m_gridManager.GetCell(oobCell);
                    if (o != null)
                    {
                        cell  = oobCell;
                        found = true;
                    }
                }
                if (!found && foundGrid)
                {
                    cell  = new CellCoordinates((uint)oGrid.x, (uint)oGrid.y);
                    found = true;
                }

                if (found)
                {
                    PassThroughCell(cell);
                    string log = "WireManager: Current path - ";
                    foreach (CellCoordinates x in m_passedThrough.ToArray())
                    {
                        log += x.ToString() + " ";
                    }
                    if (LogVerbose)
                    {
                        Log(log);
                    }
                }
            }

            // Start drag
            if (Input.GetMouseButtonDown(0) && !m_isDragging)
            {
                if (foundGridOOB)
                {
                    CellCoordinates cell = new CellCoordinates((uint)oGridoob.x, (uint)oGridoob.y);
                    // Valid start point, begin
                    StartDragging(cell);
                    if (!m_isDragging)
                    {
                        EndMode();
                    }
                }
                else
                {
                    EndMode();
                }
            }

            // End Drag/Mode
            if (Input.GetMouseButtonUp(0) && m_isDragging)
            {
                TryCommit();
            }

            // Delete
            if (Input.GetMouseButtonUp(1) && !m_isDragging)
            {
                if (foundGrid)
                {
                    CellCoordinates cell = new CellCoordinates((uint)oGrid.x, (uint)oGrid.y);;
                    GridObject      o    = m_gridManager.GetCell(cell);
                    if (o != null && o.ObjectType == GridObjectType.Wire)
                    {
                        m_wireVisualManager.ClearWire(cell);
                        m_gridManager.ClearCell(cell);
                    }
                    else
                    {
                        EndMode();
                    }
                }
                else
                {
                    EndMode();
                }
            }
        }
    }
    public void ClearCell(CellCoordinates _coordinates, bool _isSwap)
    {
        GridObject             gridObject = gridManager.GetCell(_coordinates);
        List <CellCoordinates> neighboursToInformOfDeath = new List <CellCoordinates>();

        //Clear Wires
        if (gridObject != null && gridObject.ObjectType == GridObjectType.Gate)
        {
            foreach (CellCoordinates cell in gridObject.Coordinates)
            {
                Gate gate = (Gate)gridManager.GetCell(cell);

                foreach (CellCoordinates inputCoords in gate.Inputs)
                {
                    GridObject input = gridManager.GetCell(inputCoords);
                    if (input != null)
                    {
                        if (input.ObjectType == GridObjectType.Wire)
                        {
                            Wire wire = (Wire)input;
                            for (uint i = 0; i < gridObject.Coordinates.Length; ++i)
                            {
                                if (gridObject.Coordinates[i] == wire.Exit)
                                {
                                    // This wire actually connects to us, destroy it
                                    wireVisualManager.ClearWire(inputCoords);
                                    break;
                                }
                            }
                        }

                        neighboursToInformOfDeath.Add(inputCoords);
                    }
                }
                foreach (CellCoordinates outputCoords in gate.Outputs)
                {
                    GridObject output = gridManager.GetCell(outputCoords);
                    if (output != null)
                    {
                        if (output.ObjectType == GridObjectType.Wire)
                        {
                            Wire wire = (Wire)output;
                            for (uint i = 0; i < gridObject.Coordinates.Length; ++i)
                            {
                                if (gridObject.Coordinates[i] == wire.Entry)
                                {
                                    // This wire actually connects to us, destroy it
                                    wireVisualManager.ClearWire(outputCoords);
                                    break;
                                }
                            }
                        }
                        else
                        {
                            wireVisualManager.ClearWire(outputCoords);
                        }

                        neighboursToInformOfDeath.Add(outputCoords);
                    }
                }
            }
        }

        if (gridObject != null)
        {
            if (gridObject.ObjectType != GridObjectType.Wire)
            {
                if (!_isSwap)
                {
                    Destroy(placedGridObjects[gridObject]);
                    placedGridObjects.Remove(gridObject);
                }
            }

            wireVisualManager.ClearWire(_coordinates);
        }

        gridManager.ClearCell(_coordinates);

        foreach (CellCoordinates cell in neighboursToInformOfDeath)
        {
            UpdateGiblets(cell, false);
        }
    }