/// <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); }
/// <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++; }