public static void GenerateRandomBallGrid(BallGrid ballGrid) { for (int z = ballGrid.zMax; z >= ballGrid.zMin; z--) { Transform wallTF = ballGrid.GetOrCreateLayer(z); int yBottom = ballGrid.yMin; int yTop = ballGrid.yMax; for (int y = yBottom; y <= yTop; y++) { Transform rowTF = ballGrid.GetOrCreateRow(z, y, wallTF); int xLeft = ballGrid.xMin; int xRight = ballGrid.xMax; if (Utils.IsOdd(ballGrid.xSize)) { if ((ballGrid.xSize + 1) % 4 == 0) { if ((Utils.IsEven(z) && Utils.IsEven(y)) || (Utils.IsOdd(z) && Utils.IsOdd(y))) { xLeft++; } } else { if ((Utils.IsEven(z) && Utils.IsOdd(y)) || (Utils.IsEven(y) && Utils.IsOdd(z))) { xRight--; } } } for (int x = xLeft; x <= xRight; x++) { Transform colTF = ballGrid.GetOrCreateColumn(x, rowTF); GridBall gridBall = BallGridEditor.GenerateGridBall(colTF); gridBall.RefreshGridCoords(); } } } }
public GridBall SnapTo(BallGrid ballGrid, GridBallSensorNode sensorNode) { this._state = State.Snapping; this.StopCoroutine("CountdownToRecycle"); this.ballCollider.enabled = false; this.ballRigidBody.velocity = Vector3.zero; this.ballRigidBody.angularVelocity = Vector3.zero; // instantiate a grid ball in the correct spot and recycle this ball GridBall gridBall = sensorNode.GetComponentInParent <GridBall>(); Vector3i direction = new Vector3i( Mathf.RoundToInt(Utils.RoundAwayFromZero(sensorNode.transform.localPosition.x)), Mathf.RoundToInt(Utils.RoundAwayFromZero(sensorNode.transform.localPosition.y)), Mathf.RoundToInt(Utils.RoundAwayFromZero(sensorNode.transform.localPosition.z)) ); Vector3i newPos = BallGrid.GetNeighborCoords(new Vector3i(gridBall.gridCoords), direction); Transform wallTF = ballGrid.GetOrCreateLayer(Mathf.RoundToInt(newPos.z)); Transform rowTF = ballGrid.GetOrCreateRow(Mathf.RoundToInt(newPos.z), Mathf.RoundToInt(newPos.y), wallTF); Transform colTF = ballGrid.GetOrCreateColumn(Mathf.RoundToInt(newPos.x), rowTF); GridBall newGridBall = colTF.GetComponentInChildren <GridBall>(); if (newGridBall == null) { newGridBall = GridBall.Generate(colTF, wasBallShot: true); newGridBall.RefreshGridCoords(); } else { Debug.LogWarningFormat("grid ball already exists at zyx {0}", newPos.ToString()); } newGridBall.SetColor(this.ballColor); this.Recycle(); return(newGridBall); }