示例#1
0
        public void ResetBody()
        {
            _bodyParts = new List <SnakePart>();

            var _random = new Random();

            var headX = _partSize * _random.Next(_distanceToWallAtSpawn, (Config.WindowWidth - _partSize) / _partSize - _distanceToWallAtSpawn);
            var headY = _partSize * _random.Next(_distanceToWallAtSpawn, (Config.WindowHeight - _partSize) / _partSize - _distanceToWallAtSpawn);
            var head  = new SnakePart(new Vector2(headX, headY), Config.EntitySize, Config.EntitySize);

            var  direction      = (Direction)_random.Next(Enum.GetNames(typeof(Direction)).Length);
            bool validDirection = false;

            switch (direction)
            {
            case Direction.North:
                validDirection = headY + _partSize + _bodyStartLength * _partSize <= Config.WindowHeight;
                break;

            case Direction.East:
                validDirection = headX - _bodyStartLength * _partSize >= 0;
                break;

            case Direction.South:
                validDirection = headY - _bodyStartLength * _partSize >= 0;
                break;

            case Direction.West:
                validDirection = headX + _partSize + _bodyStartLength * _partSize <= Config.WindowWidth;
                break;
            }
            direction = validDirection ? direction : (Direction)(((int)direction + 2) % 4);

            _movement = new SnakeMovement(this, direction);


            _bodyParts.Add(head);
            for (int i = 0; i < _bodyStartLength; i++)
            {
                SpawnPart();
            }

            foreach (var part in _bodyParts)
            {
                part.Texture        = BodyTexture;
                part.FacedDirection = direction;
            }
            Head.Texture        = HeadTexture;
            head.FacedDirection = direction;
        }