private void Move()
    {
        SnakeBlock newHead = snake.GetNewHead(moveX, moveY);

        if (!CheckCollision(newHead))
        {
            if (eating)
            {
                eatingCounter--;
            }
            snake.MoveSnake(moveX, moveY);
            if (snake.head.x == food.x && snake.head.y == food.y)
            {
                Debug.Log("eating");
                newTail = food;
                Debug.Log(newTail.x);
                Debug.Log(newTail.y);
                eating = true;

                eatingCounter = snakeLength;
                foodAvailable = false;
            }
        }

        if (changingDirection)
        {
            changingDirection = false;
        }
    }
Exemplo n.º 2
0
    public void Feed(Entity collector, CollectableType collectedType)
    {
        _snakeEater._nextTailParts.Add(collectedType);
        SnakeBlock collectorSnakeBlock = (SnakeBlock)collector;

        collectorSnakeBlock.HasFood = true;
    }
Exemplo n.º 3
0
    private void MovingObstacle()
    {
        int count = 20;
        TraversalDirection direction = TraversalDirection.LEFT;
        GridNode           node      = CheckforScopeToMove(direction, count);

        if (node == null)
        {
            direction = TraversalDirection.RIGHT;
            node      = CheckforScopeToMove(direction, count);
        }
        if (node == null)
        {
            direction = TraversalDirection.TOP;
            node      = CheckforScopeToMove(direction, count);
        }
        if (node == null)
        {
            direction = TraversalDirection.BOTTOM;
            node      = CheckforScopeToMove(direction, count);
        }
        if (node != null)
        {
            GameObject movingObs = gridManager.InstantiateObstacle(staticObstacleprefab_0, node, PoolManager.GetInstance().GetPoolContainer(false));
            SnakeBlock block     = movingObs.AddComponent <SnakeBlock>();
            StartCoroutine(MoveObstacle(block, node, direction, count));
        }
    }
Exemplo n.º 4
0
    public void Start()
    {
        // find the model
        m_Model = FindObjectOfType <SnakeModel>();

        // create the 3 initial blocks
        var obj = Instantiate(blockReference, Vector3.zero, Quaternion.identity);

        m_Model.SnakeBlocks.Add(obj);

        obj = Instantiate(blockReference,
                          new Vector3(-1, 0, 0), Quaternion.identity);
        m_Model.SnakeBlocks.Add(obj);

        obj = Instantiate(blockReference,
                          new Vector3(-2, 0, 0), Quaternion.identity);
        m_Model.SnakeBlocks.Add(obj);

        // fruit
        m_Fruit = Instantiate(
            blockReference,
            new Vector3(
                UnityEngine.Random.Range(-10, 10),
                UnityEngine.Random.Range(-10, 10),
                0), Quaternion.identity);

        StartCoroutine(Move());
    }
    private void LoadSnakes(TimeTravelData timeTravelData)
    {
        Debug.Log("Time travel Load Snakes");
        foreach (var snake in timeTravelData.snakes)
        {
            List <SnakeBlock> newThisSnake = new List <SnakeBlock>();
            foreach (SnakeTailData snakeTail in snake.Value)
            {
                GameObject newSnakeTailGO = Instantiate(snakePrefabsSettings.TailBlockPrefab, snakeTail.parent);
                SnakeBlock newSnakeTail   = newSnakeTailGO.GetComponent <SnakeBlock>();

                newSnakeTail.currentGridCell = snakeTail.entityCell;
                newSnakeTail.BlockType       = snakeTail.type;
                newSnakeTail.IsHead          = snakeTail.isHead;
                if (newSnakeTail.IsHead)
                {
                    newSnakeTail.spriteRenderer.sprite = newSnakeTail.headSprite;
                    snake.Key.CurrentHead = newSnakeTail;
                }
                newSnakeTail.spriteRenderer.color = snakeTail.color;

                newThisSnake.Add(newSnakeTail);
            }
            snake.Key.ThisSnake = newThisSnake;
        }
    }
