Пример #1
0
    private int SetSegmentPositionAndRotation(SnakeBody bodySegment, int startingIndex)
    {
        // get the last position index in the trail, prior to the location of the specified body segment
        int indexBefore = GetPositionIndexBefore(bodySegment.DistanceFromHead, startingIndex);

        if (indexBefore == -1)
        {
            // position specified is not contained in the trail
            // extrapolate based on last position in the trail, if possible.
            // return the last position in the trail.
            SnakePosition position = this.positions[this.positions.Count - 1];
            if (position.UnitVectorToPreviousPosition.magnitude > 0.0f)
            {
                indexBefore = this.positions.Count - 1;
            }
            else
            {
                // no unit vector available
                bodySegment.transform.localPosition = position.Position;
                // don't try to set rotation.
                return(0);
            }
        }

        SnakePosition posBefore = this.positions[indexBefore];

        SetSegmentRotation(bodySegment, posBefore);

        Vector3 displacement = posBefore.UnitVectorToPreviousPosition * (bodySegment.DistanceFromHead - posBefore.DistanceFromHead);
        Vector3 newPos       = posBefore.Position + displacement;

        bodySegment.transform.localPosition = newPos;

        return(indexBefore);
    }
Пример #2
0
    private void SetHeadPosition(SnakeHead head)
    {
        // This resets the head's transform's position which may have been adjusted by sinusoidal code.
        // TODO move out to Snake
        SnakePosition posBefore = this.positions[0];

        head.transform.localPosition = posBefore.Position;
    }
Пример #3
0
    public void Reset()
    {
        // Clear all
        this.positions.Clear();
        // Add a dummy head position
        SnakePosition headPosition = new SnakePosition(new Vector3(0, 0, 0));

        headPosition.DistanceFromHead = 0.0f;
        this.positions.Add(headPosition);
    }
Пример #4
0
    private void SetSegmentRotation(SnakeSegment bodySegment, SnakePosition position)
    {
        Direction dir = SerpentConsts.GetDirectionForVector(position.UnitVectorToPreviousPosition);

        if (dir == Direction.None)
        {
            return;
        }

        Direction oppositeDir = SerpentConsts.OppositeDirection[(int)dir];

        bodySegment.CurrentDirection = oppositeDir;
    }
Пример #5
0
        public void Update()
        {
            PlaySound = false;

            if (Direction == SnakeDirection.Stop)
            {
                return;
            }

            var head = new Point((Point)SnakePosition[0]);
            var tail = new Point((Point)SnakePosition[SnakePosition.Count - 1]);

            if (Direction == SnakeDirection.Left)
            {
                head.X--;
            }
            if (Direction == SnakeDirection.Right)
            {
                head.X++;
            }
            if (Direction == SnakeDirection.Up)
            {
                head.Y--;
            }
            if (Direction == SnakeDirection.Down)
            {
                head.Y++;
            }

            for (int i = 0; i < SnakePosition.Count - 1; i++)
            {
                SnakePosition[SnakePosition.Count - 1 - i] = new Point((Point)SnakePosition[SnakePosition.Count - 2 - i]);
            }

            SnakePosition[0] = head;

            if (IsCellEmpty(head.X, head.Y, true) == false)
            {
                Reset();
            }

            if (head.X == FoodPosition.X && head.Y == FoodPosition.Y)
            {
                SnakePosition.Add(tail);
                UpdateFood();
                PlaySound = true;
            }
        }
Пример #6
0
    public void AddPosition(Vector3 pos)
    {
        // If this position is a duplicate of the one just behind the head then don't add it.
        if (this.positions.Count > 1)
        {
            Vector3 mostRecentPosition = this.positions[1].Position;
            if (pos == mostRecentPosition)
            {
                return;
            }
        }

        // Insert a new position after the head.
        SnakePosition newPosition = new SnakePosition(pos);

        this.positions.Insert(1, newPosition);

        // Update unit vectors
        UpdateUnitVector(1);
        UpdateUnitVector(0);
    }
Пример #7
0
 private void Awake()
 {
     snakePosition = Deserialize <SnakePosition>("Assets\\Resources\\riddle2.xml");
 }
Пример #8
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            if (snakeMovePosition != SnakeDirection.down)
            {
                snakeMovePosition = SnakeDirection.up;
            }
        }
        else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            if (snakeMovePosition != SnakeDirection.up)
            {
                snakeMovePosition = SnakeDirection.down;
            }
        }
        else if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            if (snakeMovePosition != SnakeDirection.right)
            {
                snakeMovePosition = SnakeDirection.left;
            }
        }
        else if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            if (snakeMovePosition != SnakeDirection.left)
            {
                snakeMovePosition = SnakeDirection.right;
            }
        }


        snakeMoveTimer += Time.deltaTime;
        if (snakeMoveTimer >= snakeMoveTimerMax)
        {
            if (snakeBodySize != previousSnakeBodySize)
            {
                previousSnakeBodySize = snakeBodySize;
                generateSnakeBody();
                generateAppleIcon();
            }

            SnakePosition previousSnakePosition = new SnakePosition(snakeHeadPosition, snakeMovePosition);
            snakeBodyPositionList.Insert(0, previousSnakePosition);
            if (snakeBodyPositionList.Count > snakeBodySize)
            {
                snakeBodyPositionList.RemoveAt(snakeBodyPositionList.Count - 1);
            }


            switch (snakeMovePosition)
            {
            case SnakeDirection.up:
                currentSnakeMovePosition = new Vector2Int(0, 1);
                break;

            case SnakeDirection.down:
                currentSnakeMovePosition = new Vector2Int(0, -1);
                break;

            case SnakeDirection.left:
                currentSnakeMovePosition = new Vector2Int(-1, 0);
                break;

            case SnakeDirection.right:
                currentSnakeMovePosition = new Vector2Int(1, 0);
                break;


            default:
                currentSnakeMovePosition = new Vector2Int(0, 0);
                break;
            }
            snakeMoveTimer    -= snakeMoveTimerMax;
            snakeHeadPosition += currentSnakeMovePosition;

            updateSnakePosition();
        }
    }