コード例 #1
0
        public void Update()
        {
            Point  newPosition     = Snake.CalculateNewPosition(Width, Height);
            Entity collisionEntity = EntityOccupyingPosition(newPosition);

            ClearedPositions.Clear();

            if (collisionEntity == null)
            {
                ClearedPositions.Add(Snake.Elements[Snake.Elements.Count - 1].Position);
                Snake.Move(newPosition);
            }
            else if (!newPosition.Equals(Snake.GetPosition()))
            {
                if (collisionEntity is Fruit)
                {
                    Snake.EatFruit(collisionEntity.GetPosition());
                    Entities.Remove(collisionEntity);
                    GenerateFruit();
                }
                else if (collisionEntity is Mouse)
                {
                    Snake.EatMouse(collisionEntity.GetPosition());
                    Entities.Remove(collisionEntity);
                }
                else if (collisionEntity is Obstacle)
                {
                    ClearedPositions.Add(Snake.Elements[Snake.Elements.Count - 1].Position);
                    Snake.EatObstacle(collisionEntity.GetPosition());
                    if (Snake.Lives <= 0)
                    {
                        GameOver = true;
                        return;
                    }
                }
                else if (collisionEntity is Snake)
                {
                    ClearedPositions.Add(Snake.Elements[Snake.Elements.Count - 1].Position);
                    List <Point> removedPositions = Snake.EatSelf(newPosition);
                    if (removedPositions != null)
                    {
                        foreach (Point position in removedPositions)
                        {
                            ClearedPositions.Add(position);
                        }
                    }
                }

                else if (collisionEntity is Powerup)
                {
                    Powerup powerup = (Powerup)collisionEntity;
                    ClearedPositions.Add(Snake.Elements[Snake.Elements.Count - 1].Position);
                    if (powerup.Effect.Variant == Effect.EffectVariant.Shrink)
                    {
                        List <Element> removedElements = Snake.Elements.GetRange(Snake.Size() / 2, Snake.Size() / 2 + Snake.Size() % 2);
                        foreach (Element element in removedElements)
                        {
                            ClearedPositions.Add(element.Position);
                        }
                    }
                    Snake.EatPowerup(collisionEntity.GetPosition(), ((Powerup)collisionEntity).Effect);
                    Entities.Remove(collisionEntity);
                }
            }
            List <Entity> removedEntities = new List <Entity>();
            bool          powerupExists = false, mouseExists = false;

            for (int i = 0; i < Entities.Count; i++)
            {
                if (Entities[i] is Powerup)
                {
                    Powerup powerup = (Powerup)Entities[i];
                    if (powerup.SpawnDuration > 0)
                    {
                        powerup.LowerDuration();
                        powerupExists = true;
                    }
                    else
                    {
                        ClearedPositions.Add(powerup.GetPosition());
                        removedEntities.Add(powerup);
                        powerupExists = false;
                    }
                }

                if (Entities[i] is Mouse)
                {
                    Mouse mouse            = (Mouse)Entities[i];
                    Point newMousePosition = mouse.CalculateNewPosition();
                    ClearedPositions.Add(mouse.GetPosition());
                    if (newMousePosition.X >= Height || newMousePosition.Y >= Width || newMousePosition.X < 0 || newMousePosition.Y < 0)
                    {
                        removedEntities.Add(mouse);
                    }
                    else
                    {
                        mouse.Move(newMousePosition);
                        mouseExists = true;
                    }
                }
            }

            foreach (Entity entity in removedEntities)
            {
                Entities.Remove(entity);
            }

            if (!powerupExists && !Snake.IsEffectActive() && IsDrawn(1))
            {
                GeneratePowerup();
            }

            if (Snake.IsEffectActive() && Snake.Effect.Duration > 0)
            {
                Snake.Effect.LowerDuration();
            }
            else
            {
                Snake.EndEffect();
            }

            if (!mouseExists && IsDrawn(1))
            {
                GenerateMouse();
            }

            if (Snake.Score >= nextStage)
            {
                GenerateObstacle();
                nextStage += Stage;
            }
        }