Exemplo n.º 6
0
 public override void OnMoved(SnakeBlock block)
 {
     for (int i = 1; i < blocks.Count; i++)
     {
         blocks[i].OnMoved(i == 0 ? null : blocks[i - 1]);
     }
 }
    //After request references, every snake or collectable will answer back sending their referencies
    public void OnGetReference(Tuple <int, GameObject> reference)
    {
        int        id          = reference.Item1;
        GameObject referenceGO = reference.Item2;

        foreach (TimeTravelData timeTravelData in snakesTimeTravelData)
        {
            if (timeTravelData.id == id)
            {
                Entity     entity     = referenceGO.GetComponent <Entity>();
                SnakeBlock snakeBlock = entity.GetComponent <SnakeBlock>();
                if (snakeBlock != null)
                {
                    if (snakeBlock.IsHead)
                    {
                        Snake thisHeadSnake = snakeBlock.GetComponentInParent <Snake>();
                        timeTravelData.AddSnake(thisHeadSnake);
                    }
                }
                else if (entity is ICollectable)
                {
                    timeTravelData.AddFood(entity);
                }
            }
        }
    }
Exemplo n.º 8
0
    void GenerateBlock()
    {
        var block = Instantiate(blockPrefab.gameObject).GetComponent <SnakeBlock>();

        blocks.Add(block);
        block.snake = this;
        lastBlock   = block;
    }
    private bool CompareSnakeBlocks(SnakeBlock firstBlock, SnakeBlock secondBlock)
    {
        bool status = false;

        if (firstBlock.x == secondBlock.x && firstBlock.y == secondBlock.y)
        {
            status = true;
        }

        return(status);
    }
    private void InstantiateNewSnake(int x, int y, CollectableType block)
    {
        GameObject tailPrefab = _snakeSettings.SnakePrefabsData.TailBlockPrefab;
        GameObject newTailGO  = Instantiate(tailPrefab, transform);
        SnakeBlock newTail    = newTailGO.GetComponent <SnakeBlock>();

        newTail.BlockType = block;
        ActivateBlockPower(block);
        newTail.currentGridCell = _gridManager.GetGridCellByCoordinate(x, y);
        snake.ThisSnake.Add(newTail);
    }
Exemplo n.º 11
0
 private void SetPositions(SnakeBlock block, GridNode node, GridSystemManager gridSystem, TraversalDirection dir)
 {
     if (block != null)
     {
         GridNode n = gridSystem.GetNextNode(node, dir);
         if (!n.isFilled)
         {
             block.SetPosition(n);
             SetPositions(block.backBlock, n, gridSystem, dir);
         }
     }
 }
Exemplo n.º 12
0
 public virtual void OnMoved(SnakeBlock block)
 {
     this.lastPos = this.currentPos;
     if (block == null)
     {
         this.currentPos = snake.lastPos;
     }
     else
     {
         this.currentPos = block.lastPos;
     }
 }
Exemplo n.º 13
0
    private void CreateInitialBlocks(Vector3 position)
    {
        snakeBlocks = new List <SnakeBlock>();
        for (int i = 0; i < blockStartAmount; i++)
        {
            SnakeBlock snakeBlock = CreateBlock(position);
            position = snakeBlock.Position();
            // Subtract one unit for every block
            position.x -= 1;
        }

        ConnectSnakeBlocks();
    }
Exemplo n.º 14
0
    private void SpawnFood()
    {
        int x = Random.Range(0, width);
        int y = Random.Range(0, height);

        if (dataGrid[x, y] != 0)
        {
            SpawnFood();
        }

        food          = new SnakeBlock(x, y);
        foodAvailable = true;
    }
Exemplo n.º 15
0
    private void DrawSnake()
    {
        blocks = new List <SnakeBlock>();
        blocks.Add(head);

        SnakeBlock lastBlock = this.head;
        SnakeBlock currentBlock;

        for (int i = 0; i < lenght - 1; i++)
        {
            currentBlock = new SnakeBlock(lastBlock.x, lastBlock.y + 1);
            blocks.Add(currentBlock);
            lastBlock = currentBlock;
        }
    }
Exemplo n.º 16
0
    private SnakeBlock CreateBlock(Vector3 position)
    {
        SnakeBlock snakeBlock = Instantiate(
            snakeBlockPrefab,
            position,
            Quaternion.identity
            ) as SnakeBlock;

        snakeBlock.transform.parent = transform;
        snakeBlock.SetPosition(position);

        snakeBlocks.Add(snakeBlock);

        return(snakeBlock);
    }
Exemplo n.º 17
0
    private List <Entity> GetEverySnake()
    {
        List <Entity> entities   = GetEntities();
        List <Entity> snakeHeads = new List <Entity>();

        foreach (Entity entity in entities)
        {
            SnakeBlock snakeBlock = entity.GetComponent <SnakeBlock>();
            if (snakeBlock != null && snakeBlock.IsHead)
            {
                snakeHeads.Add(entity);
            }
        }
        return(snakeHeads);
    }
