예제 #1
0
    public void Grow(int num = 1)
    {
        while (num-- > 0)
        {
            // All cells are filled
            if (firstEmptyCell == null)
            {
                // Update grow queue
                growQueue++;

                // Empty all items
                while (GetItem())
                {
                }

                // Set initial empty cell
                firstEmptyCell = _head.next;
            }

            // There is still place for item
            else
            {
                // Mark cell filled
                firstEmptyCell.isItemOn = true;

                // Get cell reference from where to pull next item
                currentItemCell = firstEmptyCell;

                // Previously empty cell is filled now, so set next cell to be filled
                firstEmptyCell = firstEmptyCell.next;
            }
        }
    }
예제 #2
0
    // Remove item from body and return flag if it even existed
    public bool GetItem()
    {
        // If any cell has item
        if (currentItemCell != null)
        {
            // Empty item
            currentItemCell.isItemOn = false;

            // Currently emptied cell is now first empty
            firstEmptyCell = currentItemCell;

            // Set new current item cell
            if (currentItemCell.previous.isHead == false)
            {
                currentItemCell = currentItemCell.previous;
            }
            else
            {
                currentItemCell = null;
            }

            return(true);
        }
        return(false);
    }
예제 #3
0
    /**
     * UpdateBodyShrink
     */
    public void UpdateBodyShrink(float positionChange)
    {
        // print("UPDATE");
        for (int i = 0; i < _cellList.Count; i++)
        {
            SnakeBodyCell cell = _cellList[i];

            // UPDATE VALUES ///////////////////////////////////////////////////////////////
            if (cell.isHead)
            {
                // Move cell value towards skeleton beginning
                cell.relPos -= positionChange;
            }
            else
            {
                // Get previous cell reference
                SnakeBodyCell prev = _cellList[i - 1];

                // Distance between centers of cell and previous cell
                float distance = -(prev.relPos - cell.relPos);

                // Gap between edges of cell and previous cell
                float gap = distance - cell.buffer - prev.buffer;

                // Move cell forward towards skeleton beginning
                // for gap distance and consider bond strength (0 to 1)
                cell.relPos -= Mathf.Max(0, gap) * cell.bondStrength;
            }

            // Put cell on the skeleton to it's relative position with correction
            PutOnSkeleton(cell.transform, cell.relPos + correction);
            //////////////////////////////////////////////////////////// EO UPDATE VALUES //
        }
    }
예제 #4
0
//////////////////////////////////////////////////////////// EO UNITY METHODS //

// INIT ///////////////////////////////////////////////////////////////

    public void Init()
    {
        // Reposition
        if (snakeController.respawnPoint != null)
        {
            transform.position = snakeController.respawnPoint.position;
            transform.rotation = snakeController.respawnPoint.rotation;
        }

        // // Set Color of head
        // _head.GetComponent<Renderer>().material.color = snakeController.settings.color;

        // Create initial skeleton
        _skeleton = new SnakeSkeleton();
        skeleton.AppendJoint(transform.position + transform.forward * 0);
        // skeleton.AppendJoint( transform.position + transform.forward * -snakeController.settings.lengthOnBorn * 1.1f );
        skeleton.AppendJoint(transform.position + transform.forward * -100f);

        // SETUP INIT CELLS ///////////////////////////////////////////////////////////////
        //

        // Remove old body cells from scene
        if (_cellList != null)
        {
            foreach (SnakeBodyCell cell in _cellList)
            {
                if (!cell.isHead)
                {
                    Destroy(cell);
                }
            }
        }

        // Create empty list
        _cellList = new List <SnakeBodyCell>();

        // Add head cell to the list
        _cellList.Add(_head);

        // Mark head cell as isHead
        _head.isHead = true;

        // Mark head cell as isTail
        _head.isTail = true;

        // Set tail reference
        _tail = _head;

        // Instantiate cells
        for (int i = 0; i < snakeController.settings.lengthOnBorn; i++)
        {
            InstantiateCell(true);
        }
        //////////////////////////////////////////////////////////// EO SETUP INIT CELLS //
    }
예제 #5
0
// UNITY METHODS ///////////////////////////////////////////////////////////////

    void Awake()
    {
        // Get snake controller reference
        snakeController = GetComponent <SnakeController>();

        // Get head cell reference
        _head      = GetComponent <SnakeBodyCell>();
        _head.body = this;

        // Initialize body
        Init();
    }
예제 #6
0
    private void ShrinkEnterState()
    {
        DebugEnter("Shrink");

        // Reset speed
        speed = 0;

        // Set reference to cell to which to srhink
        targetCell = body.cellList[Mathf.Min(body.cellList.Count - 1, shrinkNumberOfCells)];

        // If target node is the last node of the snake,
        // this is last, dying shrink
        dying = (shrinkNumberOfCells >= body.cellList.Count);
    }
