コード例 #1
0
 public void OnTriggerEnter2D(Collider2D other)
 {
     if (other.tag == "Router")
     {
         var router = other.gameObject.GetComponent <RouterController>();
         if (router.CanMove(_nextDir))
         {
             ChangeDirection(_nextDir);
             _nextDir = Direction.None;
             return;
         }
         if (!router.CanMove(_dir))
         {
             Stopped = true;
             _router = router;
             return;
         }
     }
     if (other.tag == "Teleport")
     {
         var teleport = other.gameObject.GetComponent <TeleportController>();
         transform.position = new Vector3(teleport.Distination.x, teleport.Distination.y, 0);
     }
     if (other.tag == "Food")
     {
         other.gameObject.active = false;
         _score += 10;
     }
     if (other.tag == "SuperFood")
     {
         other.gameObject.active = false;
         SuperMode     = true;
         SuperModeTime = SuperModeTimeDefault;
         var monsters = GameObject.FindGameObjectsWithTag("Monster");
         foreach (var monster in monsters)
         {
             var monsterController = monster.GetComponent <MonsterController>();
             monsterController.SetSuperMode(true);
         }
         _score += 50;
     }
     if (other.tag == "Monster" && !IsDead)
     {
         if (SuperMode)
         {
             _score += 100;
             var monster = other.gameObject.GetComponent <MonsterController>();
             monster.Respawn();
         }
         else
         {
             _animator.SetTrigger("IsDead");
             IsDead = true;
             _lives--;
             _deathTimes = DeathTimesDefault;
         }
     }
 }
コード例 #2
0
        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)]);
        }