IEnumerator MoveLeft() { yield return(StartCoroutine(TurnTo(Direction.Left))); yield return(StartCoroutine(MoveForwards(Vector3.left))); yield return(StartCoroutine(FinishMovement())); cubeCoord = cubeCoord.Move(-1, 0); }
IEnumerator MoveDown() { yield return(StartCoroutine(TurnTo(Direction.Down))); yield return(StartCoroutine(MoveForwards(Vector3.back))); yield return(StartCoroutine(FinishMovement())); cubeCoord = cubeCoord.Move(0, -1); }
IEnumerator MoveUp() { yield return(StartCoroutine(TurnTo(Direction.Up))); yield return(StartCoroutine(MoveForwards(Vector3.forward))); yield return(StartCoroutine(FinishMovement())); cubeCoord = cubeCoord.Move(0, 1); }
void FixedUpdate() { if (!playerIsMoving && (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))) { Direction nextDirection = GetNextDirection(); CubeCoord nextCubeCoord = GetNextCubeCoord(); if (nextCubeCoord.x >= 0 && nextCubeCoord.x < GameController.Instance.levelWidth && nextCubeCoord.y >= 0 && nextCubeCoord.y < GameController.Instance.levelHeight) { Cube nextCube = GameController.Instance.cubes[nextCubeCoord]; if (nextCube.type != CubeType.NoCube && !nextCube.isVisited) { playerIsMoving = true; nextCube.isVisited = true; if (nextCube.type == CubeType.Finish) { isOnFinishCube = true; } switch (nextDirection) { case Direction.Left: StartCoroutine(MoveLeft()); break; case Direction.Right: StartCoroutine(MoveRight()); break; case Direction.Down: StartCoroutine(MoveDown()); break; case Direction.Up: StartCoroutine(MoveUp()); break; } } } } }
void Start() { cubes = new Dictionary <CubeCoord, Cube>(levelWidth * levelHeight); foreach (Transform cube in cubesObject.transform) { var x = (int)cube.localPosition.x; var y = (int)cube.localPosition.z; // Так как Vector3.y отвечает за высоту, я использую Vector3.z для CubeCoord.y var cubeCoord = new CubeCoord(x, y); switch (cube.tag) { case "Cube": cubes[cubeCoord] = Cube.cube; break; case "StartCube": cubes[cubeCoord] = Cube.startCube; break; case "FinishCube": cubes[cubeCoord] = Cube.finishCube; break; default: Debug.LogError("[GameController] Неизвестный тег куба '" + cube.tag + "'!"); cubes[cubeCoord] = Cube.noCube; break; } } foreach (int x in Enumerable.Range(0, levelWidth)) { foreach (int y in Enumerable.Range(0, levelHeight)) { CubeCoord coord = new CubeCoord(x, y); if (!cubes.ContainsKey(coord)) { cubes[coord] = Cube.noCube; } } } }
void FixedUpdate() { if (!freezeMovement && Input.anyKey) { Direction nextDirection = Direction.Up; int nextCubeX = cubeX; int nextCubeY = cubeY; if (Input.GetKey(KeyCode.A)) { nextCubeX = cubeX - 1; nextDirection = Direction.Left; } else if (Input.GetKey(KeyCode.D)) { nextCubeX = cubeX + 1; nextDirection = Direction.Right; } else if (Input.GetKey(KeyCode.S)) { nextCubeY = cubeY - 1; nextDirection = Direction.Down; } else if (Input.GetKey(KeyCode.W)) { nextCubeY = cubeY + 1; nextDirection = Direction.Up; } if (nextCubeX >= 0 && nextCubeX < GameController.Instance.levelWidth && nextCubeY >= 0 && nextCubeY < GameController.Instance.levelHeight) { CubeCoord nextCubeCoord = new CubeCoord(nextCubeX, nextCubeY); Cube nextCube = GameController.Instance.cubes[nextCubeCoord]; if (nextCube.type != CubeType.NoCube && !nextCube.isVisited) { freezeMovement = true; nextCube.isVisited = true; if (nextCube.type == CubeType.Finish) { isOnFinishCube = true; } switch (nextDirection) { case Direction.Left: StartCoroutine(MoveLeft()); break; case Direction.Right: StartCoroutine(MoveRight()); break; case Direction.Down: StartCoroutine(MoveDown()); break; case Direction.Up: StartCoroutine(MoveUp()); break; } } } } }