예제 #7
0
    /**
     * UpdateBody
     */
    public void UpdateBody(float positionChange)
    {
        // Check if any cell needs to be grown
        CheckGrowing();

        // print("UPDATE");
        for (int i = 0; i < _cellList.Count; i++)
        {
            SnakeBodyCell cell = _cellList[i];

            // UPDATE VALUES ///////////////////////////////////////////////////////////////
            if (cell.isHead)
            {
                // Move cell value towards skeleton beginning
                cell.relPos -= positionChange;

                // Update correction
                correction += positionChange;
            }
            else
            {
                // Get previous cell reference
                SnakeBodyCell prev = _cellList[i - 1];

                // Distance between centers of cell and previous cell
                float distance = -(prev.relPos - cell.relPos);

                // Gap between edges of cell and previous cell
                float gap = distance - cell.buffer - prev.buffer;

                // Move cell forward towards skeleton beginning
                // for gap distance and consider bond strength (0 to 1)
                cell.relPos -= Mathf.Max(0, gap) * cell.bondStrength;

                // Put cell on the skeleton to it's relative position with correction
                PutOnSkeleton(cell.transform, cell.relPos + correction);
            }
            //////////////////////////////////////////////////////////// EO UPDATE VALUES //
        }

        // Trim exces part of the skeleton
        float realSnakeLength = tail.relPos + correction;

        skeleton.TrimEnd(Mathf.Max(Mathf.Min(realSnakeLength + 1, _skeleton.length), 0));
    }
예제 #8
0
    /**
     *
     */
    public void DestroyCell(SnakeBodyCell cell)
    {
        // Set new tail
        if (cell.isTail)
        {
            try {
                cell.previous.isTail = true;
                _tail = cell.previous;
            }
            catch (System.ArgumentException e) { print(e); }
        }

        // Remove from list
        cellList.Remove(cell);

        // Remove object
        Destroy(cell.gameObject);
    }
예제 #9
0
//////////////////////////////////////////////////////////// EO INIT //

// OTHER METHODS ///////////////////////////////////////////////////////////////

    /**
     * InstantiateCell
     */
    public SnakeBodyCell InstantiateCell(bool fullyGrown = false)
    {
        // print("INSTANTIATE");
        // Instantiate cell
        SnakeBodyCell cell = (Instantiate(cellPrefab.transform, Vector3.zero, Quaternion.identity) as Transform).GetComponent <SnakeBodyCell>();

        // // Set color to new cell
        // cell.SetColor( snakeController.settings.color );

        // Set body reference
        cell.body = this;

        // Unset old tail
        if (_tail != null)
        {
            _tail.isTail = false;
        }

        // Set new tail
        _tail        = cell;
        _tail.isTail = true;

        // Set initial empty cell
        if (firstEmptyCell == null)
        {
            firstEmptyCell = cell;
        }

        // Set value to last cells value, plus combined buffers
        SnakeBodyCell prev = _cellList[_cellList.Count - 1];

        // Decide where to spawn:
        // behind the last cell, or under it
        cell.relPos = fullyGrown ? prev.relPos + (prev.buffer + cell.buffer) : prev.relPos;

        // Put cell on the skeleton to it's relative position with correction
        PutOnSkeleton(cell.transform, cell.relPos + correction);

        // Keep list of all body cells
        _cellList.Add(cell);

        // Return newly created cell
        return(cell);
    }
예제 #10
0
    void OnTriggerEnter(Collider other)
    {
        switch (other.tag)
        {
        case "Food":
            for (int i = 0; i < other.GetComponent <Item>().nutritionValue; i++)
            {
                body.Grow();
                GameManager.SCORE++;
            }
            // settings.color = RandomColor.GetRandomColor();
            // body.SetColor( settings.color );
            break;

        case "Coin":
            GameManager.SCORE += 10;
            break;

        case "Star":
            GameManager.SCORE += 100;
            GameManager.STARS++;
            if (GameManager.STARS == 3)
            {
                GameManager.INSTANCE.currentState = GameManager.GameState.OpenExit;
            }
            break;

        case "Snake Cell":
        case "Snake":
            if (currentState != SnakeState.Shrink && currentState != SnakeState.OnRail)
            {
                SnakeBodyCell otherCell = other.GetComponent <SnakeBodyCell>();

                bool otherIsSelfNeck = false;
                try{ otherIsSelfNeck = (GetComponent <SnakeBodyCell>() == otherCell.previous); }
                catch (System.ArgumentException e) { print(e); }

                // Ignore collisions for neck cell
                if (!otherIsSelfNeck)
                {
                    // Eat any kind of tail - enemy tail or self tail
                    if (otherCell.isTail)
                    {
                        // Both cells are one size long
                        if (otherCell.isHead && body.size == 1)
                        {
                            currentState = SnakeState.Shrink;
                        }
                        else
                        {
                            // Destroy hit tail
                            otherCell.body.DestroyCell(otherCell);

                            body.Grow();
                        }
                    }
                    else if (otherCell.isHead)
                    {
                        currentState = SnakeState.Shrink;
                    }
                    else
                    {
                        currentState = SnakeState.Shrink;
                    }
                }
            }
            break;

        case "Ground":
        case "Wall":
            currentState = SnakeState.Shrink;
            break;

        case "Hole":
        case "Exit":
            if (currentState != SnakeState.OnRail)
            {
                hole = other.GetComponent <Hole>();
                hole.RotatePeriscope(this.transform);
                spline = other.transform.Find("Periscope Spline").GetComponent <BezierSpline>();

                currentState = SnakeState.OnRail;
            }
            break;

        case "Button":
            break;

        default:
            // print("SnakeHeadCollider hit something not handeld by code! " + other);
            break;
        }
    }