Example #1
0
    /// <summary>
    /// Method called on button click, used to remove wires from world
    /// </summary>
    /// <param name="cableUI">cable ui cell which calling this method</param>
    /// <param name="data">electircal cable tile data</param>
    public void CutWire(GameObject cableUI, ElectricalCableTileData data)
    {
        if (data.electricalCable == null)
        {
            return;
        }

        Vector3 targetPosition = targetWorldPosition.ToLocal(matrix);
        var     apply          = PositionalHandApply.ByLocalPlayer(matrix.gameObject, targetPosition);

        // if can interact and there are no cooldown - send message to server and destroy UI cell
        if (WillInteract(apply) && Cooldowns.TryStartClient(apply, CommonCooldowns.Instance.Interaction))
        {
            // send message to destroy cable
            SendCableCuttingMessage(targetWorldPosition, data.Name, data.TileType);
            // destroy ui object
            Destroy(cableUI);
            // decrease cable count
            electricalCablesCount--;
            // resize scroll view height
            CalculateRequiredWindowSize();

            // disable window if there are no objects in scrollview
            if (electricalCablesCount == 0)
            {
                SetWindowActive(false);
            }
        }
    }
Example #2
0
    /// <summary>
    /// Initialize window - find cables, instantiate ui cells, assign sprites
    /// </summary>
    /// <param name="matrix">Matrix on which target tile is placed</param>
    /// <param name="targetCellPosition">Cell position of target tile</param>
    /// <param name="targetWorldPosition">World position of target tile</param>
    public void InitializeCableCuttingWindow(Matrix matrix, Vector3Int targetCellPosition, Vector3 targetWorldPosition)
    {
        this.matrix = matrix;
        this.targetWorldPosition = targetWorldPosition;

        // destroy all old cable ui cells
        for (int i = 0; i < scrollViewLayoutGroup.transform.childCount; i++)
        {
            Destroy(scrollViewLayoutGroup.transform.GetChild(i).gameObject);
        }

        Vector3Int cellPos = targetCellPosition;

        electricalCablesCount = 0;

        // loop trough all layers searching for electrical cable tiles
        for (int i = 0; i < 50; i++)
        {
            cellPos.z = -i + 1;
            if (matrix.UnderFloorLayer.GetTileUsingZ(cellPos) is ElectricalCableTile electricalCableTile)
            {
                ElectricalCableTileData data = new ElectricalCableTileData
                {
                    electricalCable = electricalCableTile,
                    positionZ       = cellPos.z
                };
                // instantiate ui object
                InstantiateCableUICell(data);
                electricalCablesCount++;
            }
        }

        // resize scroll view height after spawning objects
        CalculateRequiredWindowSize();
    }
Example #3
0
    /// <summary>
    /// Instantiate cableCellUIPrefab under scrollViewLayoutGroup and assign all necessary values
    /// </summary>
    private void InstantiateCableUICell(ElectricalCableTileData data)
    {
        // instantiate ui cell and get CableUICell script
        GameObject  cableUI     = Instantiate(cableCellUIPrefab, scrollViewLayoutGroup.transform);
        CableUICell cableUICell = cableUI.GetComponent <CableUICell>();

        // add on click listener
        cableUICell.cutWireButton.onClick.AddListener(() => { CutWire(cableUI, data); });

        // set ui cell text
        cableUICell.wireLabelText.text = data.electricalCable.WireEndA + " - " + data.electricalCable.WireEndB;

        // set ui cell sprite
        int index = GetSpriteIndexByConnectionPoints(data.electricalCable.WireEndA, data.electricalCable.WireEndB);

        cableUICell.wireIconImage.sprite = GetSpriteAtlasByCableType(data.electricalCable.PowerType)[index];
    }
Example #4
0
    /// <summary>
    /// Initialize window - find cables, instantiate ui cells, assign sprites
    /// </summary>
    /// <param name="matrix">Matrix on which target tile is placed</param>
    /// <param name="targetCellPosition">Cell position of target tile</param>
    /// <param name="targetWorldPosition">World position of target tile</param>
    public void InitializeCableCuttingWindow(Matrix matrix, Vector3Int targetCellPosition, Vector3 targetWorldPosition)
    {
        this.matrix = matrix;
        this.targetWorldPosition = targetWorldPosition;

        // destroy all old cable ui cells
        for (int i = 0; i < scrollViewLayoutGroup.transform.childCount; i++)
        {
            Destroy(scrollViewLayoutGroup.transform.GetChild(i).gameObject);
        }

        Vector3Int cellPos = targetCellPosition;

        electricalCablesCount = 0;

        //No Hardcoded depth
        //know Location exactly
        //loop through all tiles on layer and check if a certain type


        // loop trough all layers searching for electrical cable tiles
        foreach (var CableTile in matrix.MetaTileMap.GetAllTilesByType <ElectricalCableTile>(cellPos, LayerType.Underfloor))
        {
            ElectricalCableTileData data = new ElectricalCableTileData
            {
                electricalCable = CableTile,
                TileType        = CableTile.TileType,
                Name            = CableTile.name
            };
            // instantiate ui object
            InstantiateCableUICell(data);
            electricalCablesCount++;
        }

        // resize scroll view height after spawning objects
        CalculateRequiredWindowSize();
    }