Exemplo n.º 18
0
        public T CreateCmd <T>(SnakeBlock excer, ICmdParam param) where T : SnakeCmd
        {
            if (param != null)
            {
                ObjectCacheModel cache = SingleModel <ObjectCacheModel> .Get();

                T cmd = cache.GetObjectCache <T>();
                cmd.Gen(excer, param);
                return(cmd);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 19
0
    public void Grow(GridCell newBlockPosition)
    {
        GameObject tailPrefab = _snakeSettings.SnakePrefabsData.TailBlockPrefab;
        GameObject newTailGO  = Instantiate(tailPrefab, transform);

        SnakeBlock newTail = newTailGO.GetComponent <SnakeBlock>();

        newTail.BlockType = _snakeEater.NextTailPartsPop();
        newTail.GetComponent <SpriteRenderer>().color = SnakeColor;
        SnakeBlock lastSnakePart = ThisSnake.Last();

        lastSnakePart.HasFood = false;

        newTail.currentGridCell = newBlockPosition;

        ThisSnake.Add(newTail);
    }
Exemplo n.º 20
0
    private bool CheckCollision(SnakeBlock newHead)
    {
        bool status = false;

        if (newHead.x < 0 || newHead.x >= width || newHead.y < 0 || newHead.y >= height)
        {
            Debug.Log("you hit the wall");
            status = true;
        }
        else if (dataGrid[newHead.x, newHead.y] == 1 && !CompareSnakeBlocks(newHead, snake.GetTail()))
        {
            Debug.Log("you hit yourself");
            status = true;
        }

        return(status);
    }
Exemplo n.º 21
0
    private void UpdateDataGrid()
    {
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                dataGrid[i, j] = 0;
            }
        }

        dataGrid[food.x, food.y] = 2;

        for (int i = 0; i < snakeLength; i++)
        {
            SnakeBlock currentBlock = snake.blocks[i];
            dataGrid[currentBlock.x, currentBlock.y] = 1;
        }
    }
Exemplo n.º 22
0
 public void ConnectTo(SnakeBlock snakeBlock)
 {
     if (transform.position.y < snakeBlock.transform.position.y)
     {
         ShowPart(top);
     }
     if (transform.position.y > snakeBlock.transform.position.y)
     {
         ShowPart(bottom);
     }
     if (transform.position.x > snakeBlock.transform.position.x)
     {
         ShowPart(left);
     }
     if (transform.position.x < snakeBlock.transform.position.x)
     {
         ShowPart(right);
     }
 }
Exemplo n.º 23
0
    public void GenerateTail()
    {
        GameObject tail = null;

        if (tailBlock == null)
        {
            tail      = Instantiate(tailPrefab, ReturnPosition(headBlock.current, bodyPrefab.transform.localScale.y * 0.5f), Quaternion.identity) as GameObject;
            tailBlock = headBlock.backBlock = tail.GetComponent <SnakeBlock>();
        }
        else
        {
            tailBlock.SetTailMaterial(bodyMat);
            tail = Instantiate(bodyPrefab, ReturnPosition(tailBlock.current, bodyPrefab.transform.localScale.y * 0.5f), Quaternion.identity) as GameObject;
            tailBlock.backBlock = tail.GetComponent <SnakeBlock>();
            tailBlock.backBlock.SetTailMaterial(tailMat);
            tailBlock = tailBlock.backBlock;
        }
        tail.transform.parent = transform;
    }
    private void DestroyLastSnake()
    {
        SnakeBlock head = null;

        foreach (SnakeBlock snakeBlock in snake.ThisSnake)
        {
            if (!snakeBlock.IsHead)
            {
                Destroy(snakeBlock.gameObject);
                DeactivateBlockPower(snakeBlock.BlockType);
            }
            else
            {
                head = snakeBlock;
            }
        }
        snake.ThisSnake.Clear();
        snake.ThisSnake.Add(head);
    }
Exemplo n.º 25
0
    private void InitializeVariables()
    {
        height  = 2 * (int)Camera.main.orthographicSize;
        width   = (int)(height * Screen.width / Screen.height);
        offsetX = -(0.5f * width - 0.5f);
        offsetY = 0.5f * height - 0.5f;

        snakeLength = 4;
        start       = new SnakeBlock((int)(width / 2), (int)((height - snakeLength) / 2));

        gameOver          = false;
        foodAvailable     = false;
        changingDirection = false;
        eating            = false;

        lastMoveTime  = Time.time;
        gameSpeed     = 0.5f;
        direction     = "up";
        eatingCounter = 0;
    }
