Example #1
0
        /// <summary>
        /// Spawn worm.
        /// </summary>
        /// <param name="x">Horizontal field position</param>
        /// <param name="y">Vertical field position</param>
        /// <param name="length">Worm length</param>
        /// <param name="color">Worm color</param>
        /// <returns>Worm or null</returns>
        public Worm Spawn(int x, int y, int length, Color color)
        {
            LengthCap = 1;
            Length    = 1;
            moving    = true;

            head.Color = color;

            firstModule = modules.Enable();
            if (firstModule == null)
            {
                Disable(false);
                return(null);
            }
            firstModule.Position = new Vector2(collision.EntityX(x), collision.EntityY(y));
            firstModule.SetTarget(collision.EntityX(x), collision.EntityY(y));
            eraser.X = -size;
            eraser.Y = -size;
            head.SetPosition(firstModule.Position);

            lastModule = firstModule;
            for (int i = 1; i < length; i++)
            {
                Grow();
            }

            direction = Random.ValidDirection(collision, Position, size);
            collision.Set(this, x, y);
            return(this);
        }
Example #2
0
        /// <summary>
        /// Initialize optimization buffer and check if any of the neighbours has the same color.
        /// </summary>
        /// <param name="wormModule">Worm.firstModule</param>
        /// <param name="wormLength">Worm length</param>
        /// <returns>Spawn current block</returns>
        private bool InitBuffer(WormModule wormModule, int wormLength)
        {
            bool spawn = true;

            for (int i = 0; i < wormLength; i++)
            {
                int x = collision.X(wormModule.Target.X);
                int y = collision.Y(wormModule.Target.Y);
                optimizationBuffer[x, y] = true;
                if (FindNeighbors(x, y))
                {
                    spawn = false;
                }
                if (x < left)
                {
                    left = x;
                }
                if (y < bottom)
                {
                    bottom = y;
                }
                if (x > right)
                {
                    right = x;
                }
                if (y > top)
                {
                    top = y;
                }
                wormModule = wormModule.Next;
            }
            return(spawn);
        }
Example #3
0
 /// <summary>
 /// Grow worm by adding one module to it.
 /// </summary>
 private void Grow()
 {
     newModule = modules.Enable();
     if (newModule == null)
     {
         return;
     }
     newModule.Position = lastModule.Position;
     newModule.SetTarget(lastModule.Target);
     lastModule.ResetDirection();
     lastModule.Next = newModule;
     lastModule      = newModule;
     LengthCap++;
 }