public override void ApplyTo(SimpleGameServer server)
        {
            Snakes = server.GetObjectsByName("Snake").Where(@object => @object.ObjectName == "SnakeHead").ToList();
            Apples = server.GetObjectsByName("Apple").ToList();

            Snakes.ForEach(snake =>
            {
                SnakeProperties properties = (SnakeProperties)snake.ObjectProperties;
                Apples.ForEach(apple =>
                {
                    if (apple.Position == properties.Head.Position)
                    {
                        LoggingService.LogMessage($"Player picked up an Apple.{snake.Owner.ClientHashCode}");
                        server.RemoveObject(apple);
                        properties.Length += 1;
                        properties.Score  += 1;
                        if (server.TickInterval - 20 > 0)
                        {
                            server.SetTickInterval(server.TickInterval - 20);
                        }
                    }
                });
            });
        }
Esempio n. 2
0
        public override void ApplyTo(SimpleGameServer server)
        {
            Snakes = server
                     .GetObjectsByName("Snake")
                     .Where(@object => @object.ObjectName == "SnakeHead")
                     .ToList();

            Snakes.ForEach(snake =>
            {
                SnakeProperties props = (SnakeProperties)snake.ObjectProperties;
                bool colided          = server.GetObjectAt(props.Head.Position).Any(obj => obj.IsSolid && obj != snake);
                if (colided)
                {
                    LoggingService.LogMessage($"Snake {snake.Owner.ClientHashCode} colided with an object.");

                    ((SnakeProperties)snake.ObjectProperties).Tail
                    .ToList()
                    .ForEach(tailBit => server.RemoveObject(tailBit));
                    server.RemoveObject(snake);
                }
            });
        }
Esempio n. 3
0
        public override void ApplyTo(SimpleGameServer server)
        {
            Snakes = server
                     .GetObjectsByName("Snake")
                     .Where(@object => @object.ObjectName == "SnakeHead")
                     .ToList();

            Snakes.ForEach(snake =>
            {
                server.LoggingService.LogMessage("Moving snake");
                SnakeProperties snakeProperties = (SnakeProperties)snake.ObjectProperties;

                if (snakeProperties.Tail.Any())
                {
                    server.RemoveObject(snakeProperties.Tail.Last.Value);
                    snakeProperties.Tail.RemoveLast();
                }

                while (snakeProperties.Tail.Count < snakeProperties.Length)
                {
                    GameObject copy = snakeProperties.Head.Copy();
                    snakeProperties.Tail.AddFirst(copy);
                    server.AddObject(copy);
                }
                if (!snake.Playable)
                {
                    Vector2D newVelocity = new Vector2D(random.Next(-1, 1) * 32, random.Next(-1, 1) * 32);
                    if (AvailableDirections[snakeProperties.Head.Velocity].Contains(newVelocity))
                    {
                        snakeProperties.Head.Velocity = newVelocity;
                    }
                }

                snakeProperties.Head.Position += snakeProperties.Head.Velocity;
            });
        }