Example #1
0
        private void Move(World.World world)
        {
            // Convert indicated direction state to movement, Right = Positive X, Down = Positive Y
            int xMove = HorizontalIndicatedDirection == HorizontalPlayerDirection.Left
                ? -1 : HorizontalIndicatedDirection == HorizontalPlayerDirection.Right
                ? 1 : 0;

            int yMove = VerticalIndicatedDirection == VerticalPlayerDirection.Down
                ? 1 : VerticalIndicatedDirection == VerticalPlayerDirection.Up
                ? -1 : 0;

            // Change rotational sprite based off of player movement
            // Right
            if (xMove == 1 && yMove == 0)
            {
                Image = SpriteAtlas.PlayerRight;
            }
            // Down Right
            else if (xMove == 1 && yMove == 1)
            {
                Image = SpriteAtlas.PlayerBottomRight;
            }
            // Down
            else if (xMove == 0 && yMove == 1)
            {
                Image = SpriteAtlas.PlayerDown;
            }
            // Down Left
            if (xMove == -1 && yMove == 1)
            {
                Image = SpriteAtlas.PlayerBottomLeft;
            }
            // Left
            else if (xMove == -1 && yMove == 0)
            {
                Image = SpriteAtlas.PlayerLeft;
            }
            // Up Left
            else if (xMove == -1 && yMove == -1)
            {
                Image = SpriteAtlas.PlayerTopLeft;
            }
            // Up
            else if (xMove == 0 && yMove == -1)
            {
                Image = SpriteAtlas.PlayerUp;
            }
            // Up Right
            else if (xMove == 1 && yMove == -1)
            {
                Image = SpriteAtlas.PlayerTopRight;
            }

            // Player Sliding
            Tile newTile = world.GetTile(ContainingTile.XPosition + xMove, ContainingTile.YPosition + yMove, false);

            if (newTile != null)
            {
                world.GetChunk(newTile.XPosition, newTile.YPosition).IsDirty = true;
                world.GetChunk(ContainingTile.XPosition, ContainingTile.YPosition).IsDirty = true;

                newTile.tileObjects.Add(this);
                ContainingTile.tileObjects.Remove(this);

                ContainingTile = newTile;
            }

            // Reset movement direction
            VerticalIndicatedDirection   = VerticalPlayerDirection.None;
            HorizontalIndicatedDirection = HorizontalPlayerDirection.None;
        }
Example #2
0
        public static void Start(params string[] args)
        {
            if (args.Length == 1)
            {
                BaseDirectory = args[0];
                Directory.CreateDirectory(BaseDirectory);
            }

            ServerConfiguration = Configuration.LoadConfiguration <ServerConfiguration>(ResolvePath("config.yaml")) ?? new ServerConfiguration();
            Server = new MultiPlayerServer(ServerConfiguration);

            AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
            {
                Server.Trace.TraceData(TraceEventType.Critical, 0, "unhandled exception", e.ExceptionObject);

                if (e.IsTerminating && Environment.UserInteractive)
                {
                    Console.WriteLine("Press any key to quit.");
                    Console.ReadKey();
                }
            };

            var buckets = ServerConfiguration.Debug?.Profiler?.Buckets?.Split(',');

            if (buckets != null)
            {
                foreach (var bucket in buckets)
                {
                    Profiler.EnableBucket(bucket.Trim());
                }
            }

            if (ServerConfiguration.Debug != null && ServerConfiguration.Debug.DeleteWorldOnStartup)
            {
                if (Directory.Exists(ResolvePath("world")))
                {
                    Directory.Delete(ResolvePath("world"), true);
                }
            }

            if (ServerConfiguration.Debug != null && ServerConfiguration.Debug.DeletePlayersOnStartup)
            {
                if (Directory.Exists(ResolvePath("players")))
                {
                    Directory.Delete(ResolvePath("players"), true);
                }
            }

            IWorld world;

            try
            {
                world = World.World.LoadWorld(ResolvePath("world"));
                Server.AddWorld(world);
            }
            catch
            {
                world = new World.World(ResolvePath("default"), new FlatlandGenerator())
                {
                    BlockRepository = Server.BlockRepository
                };
                world.Save(ResolvePath("world"));
                Server.AddWorld(world);

                Server.Trace.TraceEvent(TraceEventType.Information, 0, "Generating world around spawn point...");
                for (var x = -5; x < 5; x++)
                {
                    for (var z = -5; z < 5; z++)
                    {
                        world.GetChunk(new Coordinates2D(x, z));
                    }
                    var progress = (int)((x + 5) / 10.0 * 100);
                    if (progress % 10 == 0)
                    {
                        Server.Trace.TraceEvent(TraceEventType.Information, 0, "{0}% complete", progress + 10);
                    }
                }

                Server.Trace.TraceEvent(TraceEventType.Information, 0, "Simulating the world for a moment...");
                for (var x = -5; x < 5; x++)
                {
                    for (var z = -5; z < 5; z++)
                    {
                        var chunk = world.GetChunk(new Coordinates2D(x, z));
                        for (byte w = 0; w < Chunk.Width; w++)
                        {
                            for (byte d = 0; d < Chunk.Depth; d++)
                            {
                                for (int y = 0; y < chunk.GetHeight(w, d); y++)
                                {
                                    var coords   = new Coordinates3D(x + w, y, z + d);
                                    var data     = world.GetBlockData(coords);
                                    var provider = world.BlockRepository.GetBlockProvider(data.Id);
                                    provider.BlockUpdate(data, data, Server, world);
                                }
                            }
                        }
                    }

                    var progress = (int)((x + 5) / 10.0 * 100);
                    if (progress % 10 == 0)
                    {
                        Server.Trace.TraceEvent(TraceEventType.Information, 0, "{0}% complete", progress + 10);
                    }
                }

                Server.Trace.TraceEvent(TraceEventType.Information, 0, "Lighting the world (this will take a moment)...");
                foreach (var lighter in Server.WorldLighters)
                {
                    while (lighter.TryLightNext())
                    {
                    }
                }
            }

            world.Save();
            CommandManager              = new CommandManager();
            Server.ChatMessageReceived += HandleChatMessageReceived;
            Server.Start(new IPEndPoint(IPAddress.Parse(ServerConfiguration.ServerAddress),
                                        ServerConfiguration.ServerPort));
            Console.CancelKeyPress += HandleCancelKeyPress;

            Server.Scheduler.ScheduleEvent(Constants.Events.WorldSave, null,
                                           TimeSpan.FromSeconds(ServerConfiguration.WorldSaveInterval), SaveWorlds);
            while (true)
            {
                Thread.Yield();
            }
        }