Beispiel #1
0
    private float timeToDisappear; //for SpecialFood

    private void Start()
    {
        food               = FindObjectOfType <Food>();
        specialFood        = FindObjectOfType <SpecialFood>();
        snakeBehaviour     = FindObjectOfType <SnakeBehaviour>();
        FoodSpawned        = false;
        SpecialFoodSpawned = false;
        RanTime();
    }
Beispiel #2
0
    // Update is called once per frame

    /*<summary>
     * Update is responsible for movement, ar for instantiation of special food.
     * </summary>
     */
    void Update()
    {
        //if game is over don't want snake to move
        if (GameManager.instance.gameOver)
        {
            return;
        }

        //to be sure if game could be continued if for some reason food was not created
        if (GameObject.FindGameObjectsWithTag("NormalFood").Length == 0)
        {
            foodPrefab.GetComponent <Food>().AddNewOne();
        }

        //counting time to instantiate new special food object
        timeToNewSpecialFood -= Time.deltaTime;

        if (timeToNewSpecialFood <= 0) // if all time has passed
        {
            //create new special food object
            SpecialFood sf = specialFoodPrefab.GetComponent <SpecialFood>();
            sf.AddNewOne();

            //random new time to create new special food object
            //range is selected to not create new object before old one is not expired
            timeToNewSpecialFood = Random.Range(2 * sf.timeToDestroy, 2 * sf.timeToDestroy + maxTimeToNewSpecialFood);
        }

        //if there is not a time for a next move
        if (Time.time < nextFrameTime)
        {
            return;
        }

        nextFrameTime += interval;              // set up a new move time

        SnakeElement.MoveTail(snake);           //firstly move a snake tail

        SnakeHead head = snake[0] as SnakeHead; // head will always be firs member

        //check if rotation is set up
        if (isTurningLeft)
        {
            isTurningLeft = false;
            head.RotateHeadCounterClockwise();
        }
        else if (isTurningRight)
        {
            isTurningRight = false;
            head.RotateHeadClockwise();
        }

        SnakeElement.MoveHead(snake); // move head including rotation
    }
Beispiel #3
0
    private void OnTriggerEnter2D(Collider2D collision)
    {
        GameObject obj = collision.gameObject;

        if (obj.GetComponent <Food>())
        {
            Food food = collision.GetComponent <Food>();
            food.DestroyAndAddPoints();
            snakeGrow = true;
            foodSpawner.FoodSpawned = false;
            Destroy(obj);
        }
        else if (obj.GetComponent <SpecialFood>())
        {
            SpecialFood spFood = collision.GetComponent <SpecialFood>();
            spFood.DestroyAndAddPoints();
            snakeGrow = true;
            foodSpawner.SpecialFoodSpawned = false;
        }
        else
        {
            levMan.LoadScene("03_GameOver");
        }
    }