private IEnumerator BuildGameTask(Stack <GameRay> rayStack) { Stack <GameRay> testRayStack = new Stack <GameRay> (); while (rayStack.Count > 0) { GameRay ray = rayStack.Pop(); // double check not already created if (gameRoot.transform.FindChild(BuildTileName("tile", ray.origin)) != null) { throw new Exception("PROBLEM!"); } CreateGameTile(ray); PushGameDirections(testRayStack, ray.origin); ProcessGameStack(rayStack, testRayStack, ray); if (Application.isPlaying) { yield return(new WaitForSeconds(stepDelay)); } else { yield return(null); } } Debug.Log(gameObject.name + " build game complete."); }
private void CreateGameTile(GameRay ray) { //HACKy... =P string leftTile = BuildTileName("tile", ray.origin + (Vector3.left * gameStep)); string rightTile = BuildTileName("tile", ray.origin + (Vector3.right * gameStep)); string backTile = BuildTileName("tile", ray.origin + (Vector3.back * gameStep)); string forwardTile = BuildTileName("tile", ray.origin + (Vector3.forward * gameStep)); string leftGame = BuildTileName("game", ray.origin + (Vector3.left * gameStep)); string rightGame = BuildTileName("game", ray.origin + (Vector3.right * gameStep)); string backGame = BuildTileName("game", ray.origin + (Vector3.back * gameStep)); string forwardGame = BuildTileName("game", ray.origin + (Vector3.forward * gameStep)); Transform mazeXform = mazeRoot.transform; Transform gameXform = gameRoot.transform; bool hasLeftTile = mazeXform.FindChild(leftTile) != null; bool hasRightTile = mazeXform.FindChild(rightTile) != null; bool hasBackTile = mazeXform.FindChild(backTile) != null; bool hasForwardTile = mazeXform.FindChild(forwardTile) != null; bool hasLeftGame = gameXform.FindChild(leftGame) != null; bool hasRightGame = gameXform.FindChild(rightGame) != null; bool hasBackGame = gameXform.FindChild(backGame) != null; bool hasForwardGame = gameXform.FindChild(forwardGame) != null; bool hasLeft = hasLeftTile || hasLeftGame; bool hasRight = hasRightTile || hasRightGame; bool hasBack = hasBackTile || hasBackGame; bool hasForward = hasForwardTile || hasForwardGame; int count = 0; count = hasLeft ? count + 1 : count; count = hasRight ? count + 1 : count; count = hasBack ? count + 1 : count; count = hasForward ? count + 1 : count; if (count == 3) { if ((!hasLeft && hasRightGame) || (!hasRight && hasLeftGame) || (!hasBack && hasForwardGame) || (!hasForward && hasBackGame)) { CreateGameWall(ray); } else { CreateGameCorner(ray); } } else { CreateGameCorner(ray); } }
private void CreateGameWall(GameRay ray) { if (wallGamePrefab == null) { throw new InvalidOperationException("Wall prefab should not be null"); } GameObject obj = GameObject.Instantiate(wallGamePrefab); Transform objXform = obj.transform; obj.name = BuildTileName("game", ray.origin); objXform.position = ray.origin + cornerOffset; objXform.forward = ray.dir; objXform.parent = gameRoot.transform; }
private void BuildGameCore(GameRay ray) { Stack <GameRay> stack = new Stack <GameRay> (); stack.Push(ray); if (Application.isPlaying) { StartCoroutine(BuildGameTask(stack)); } else { //HACK: editor doesn't support coroutines IEnumerator e = BuildGameTask(stack); while (e.MoveNext()) { } } }
private void ProcessGameStack(Stack <GameRay> rayStack, Stack <GameRay> testRayStack, GameRay ray) { Transform mazeXform = mazeRoot.transform; Transform gameXform = gameRoot.transform; while (testRayStack.Count > 0) { GameRay testRay = testRayStack.Pop(); string nameTile = BuildTileName("tile", testRay.origin + (testRay.dir * gameStep)); string nameGame = BuildTileName("game", testRay.origin + (testRay.dir * gameStep)); bool has = (mazeXform.FindChild(nameTile) != null) || (gameXform.FindChild(nameGame) != null); if (!has) { rayStack.Push(new GameRay(testRay.origin + (testRay.dir * gameStep), testRay.dir)); } } }
private bool ProcessStacks(Stack <GameRay> stack, Stack <GameRay> queue) { while (queue.Count > 0) { GameRay testRay = queue.Pop(); RaycastHit info; if (!Physics.Raycast(testRay.origin, testRay.dir, out info, step)) { Debug.DrawRay(testRay.origin, testRay.dir * step, Color.green, stepDelay * 10f); // Debug.Log("miss: " + origin + ", " + (dir * step)); stack.Push(new GameRay(testRay.origin + (testRay.dir * step), testRay.dir)); return(true); } else { // Debug.LogWarning("hit: " + info.collider.name + ", " + origin + ", " + (dir * step)); Debug.DrawRay(testRay.origin, testRay.dir * step, Color.red, stepDelay * 10f); } } return(false); }
private void CreateCorner(GameRay ray) { { if (cornerPrefab == null) { throw new InvalidOperationException("Corner prefab should not be null"); } GameObject obj = GameObject.Instantiate(cornerPrefab); Transform objXform = obj.transform; Vector3 origin = ray.origin; obj.name = BuildTileName("tile", origin); objXform.position = origin; objXform.forward = ray.dir; objXform.parent = mazeRoot.transform; } { if (wallPrefab == null) { throw new InvalidOperationException("Wall prefab should not be null"); } GameObject obj = GameObject.Instantiate(wallPrefab); Transform objXform = obj.transform; Vector3 origin = ray.origin + (ray.dir * -1f * gameStep); obj.name = BuildTileName("tile", origin); objXform.position = origin; objXform.forward = ray.dir; objXform.parent = mazeRoot.transform; } }