Beispiel #1
0
    private IEnumerator nextTurn()
    {
        levelController.AddTurn();

        var tileList = TileGenerator.GetTileList();

        var ratTile = tileList.FindAll(t => t.GetBadGuy() is Rat).OrderBy(t => t.GetBadGuy().Life());

        foreach (var ratT in ratTile)
        {
            if (ratT.GetBadGuy().DecreaseLife() <= 0)
            {
                Debug.Log("Deduct User Life");

                var throwingProduct = sherDog.GetThrowableObject();
                throwingProduct.SetActive(true);
                throwingProduct.transform.position = new Vector3(ratT.transform.position.x, ratT.transform.position.y, throwingProduct.transform.position.z);
                yield return(StartCoroutine(Util.SmoothMovement(throwingProduct.transform, sherDog.GetThrowingPosition(), 10f)));

                throwingProduct.SetActive(false);
                sherDog.Hit();
                var ranSound     = Random.Range(0, 20);
                var hitSoundname = (ranSound) > 10?"hit":"hit2";
                MainSoundSrc.PlaySound(hitSoundname);

                gController.mDecreaseLifeGuage();
                gController.mBrokeCombo();

                yield return(new WaitForSeconds(1f));

                //Destroy( ratT.RemoveBadGuy().gameObject );
                ratT.GetBadGuy().SetLife(levelController.GetRatTurns());
            }

            var availableTileList = ratT.GetCrossBuildableTileList();
            if (availableTileList.Count() == 0)
            {
                continue;
            }
            var selectedIndex = (int)Random.Range(0f, availableTileList.Count() - 0.01f);
            var rat           = ratT.RemoveBadGuy();
            availableTileList[selectedIndex].AddBadGuy(rat);
        }


        var moleTile = tileList.FindAll(t => t.GetBadGuy() is Mole);
        var moleList = moleTile.Select(mt => mt.RemoveBadGuy() as Mole).OrderBy(m => m.Life());

        var buildableTile  = tileList.FindAll(t => t.IsBuildAble());
        var buildableCount = buildableTile.Count;

        foreach (var mole in moleList)
        {
            if (mole.DecreaseLife() <= 0)
            {
                Debug.Log("Deduct User Life");

                var throwingProduct = sherDog.GetThrowableObject();
                throwingProduct.SetActive(true);
                throwingProduct.transform.position = new Vector3(mole.transform.position.x, mole.transform.position.y, throwingProduct.transform.position.z);
                yield return(StartCoroutine(Util.SmoothMovement(throwingProduct.transform, sherDog.GetThrowingPosition(), 10f)));

                throwingProduct.SetActive(false);
                sherDog.Hit();
                var ranSound     = Random.Range(0, 20);
                var hitSoundname = (ranSound) > 10?"hit":"hit2";
                MainSoundSrc.PlaySound(hitSoundname);

                gController.mDecreaseLifeGuage();
                gController.mBrokeCombo();

                yield return(new WaitForSeconds(1f));

                //Destroy( mole.gameObject );
                mole.SetLife(levelController.GetMoleTurns());
            }

            var newHomeIndex = (int)Random.Range(0f, buildableCount - 0.01f);
            buildableTile[newHomeIndex].AddBadGuy(mole);
            buildableCount--;
            buildableTile.RemoveAt(newHomeIndex);
        }

        //Create Mole
        var moleChance = levelController.GetMoleChance(tileList.Count(t => t.GetBadGuy() is Mole));
        var ratChance  = levelController.GetRatChance(tileList.Count(t => t.GetBadGuy() is Rat));
        var chance     = Random.Range(0, 100);

        if (buildableCount > 0 && chance < moleChance + ratChance)
        {
            BadGuy bg = null;

            if (chance < ratChance)
            {
                bg = Instantiate(ratPrefab) as BadGuy;
                bg.SetLife(levelController.GetRatTurns());
            }
            else
            {
                bg = Instantiate(molePrefab) as BadGuy;
                bg.SetLife(levelController.GetMoleTurns());
            }

            var newHomeIndex = (int)Random.Range(0f, buildableCount - 0.01f);
            buildableTile[newHomeIndex].AddBadGuy(bg, true);
            buildableCount--;
            buildableTile.RemoveAt(newHomeIndex);
        }

        yield return(new WaitForSeconds(0.5f));

        if (buildableCount <= 0)
        {
            //Game Over
            Debug.Log("Game Over");
            gController.mGameOver();
        }
        else
        {
            var tileLeft = (float)buildableCount / TileGenerator.TotalSize;
            if (tileLeft <= 0.4f || gController.GetLife() == 1)
            {
                sherDog.SetTired(true);
            }
            else
            {
                sherDog.SetTired(false);
            }
        }

        //Check Game State
        if (!isGameOver)
        {
            if (oryorCharge < 100)
            {
                ProductFactory.GetInstance().GenerateProduct(tileList.Any(t => t.GetBadGuy() != null));
            }
            else
            {
                oryorCharge           = 0;
                oryorGauge.fillAmount = 0;
                ProductFactory.GetInstance().GenerateOryor();
                MainSoundSrc.PlaySound("oryorready");
            }
        }
    }