예제 #1
0
    private void Start()
    {
        level = PlayerData.Instance.GetLevelDescription(LevelIndex);

        switch (level.ObstacleType)
        {
        case AsteroidType.SMALL:
            asteroidPrefab = Resources.Load <GameObject>("Asteroid_Small");
            break;

        case AsteroidType.MEDUIM:
            asteroidPrefab = Resources.Load <GameObject>("Asteroid_Medium");
            break;

        case AsteroidType.BIG:
            asteroidPrefab = Resources.Load <GameObject>("Asteroid_Big");
            break;
        }

        asteroidPool = ObjectPoolManager.Instance.GetObjectPool(asteroidPrefab);

        var playerHealth = FindObjectOfType <PlayerController>().GetPlayerModel().PlayerHealth;

        playerHealth
        .ObserveEveryValueChanged(x => x.Value)
        .Subscribe(
            playerLife => GameUI.Instance.UpdatePlayerLifeText(playerLife)
            ).AddTo(this);

        levelCompleted = new ReactiveProperty <bool>();

        Observable.EveryUpdate()
        .Subscribe(_ => {
            levelCompleted.Value = allSpawned && AllObstaclesDestroyed;
        });

        gameOver = playerHealth
                   .CombineLatest(
            levelCompleted,
            (health, completed) => health <= 0 || completed
            ).ToReactiveProperty();

        gameOver
        .ObserveEveryValueChanged(x => x.Value)
        .Where(x => x)
        .Subscribe(_ => {
            GameUI.Instance.ShowGameOverScreen(playerHealth.Value > 0);
            if (playerHealth.Value > 0)
            {
                PlayerData.Instance.LevelCompleted(LevelIndex);
            }
            ObjectPoolManager.Instance.Clear();
        }).AddTo(this);


        StartCoroutine(SpawnObstacles());
    }