Пример #1
0
    void Start()
    {
        immunTimeGameObj.SetActive(false);
        _defaultColor = _headSR.color;

        _cellSize            = _gs.CellSize;
        transform.localScale = _gs.SnakeScale;
        _currPosInArray      = _gs.StartSnakeArrayPos;
        transform.position   = _gs.GetPosition(_currPosInArray);
        _gs.ChangGameArray(_currPosInArray, 1);

        _minMoveDelay = _speed * 0.8f;
        //_move = MoveCorutine();
        //curGameState = GameState.Game;

        for (int i = 0; i < 3; i++)
        {
            _steps.Insert(0, _currPosInArray);
        }

        if (!MenuHandler.isTipsEnable)
        {
            _dirList.Insert(0, Vector2.left);
            //_inputs.Insert(0, Vector2.left);
        }

        _winTailLength = _gs.WinTailLength;
    }
Пример #2
0
    private void TailLastToFirst(ArrayPosHandler newPos)
    {
        ArrayPosHandler oldPos = _tail.Last().arrayPos;

        _gs.ChangGameArray(oldPos, 0);
        if (_gs.GetGameArrayValue(newPos) != 2)
        {
            _gs.ChangGameArray(newPos, 2);
        }

        if (curGameState == GameState.Immun)
        {
            //if (_tail.Last().transform.GetComponent<SpriteRenderer>().color
            //        != _tail.First().transform.GetComponent<SpriteRenderer>().color)
            if (tailLength % 2 == 0)
            {
                _tail.Last().transform.GetComponent <SpriteRenderer>().color = _tail.First().transform.GetComponent <SpriteRenderer>().color;
            }
        }

        _tail.Last().arrayPos = newPos;
        _tail.Last().transform.position = _gs.GetPosition(newPos);

        SetTailCondition(_tail.Last().transform.gameObject);

        _tail.Insert(0, _tail.Last());
        _tail.RemoveAt(_tail.Count - 1);
    }
Пример #3
0
    private void SpawnTail(ArrayPosHandler spawnPos)
    {
        GameObject t = Instantiate(_tailPrefab
                                   , _gs.positionsArray[spawnPos.x, spawnPos.y]
                                   , Quaternion.identity);

        t.transform.localScale = _gs.SnakeScale;
        t.transform.parent     = _tailContainer.transform;

        SetTailCondition(t);

        if (curGameState == GameState.Immun)
        {
            t.transform.GetComponent <SpriteRenderer>().color
                = _tail.First().transform.GetComponent <SpriteRenderer>().color;
        }

        _tail.Insert(0, new TailClass(t.transform, spawnPos));

        tailLength = (ushort)_tail.Count;
        //Debug
        //if (tail.Count == 6)
        //{
        //    WinGame();
        //}

        if (_tail.Count >= _winTailLength)
        {
            WinGame();
        }
    }
Пример #4
0
    public static ArrayPosHandler operator /(ArrayPosHandler left, float value)
    {
        ArrayPosHandler temp = new ArrayPosHandler();

        temp.x = Convert.ToInt32(Math.Round(left.x / value, 0, MidpointRounding.ToEven));
        temp.y = Convert.ToInt32(Math.Round(left.y / value, 0, MidpointRounding.ToEven));
        return(temp);
    }
Пример #5
0
    public static ArrayPosHandler operator -(ArrayPosHandler left, ArrayPosHandler right)
    {
        ArrayPosHandler temp = new ArrayPosHandler();

        temp.x = left.x - right.x;
        temp.y = left.y - right.y;
        return(temp);
    }
Пример #6
0
    public static ArrayPosHandler operator +(ArrayPosHandler left, Vector2 right)
    {
        ArrayPosHandler temp = new ArrayPosHandler();

        temp.x = left.x + Convert.ToInt32(right.x);
        temp.y = left.y + Convert.ToInt32(right.y);
        return(temp);
    }
