void ScrollBlocks(BLOCK nextBlock) { currentBlockType = Block.BlockFactory(nextBlock); GetComponent <SpriteRenderer>().sprite = currentBlockType.blockSprite; LineController lineC = GetComponent <LineController>(); if (lineC.currentLine != null) { lineC.currentLine.ResetPath(); } foreach (GameObject go in couplers) { LineCoupler lc = go.GetComponent <LineCoupler>(); if (lc.currentLine != null) { lc.currentLine.ResetPath(); } } SetValue(0); // Custom collider size depending on block type if (currentBlockType.blockType == BLOCK.EMPTY) { GetComponent <BoxCollider2D>().size = new Vector2(2.0f, 2.0f); } else { GetComponent <BoxCollider2D>().size = new Vector2(1.0f, 1.0f); } }
override public void UpdateGridValue(GridBehaviour obj) { int divideAmt = 0; int curAmt = 0; foreach (GameObject go in obj.couplers) { LineCoupler lc = go.GetComponent <LineCoupler>(); if (!lc.inUse) { continue; } if (!lc.isInput) { ++divideAmt; } else if (lc.isInput) { curAmt = lc.currentLine.value; } } // Check to avoid division by 0 if (divideAmt > 0) { obj.data = new GridData(curAmt / divideAmt); } else { obj.data = new GridData(curAmt); } }
override public void UpdateGridValue(GridBehaviour obj) { int minusFrom = 0; int minusAmt = 0; foreach (GameObject go in obj.couplers) { LineCoupler lc = go.GetComponent <LineCoupler>(); if (!lc.inUse || !lc.isInput) { continue; } if (lc.currentLine.value > minusFrom) { minusAmt += minusFrom; minusFrom = lc.currentLine.value; } else { minusAmt += lc.currentLine.value; } } // Final minus amount cant go lower than 0 int newVal = Mathf.Max(minusFrom - minusAmt, 0); obj.data = new GridData(newVal); }
public void DrawLegalBlockFromCoupler(LineCoupler lc) { GameObject gridObj = null; int[] coord = lc.GetComponentInParent <GridBehaviour>().coordinates; List <List <GameObject> > grid = GameManager.Instance.grid; gridMan.ResetLegalBlocks(); switch (lc.dir) { case DIRECTION.LEFT: gridObj = grid[coord[0] - 1][coord[1]]; break; case DIRECTION.RIGHT: gridObj = grid[coord[0] + 1][coord[1]]; break; case DIRECTION.TOP: gridObj = grid[coord[0]][coord[1] + 1]; break; case DIRECTION.BOTTOM: gridObj = grid[coord[0]][coord[1] - 1]; break; } if (CheckGridForLegalBlock(gridObj)) { gridMan.legalBlockFeedback[0].transform.position = gridObj.transform.position; } }
// Trace to the originating start block to set their used flag public void TraceToStartBlock() { foreach (GameObject couplerObj in couplers) { LineCoupler coupler = couplerObj.GetComponent <LineCoupler>(); if (!coupler.inUse || !coupler.isInput) { continue; } // Take current line's starting couplers GridBehaviour originBlock = coupler.currentLine.lineCouplers[0].currentBlock.GetComponent <GridBehaviour>(); // if it is a start block, set flag if (originBlock.overrideBlock == BLOCK.START) { originBlock.GetComponent <StartBlockScript>().SetInUse(true); } else { // if not then call trace to start block on that block originBlock.TraceToStartBlock(); } } }
// Recursively update blocks through couplers public void UpdateBlockValue() { UpdateGridData(); // Send update to each output couplers foreach (GameObject obj in couplers) { LineCoupler lc = obj.GetComponent <LineCoupler>(); if (lc.isInput) { continue; } if (lc.currentLine != null) { int resValue = GetGridData().value; //lc.currentLine.value = resValue; lc.currentLine.UpdateLineValue(resValue); if (lc.currentLine.lineCouplers[1] != null) { lc.currentLine.lineCouplers[1].UpdateCouplerValue(); } } } }
// Toggle the other couplers which are not in use public void ToggleOtherCouplers(bool flag) { foreach (GameObject go in couplers) { LineCoupler lc = go.GetComponent <LineCoupler>(); if (lc.inUse) { continue; } go.SetActive(flag); } }
bool CheckLegalDirection(int xDiff, int yDiff, LineCoupler fromCoupler) { bool success = false; if (fromCoupler != null) { success = fromCoupler.CheckLegalDirection(xDiff, yDiff); } else { success = !((xDiff > 0 && yDiff > 0) || (xDiff > 1 || yDiff > 1)); } return(success); }
public bool CanSpawnInput() { int numIn = 0; foreach (GameObject go in couplers) { LineCoupler lc = go.GetComponent <LineCoupler>(); if (lc.inUse && lc.isInput) { ++numIn; } } return(numIn < currentBlockType.maxIn); }
override public void UpdateGridValue(GridBehaviour obj) { int finalVal = 0; foreach (GameObject go in obj.couplers) { LineCoupler lc = go.GetComponent <LineCoupler>(); if (!lc.inUse || !lc.isInput) { continue; } finalVal += lc.currentLine.value; } obj.data = new GridData(finalVal); }
override public void UpdateGridValue(GridBehaviour obj) { // Look for input lines foreach (GameObject go in obj.couplers) { LineCoupler lc = go.GetComponent <LineCoupler>(); if (!lc.inUse || !lc.isInput) { continue; } obj.data = new GridData(lc.currentLine.value); obj.GetComponent <EndBlockScript>().UpdateEndStatus(obj.data.value); return; } // If no input was found obj.GetComponent <EndBlockScript>().SetNoInput(); }
// Check other couplers for existing line public bool IsLineExisting(Line curLine) { GameObject fromBlock = curLine.lineCouplers[0].currentBlock; foreach (GameObject go in couplers) { LineCoupler lc = go.GetComponent <LineCoupler>(); if (!lc.inUse) { continue; } if (lc.currentLine.lineCouplers[1] != null && lc.currentLine.lineCouplers[1].currentBlock == fromBlock) { return(true); } } return(false); }
bool CheckIfMoveIsLegal() { bool success = true; gridMan.ResetIllegalBlock(); Line line = lineMan.currentLineBeingDrawn; // Check if the block is already occupied if (currentLine != null || GetComponent <GridBehaviour>().currentBlockType.blockType != BLOCK.EMPTY) { success = false; } else { List <LineController> linePath = line.linePath; int[] thisCoord = GetComponentInParent <GridBehaviour>().coordinates; int[] prevCoord; // Legal move checking from a coupler is more specific LineCoupler fromCoupler = null; if (line.linePath.Count == 0) { fromCoupler = line.lineCouplers[0]; prevCoord = fromCoupler.GetComponentInParent <GridBehaviour>().coordinates; } else { prevCoord = linePath[linePath.Count - 1].GetComponentInParent <GridBehaviour>().coordinates; } int xDiff = Mathf.Abs(thisCoord[0] - prevCoord[0]); int yDiff = Mathf.Abs(thisCoord[1] - prevCoord[1]); success = CheckLegalDirection(xDiff, yDiff, fromCoupler); } return(success); }
public void UpdateOtherCouplers() { switch (currentBlockType.blockType) { case BLOCK.START: foreach (GameObject go in couplers) { LineCoupler lc = go.GetComponent <LineCoupler>(); if (!lc.inUse) { continue; } ToggleOtherCouplers(false); return; } break; case BLOCK.END: // End blocks should have only 1 input and 1 output foreach (GameObject go in couplers) { LineCoupler lc = go.GetComponent <LineCoupler>(); if (!lc.inUse) { continue; } ToggleOtherCouplers(false); return; } break; } // If no coupler is in use, activate all couplers if (currentBlockType.blockType != BLOCK.EMPTY) { SetCouplers(true); } }
override public void UpdateGridValue(GridBehaviour obj) { bool anyInput = false; int finalVal = 1; foreach (GameObject go in obj.couplers) { LineCoupler lc = go.GetComponent <LineCoupler>(); if (!lc.inUse || !lc.isInput) { continue; } anyInput = true; finalVal *= lc.currentLine.value; } // Set the value to 0 if there are no inputs if (anyInput == false) { finalVal = 0; } obj.data = new GridData(finalVal); }
public void AddCoupler(LineCoupler coupler, int index) { lineCouplers[index] = coupler; }
public void SetCouplers(bool flag) { GridManager gridMan = GameObject.Find("GridManager").GetComponent <GridManager>(); List <List <GameObject> > grid = GameManager.Instance.grid; showCouplers = flag; // Reset spawners foreach (GameObject go in couplers) { go.SetActive(false); } if (coordinates[0] - 1 >= 0) { GameObject curBlock = grid[coordinates[0] - 1][coordinates[1]]; LineCoupler lineC = couplers[0].GetComponent <LineCoupler>(); couplers[0].SetActive(flag); lineC.currentBlock = gameObject; if (overrideBlock != BLOCK.END) { lineC.canSpawnLines = true; } } if (coordinates[0] + 1 < gridMan.gridSizeX) { GameObject curBlock = grid[coordinates[0] + 1][coordinates[1]]; LineCoupler lineC = couplers[1].GetComponent <LineCoupler>(); couplers[1].SetActive(flag); lineC.currentBlock = gameObject; if (overrideBlock != BLOCK.END) { lineC.canSpawnLines = true; } } if (coordinates[1] + 1 < gridMan.gridSizeY) { GameObject curBlock = grid[coordinates[0]][coordinates[1] + 1]; LineCoupler lineC = couplers[2].GetComponent <LineCoupler>(); couplers[2].SetActive(flag); lineC.currentBlock = gameObject; if (overrideBlock != BLOCK.END) { lineC.canSpawnLines = true; } } if (coordinates[1] - 1 >= 0) { GameObject curBlock = grid[coordinates[0]][coordinates[1] - 1]; LineCoupler lineC = couplers[3].GetComponent <LineCoupler>(); couplers[3].SetActive(flag); lineC.currentBlock = gameObject; if (overrideBlock != BLOCK.END) { lineC.canSpawnLines = true; } } }