Esempio n. 1
0
    void Awake()
    {
        AdControl.hideBanner();

        Application.targetFrameRate = 60;

        gameState = GameConst.GAME_STATE.PLAYING;

        bonusStyleControl = GetComponent<BonusStyleControl> ();

        trans = GetComponent<Transform> ();

        canvas = GameObject.Find ("Canvas");

        score = canvas.GetComponent<Score>();

        panelGameOver = goPanelGameover.GetComponent<PanelGameOver> ();

        panelPause = goPanelPause.GetComponent<PanelPause> ();

        listSnake = new List<SnakeBody> ();
        listFood = new List<Food>();
        snakeState = GameConst.snakeState.MOVING;

        AudioSource[] audios = gameObject.GetComponents<AudioSource> ();
        combineSound = audios[0];
        eatSound = audios[1];
        crushSound = audios[2];
    }
Esempio n. 2
0
    private void snakeWillDie()
    {
        snakeState = GameConst.snakeState.WILL_DIE;

        deadCountdown = GameConst.DEAD_COUNTDOWN;
    }
Esempio n. 3
0
 void startBonus()
 {
     ++bonusCount;
     bonusAC.SetBool ("play",true);
     snakeState = GameConst.snakeState.WAITING;
     StartCoroutine(bonusTime());
 }
Esempio n. 4
0
    //eat food
    private void snakeEat(Food food)
    {
        GameObject body = (GameObject)Instantiate (prefabSnakeBody,listSnake[0].trans.localPosition, Quaternion.identity);
        SnakeBody head = body.GetComponent<SnakeBody> ();
        head.num = food.num;
        body.GetComponent<Transform> ().SetParent (trans,false);
        listSnake.Insert (0,head);

        //添加吃东西效果
        float delayTime = 0f;
        const float UNIT_TIME = 0.15f;
        head.addEatingEffect (head.num,delayTime);
        int nextNum;
        for (int i = 1; i < listSnake.Count - 1; i++) {
            delayTime += UNIT_TIME;
            SnakeBody t = listSnake[i];
            nextNum = listSnake[i + 1].num;
            t.addEatingEffect(nextNum,delayTime);
        }
        if (listSnake.Count > 2) {
            delayTime += UNIT_TIME;
            SnakeBody tail = listSnake [listSnake.Count - 1];
            tail.addEatingEffect(-1,delayTime);
        }

        //蛇头延续之前方向
        head.moveTo (GameConst.getLineAndRow (food.localPosition));

        snakeState = GameConst.snakeState.EATING;
        //
        StartCoroutine (eating());
    }
Esempio n. 5
0
    private void snakeDie()
    {
        snakeState = GameConst.snakeState.DEAD;

        crushSound.Play();

        bool levelComplete = false;
        int totalGrid = (GameConst.HALF_ROW * 2 + 1) * (GameConst.HALF_LINE * 2 + 1);
        totalGrid -= 1;
        int count2048 = 0;
        foreach(var body in listSnake)
        {
            if(body.num == 2048)
            {
                ++count2048;
            }
        }
        if (totalGrid <= count2048) {
            levelComplete = true;
        }

        panelGameOver.updateScore (score.score,levelComplete);

        animtorPanelGameOver.SetBool ("gameIsOver",true);

        AdControl.showBanner();

        gameState = GameConst.GAME_STATE.GAMEOVER;
    }
Esempio n. 6
0
    IEnumerator eating()
    {
        //蛇身后退
        for (int i = 1; i < listSnake.Count - 1; i++) {
            SnakeBody body = listSnake[i];
            GRIDPOS pos = GameConst.getLineAndRow(body.trans.localPosition);
            SnakeBody next = listSnake[i + 1];

            body.trans.localPosition = next.trans.localPosition;
            body.moveTo(pos);
        }

        foreach (var body in listSnake) {
            body.blink(true);
        }
        StartCoroutine (stopBlink(0.2f));

        //check body combine
        snakeState = GameConst.snakeState.COMBINE;
        StartCoroutine(bodyCombine());

        yield return null;
    }
Esempio n. 7
0
    IEnumerator bonusTime()
    {
        if (bonusCount > 0) {
            yield return new WaitForSeconds (1f);
            //find empty position
            List<GRIDPOS> tlist = bonusStyleControl.getRandomStyle();

            //清掉食物
            foreach(var food in listFood)
            {
                GameObject.Destroy(food.gameObject);
            }
            listFood.Clear();

            int num = GameConst.BONUS_NUMS[Random.Range(0,GameConst.BONUS_NUMS.Length)];
            foreach(var trowAndLine in tlist)
            {
                Vector3 tpos = new Vector3();
                tpos.x = GameConst.GRID_SIZE * trowAndLine.line;
                tpos.y = GameConst.GRID_SIZE * trowAndLine.row;
                tpos.z = 0f;
                GameObject food = (GameObject)Instantiate (prefabFood,tpos,Quaternion.identity);
                food.GetComponent<Transform>().SetParent(trans,false);
                int targetNum = num;
                food.GetComponent<Food>().num = targetNum;
                listFood.Add (food.GetComponent<Food>());
            }
            yield return new WaitForSeconds (0.5f);
        }
        snakeState = GameConst.snakeState.MOVING;
        bonusAC.SetBool ("play",false);
        yield return null;
    }
Esempio n. 8
0
    //body combine
    IEnumerator bodyCombine()
    {
        while (true) {
            SnakeBody head = listSnake[0];
            SnakeBody second = null;
            if(listSnake.Count > 1)
            {
                second = listSnake[1];
            }
            if(second)
            {
                if(head.num < GameConst.MAX_NUM)
                {
                    if(head.num == second.num)
                    {
                        //add score
                        score.addScore(head.num);
                        ScoreEffect t = Instantiate(prefabScoreEffect);
                        Vector3 scorePos = Camera.main.WorldToScreenPoint(head.trans.localPosition + new Vector3(1f,0,0));
                        t.gameObject.transform.SetParent(canvas.transform,false);
                        t.init(scorePos,head.num);

                        //add sound
                        combineSound.Play();

                        //check speed up
                        float tScale = 1f;
                        for(int i = GameConst.timeScaleScore.Length -1; i >= 0 ;i--)
                        {
                            if(score.score >= GameConst.timeScaleScore[i])
                            {
                                tScale = GameConst.timeScaleValue[i];
                                break;
                            }
                        }
                        GameConst.timePassUnitGrid = GameConst.TIME_PASS_UNIT_GRID * tScale;

                        yield return StartCoroutine(combineAnim());
                    }else
                    {
                        //停顿一下,防止误操作
                        GRIDPOS headPos = GameConst.getLineAndRow(head.trans.localPosition);
                        if(headPos.row == GameConst.HALF_ROW || headPos.row == -GameConst.HALF_ROW||
                           headPos.line == GameConst.HALF_LINE || headPos.line == -GameConst.HALF_LINE)
                        {
                            yield return StartCoroutine(wait(0.02f));
                        }else
                        {
                            yield return StartCoroutine(wait(0.03f));
                        }
                        break;
                    }
                }else
                {
                    break;
                }
            }else
            {
                break;
            }
        }

        if(listSnake[0].num == 2048)
        {
            startBonus();
        }else
        {
            snakeState = GameConst.snakeState.MOVING;
        }

        yield return null;
    }