public void OnDifficultySelected(Difficulty difficulty)
        {
            if (!_selectedButton)
                return;

            var def = new LevelDef(World, _selectedButton.Level, difficulty);
            TransitionManager.TransferToLevel(def);
        }
        public void Load(LevelDef def)
        {
            if (IsLoaded)
            {
                Debug.LogError("trying to load again.");
                return;
            }

            _levelDef = def;
            _map = MapGenerator.Generate(def);
            _starsLeft = new HashSet<Star>(_map.Stars);
            Star.OnGetStar += OnGetStar;
            _getStarRegistered = true;

            _ball = MapFactory.InstantiateBall(_map.StartPoisition);
            _ball.transform.SetParent(transform, false);
            var ballController = _ball.gameObject.AddComponent<BallController>();
            ballController.Ball = _ball;
        }
        public static void Win(LevelDef levelDef)
        {
            var oldClearState = Get(levelDef.World, levelDef.Level);
            var newClearState = levelDef.Difficulty == Difficulty.Easy ? LevelClearState.Easy : LevelClearState.Hard;

            if (oldClearState.HasValue)
            {
                var oldClearStateValue = oldClearState.Value;
                if (oldClearStateValue == LevelClearState.Hard && levelDef.Difficulty == Difficulty.Hard)
                {
                    newClearState = LevelClearState.Perfect;
                }
                else if (levelDef.Difficulty == Difficulty.Easy)
                {
                    return;
                }
            }

            Set(levelDef.World, levelDef.Level, newClearState);
            Unlocked = new WorldAndLevel(levelDef.World, levelDef.Level).Next();
        }
        private static TiledSharp.Map Load(LevelDef def)
        {
            var path = "Level/episode" + def.World.ToNumber() + "/" + def.Level.ToNumber();
            if (def.Difficulty == Difficulty.Easy) path += "e";

            var tmxContent = Resources.Load<TextAsset>(path);
            if (tmxContent == null)
            {
                Debug.LogError("map is not exists: " + path);
                return null;
            }

            var tmxReader = new StringReader(tmxContent.text);
            var tmx = new TiledSharp.Map(tmxReader, pathToLoad =>
            {
                Debug.Log(pathToLoad);
                return null;
            });

            return tmx;
        }
 public static void TransferToLevel(LevelDef def)
 {
     LevelController.LevelDef = def;
     Application.LoadLevel("Level");
 }
 public static Map Generate(LevelDef def)
 {
     return Generate(Load(def));
 }