Example #1
0
        private void Create()
        {
            var head = new SnakePart(_initialX, _initialY);

            Tail = head;
            for (var i = 0; i < _initialLength - 1; i++)
            {
                var next = new SnakePart(head.X + 1, head.Y);
                next.SetConnection(head, -1);
                head.SetConnection(next, 1);
                head = next;
            }
            Head      = head;
            _velocity = new Velocity(0, 0);
        }
Example #2
0
        internal void SetConnection(SnakePart node, SnakeNodeType nodeType = SnakeNodeType.Head)
        {
            //var deltaX = Math.Abs(X - node.X);
            //var deltaY = Math.Abs(Y - node.Y);

            //if (deltaX != 1 && deltaY != 1)
            //    throw new ArgumentException($"{(nodeType == SnakeNodeType.Head ? "Head" : "Tail")} of the snake cannot be the same or be farther from current part more than one cell",nameof(node));

            if (nodeType == SnakeNodeType.Head)
            {
                Head = node;
            }
            else
            {
                Tail = node;
            }
        }
Example #3
0
        public void Move()
        {
            if (_velocity.FullStop)
            {
                return;
            }
            var newHead = Tail;

            Tail = Tail.Head;
            Tail.SetConnection(null, -1);

            newHead.SetConnection(Head, -1);
            newHead.SetConnection(null, 1);

            Head.SetConnection(newHead, 1);
            Head = newHead;

            newHead.CloneXY(newHead.Tail);
            newHead.Move(_velocity, _boundWidth, _boundHeight);

            if (_isTailOverNewPart)
            {
                Tail.SetConnection(_nextPartToAdd, -1);
                _nextPartToAdd.SetConnection(Tail, 1);
                Tail               = _nextPartToAdd;
                _nextPartToAdd     = null;
                _isTailOverNewPart = false;
            }

            if (_nextPartToAdd == null && _partsToAdd.Count > 0)
            {
                _nextPartToAdd = _partsToAdd.Dequeue();
            }

            if (_nextPartToAdd != null && (_nextPartToAdd.X == Tail.X &&
                                           _nextPartToAdd.Y == Tail.Y))
            {
                _isTailOverNewPart = true;
            }
        }
Example #4
0
 public SnakePart(int x, int y, SnakePart head, SnakePart tail) : this(x, y)
 {
     Head = head;
     Tail = tail;
 }
Example #5
0
 public void CloneXY(SnakePart part)
 {
     X = part.X;
     Y = part.Y;
 }