Пример #7
0
    private void ResetGame()
    {
        curGameState = GameState.Starting;
        _dirList.Clear();
        _dirList.Insert(0, Vector2.left);

        _loseCount = 0;
        tailLength = 0;

        _gs.ClearGameArray();

        _currPosInArray    = _gs.StartSnakeArrayPos;
        transform.position = _gs.GetPosition(_currPosInArray);
        _gs.ChangGameArray(_currPosInArray, 1);

        _steps.Clear();

        for (int i = 0; i < 3; i++)
        {
            _steps.Insert(0, _currPosInArray);
        }
    }
Пример #8
0
 public TailClass(Transform transform, ArrayPosHandler arrayPos)
 {
     this.transform = transform;
     this.arrayPos  = arrayPos;
 }
Пример #9
0
 public static bool IsTopBorderCrossed(ArrayPosHandler position)
 {
     return(position.x < 0);
 }
Пример #10
0
 public static bool IsRightBorderCrossed(ArrayPosHandler position, int maxValue)
 {
     return(position.y > maxValue);
 }
Пример #11
0
 public static bool IsLeftBorderCrossed(ArrayPosHandler position)
 {
     return(position.y < 0);
 }
Пример #12
0
 public static bool IsBottomBorderCrossed(ArrayPosHandler position, int maxValue)
 {
     return(position.x > maxValue);
 }
Пример #13
0
    private void Move()
    {
        if (_dirList.Count == 0)
        {
            return;
        }

        ArrayPosHandler oldPosInArray = _currPosInArray,
                        newPosInArray;


        if (_dirList.Count > 1)
        {
            newPosInArray = _currPosInArray + _dirList.Last();
            _dirList.RemoveAt(_dirList.Count - 1);
        }
        else
        {
            newPosInArray = _currPosInArray + _dirList.First();
        }

        ArrayPosHandler inputValue = newPosInArray - _currPosInArray;

        if (_inputs.Count > 0)
        {
            _inputs.Insert(0, new Vector2(inputValue.x, inputValue.y));
        }
        else
        {
            _inputs.Insert(0, new Vector2(inputValue.x, inputValue.y));
            _inputs.Insert(0, new Vector2(inputValue.x, inputValue.y));
        }


        if (ArrayPosHandler.IsLeftBorderCrossed(newPosInArray))
        {
            _currPosInArray = new ArrayPosHandler(_currPosInArray.x, _gs.gameArray.GetLength(1) - 1);
        }
        else if (ArrayPosHandler.IsRightBorderCrossed(newPosInArray, _gs.gameArray.GetLength(1) - 1))
        {
            _currPosInArray = new ArrayPosHandler(_currPosInArray.x, 0);
        }
        else if (ArrayPosHandler.IsTopBorderCrossed(newPosInArray))
        {
            _currPosInArray = new ArrayPosHandler(_gs.gameArray.GetLength(0) - 1, _currPosInArray.y);
        }
        else if (ArrayPosHandler.IsBottomBorderCrossed(newPosInArray, _gs.gameArray.GetLength(0) - 1))
        {
            _currPosInArray = new ArrayPosHandler(0, _currPosInArray.y);
        }
        else
        {
            _currPosInArray = newPosInArray;
        }

        transform.position = _gs.GetPosition(_currPosInArray);

        MoveEvent?.Invoke();
        _steps.Insert(0, _currPosInArray);
        _gs.ChangGameArray(_currPosInArray, 1);

        if (_ateFood)
        {
            EatEvent?.Invoke();
            SpawnFood?.Invoke();
            _gs.ChangGameArray(oldPosInArray, 2);
            SpawnTail(oldPosInArray);
            _ateFood = false;
        }
        else if (_ateBonus)
        {
            BonusEvent?.Invoke();
            SpawnTail(oldPosInArray);
            _gs.ChangGameArray(oldPosInArray, 2);
            _ateBonus = false;
        }
        else if (_tail.Count > 0)
        {
            TailLastToFirst(oldPosInArray);
        }
        else
        {
            _gs.ChangGameArray(oldPosInArray, 0);
        }

        if (_steps.Count > 2)
        {
            _steps.RemoveAt(_steps.Count - 1);
        }

        if (_inputs.Count > 2)
        {
            _inputs.RemoveAt(_inputs.Count - 1);
        }
    }