예제 #1
0
            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);
            }