private Direction GetNewDirection(RouterController router) { var dirs = new List <Direction>(); var allDirs = new[] { Direction.Left, Direction.Up, Direction.Right, Direction.Down }; foreach (var dir in allDirs) { if (router.CanMove(dir)) { dirs.Add(dir); } } return(dirs[_rand.Next(dirs.Count)]); }
public void Update() { UpdateUI(); SuperModeTime--; if (SuperModeTime == 0) { SuperMode = false; var monsters = GameObject.FindGameObjectsWithTag("Monster"); foreach (var monster in monsters) { var monsterController = monster.GetComponent <MonsterController>(); monsterController.SetSuperMode(false); } } if (IsDead) { _deathTimes--; if (_deathTimes == 0) { if (_lives == -1) { GameOverText.gameObject.active = true; gameObject.active = false; return; } IsDead = false; ChangeDirection(Direction.Right); transform.position = new Vector3(0f, -0.65f, 0f); } return; } var inputDir = GetDirectionFromInput(); if (Stopped) { if (!_router.CanMove(inputDir)) { return; } ChangeDirection(inputDir); Stopped = false; } else if (inputDir != Direction.None && _dir != inputDir) { var reverseDir = GetReverseDirection(_dir); if (inputDir == reverseDir) { ChangeDirection(inputDir); } else { _nextDir = inputDir; } } var directionVector = Vector3.zero; switch (_dir) { case Direction.Left: directionVector = Vector3.left; break; case Direction.Up: directionVector = Vector3.up; break; case Direction.Right: directionVector = Vector3.right; break; case Direction.Down: directionVector = Vector3.down; break; } transform.position = Vector3.MoveTowards(transform.position, transform.position + directionVector, Speed * Time.deltaTime); }