Exemplo n.º 26
0
    private bool CheckNextCellEntities(int x, int y)
    {
        GridCell nextCell = _gridManager.GetGridCellByCoordinate(x, y);

        if (nextCell == null)
        {
            return(false);
        }
        if (nextCell.EntityOcupating == null)
        {
            return(false);
        }

        if (nextCell.EntityOcupating is ICollectable)
        {
            nextCell.EntityOcupating.GetComponent <ICollectable>().Collect(_snake.CurrentHead);
        }

        else if (nextCell.EntityOcupating.typeOfEntity == Entity.TypeOfEntity.Player)
        {
            if (_snake.GetComponent <BatteringRamPowerUpController>().BlockQuantity > 0)
            {
                SnakeBlock enemySnakeBlock = nextCell.EntityOcupating.GetComponent <SnakeBlock>();
                enemySnakeBlock.gameObject.SetActive(false);

                _snake.CurrentHead.GetComponentInParent <BatteringRamPowerUpController>().RemovingBatteringRamPowerUp();
            }
            else if (_snake.GetComponent <TimeTravelPowerUpController>().BlockQuantity > 0)
            {
                TimeTravelPowerUpController timeTravel = _snake.GetComponent <TimeTravelPowerUpController>();
                timeTravel.StartTimeTravel();
                return(true);
            }
            else
            {
                _snake.CurrentHead.GetComponentInParent <Snake>().Die();
                return(true);
            }
        }
        return(false);
    }
Exemplo n.º 27
0
    IEnumerator MoveObstacle(SnakeBlock block, GridNode node, TraversalDirection dir, int moveCount)
    {
        int count = 0;

        while (_cacheState == GAMESTATE.INITILIZECOMPLETE || _cacheState == GAMESTATE.GAMEPLAY)
        {
            if (count >= moveCount)
            {
                count = 0;
                dir   = GetReverseDirection(dir);
            }
            node = gridManager.GetNextNode(node, dir);
            if (node.isFilled)
            {
                GameManager.OnGameStateChangeByOther(GAMESTATE.GAMEEND);
            }
            block.SetPosition(node);
            count++;
            yield return(new WaitForSeconds((1 / movingObstaclespeed)));
        }
    }
Exemplo n.º 28
0
    internal void CutInThisBlock(SnakeBlock entityOcupating)
    {
        int indexToRemove = 0;
        int i             = 0;

        foreach (SnakeBlock snakeBlock in ThisSnake)
        {
            if (snakeBlock == entityOcupating)
            {
                indexToRemove = i;
            }
            if (indexToRemove != 0)
            {
                Destroy(snakeBlock.gameObject);
            }

            i++;
        }

        ThisSnake.RemoveRange(indexToRemove, ThisSnake.Count - indexToRemove);
    }
Exemplo n.º 29
0
    IEnumerator Move()
    {
        while (true)
        {
            // wait for tick
            yield return(new WaitForSeconds(m_TickTime));

            // move body
            for (int i = m_Model.SnakeBlocks.Count - 1;
                 i >= 1; i--)
            {
                m_Model.SnakeBlocks[i].transform.position =
                    m_Model.SnakeBlocks[i - 1].transform.position;
            }
            // move head
            var newpos =
                SnakeTranslate(
                    m_Model.SnakeBlocks[0].transform.position,
                    m_Direction);
            // todo: improve collision check
            // if distance between fruit and head <1
            if (Vector3.Distance(newpos,
                                 m_Fruit.transform.position) < 1)
            { // eat it!
                m_Model.SnakeBlocks.Insert(0, m_Fruit);
                m_Fruit = Instantiate(
                    blockReference,
                    new Vector3(
                        UnityEngine.Random.Range(-10, 10),
                        UnityEngine.Random.Range(-10, 10),
                        0), Quaternion.identity);
                UpdateTickTime();
            }
            else // move
            {
                m_Model.SnakeBlocks[0].transform.position = newpos;
            }
        }
    }
Exemplo n.º 30
0
 public void Eat(SnakeBlock food)
 {
     blocks.Add(food);
     lenght++;
 }