Exemple #1
0
        public void Save(TextWriter output)
        {
            // Compress
            m_tileMap.Compress();

            // Save out header
            var kvp = new KeyValuePairs();

            kvp.Comment = "Level data";
            kvp.Set("id", Info.ID);
            kvp.Set("title", Info.Title);
            kvp.Set("music", Info.MusicPath);
            kvp.Set("sky", Info.SkyPath);
            kvp.Set("script", Info.ScriptPath);
            kvp.Set("item", Info.ItemPath);
            kvp.Set("intro", Info.IntroPath);
            kvp.Set("outro", Info.OutroPath);
            kvp.Set("item_count", Info.TotalPlacements);
            kvp.Set("ever_completed", Info.EverCompleted);
            kvp.Set("camera.pitch", Info.CameraPitch);
            kvp.Set("camera.yaw", Info.CameraYaw);
            kvp.Set("camera.distance", Info.CameraDistance);
            kvp.Set("random.seed", Info.RandomSeed);

            // Save out tiles
            kvp.Set("tiles.width", Tiles.Width);
            kvp.Set("tiles.height", Tiles.Height);
            kvp.Set("tiles.depth", Tiles.Depth);
            kvp.Set("tiles.x_origin", Tiles.MinX);
            kvp.Set("tiles.y_origin", Tiles.MinY);
            kvp.Set("tiles.z_origin", Tiles.MinZ);

            var lookup     = new TileLookup();
            int lastID     = -1;
            var tiles      = new StringBuilder();
            var robotCount = 0;

            for (int x = Tiles.MinX; x < Tiles.MaxX; ++x)
            {
                for (int y = Tiles.MinY; y < Tiles.MaxY; ++y)
                {
                    for (int z = Tiles.MinZ; z < Tiles.MaxZ; ++z)
                    {
                        var coords    = new TileCoordinates(x, y, z);
                        var tile      = Tiles.GetTile(coords);
                        var direction = tile.GetDirection(this, coords);
                        if (tile == Dan200.Game.Level.Tiles.Extension)
                        {
                            tile      = Dan200.Game.Level.Tiles.Air;
                            direction = FlatDirection.North;
                        }
                        int id = lookup.GetIDForTile(tile);
                        if (id < 0)
                        {
                            id     = lookup.AddTile(tile);
                            lastID = id;
                        }
                        tiles.Append(Base64.ToString(id, 2));
                        tiles.Append(Base64.ToString((int)direction, 1));

                        var behaviour = tile.GetBehaviour(this, coords);
                        if (behaviour is SpawnTileBehaviour && ((SpawnTileBehaviour)behaviour).Required)
                        {
                            robotCount++;
                        }
                    }
                }
            }
            kvp.Set("tiles.data", tiles.ToString());
            kvp.Set("robot_count", robotCount);

            // Save out lookup
            var lookupString = new StringBuilder();

            for (int i = 0; i <= lastID; ++i)
            {
                var tile = lookup.GetTileFromID(i);
                lookupString.Append(tile.Path);
                if (i < lastID)
                {
                    lookupString.Append(',');
                }
            }
            kvp.Set("tiles.lookup", lookupString.ToString());

            // Save out file
            kvp.Save(output);
        }
Exemple #2
0
        public static Level Load(LevelData data, LevelOptions options)
        {
            // Create the tile lookup
            var lookup = new TileLookup();

            for (int i = 0; i < data.TileLookup.Length; ++i)
            {
                string path = data.TileLookup[i];
                lookup.AddTile(Tile.Get(path), i);
            }

            // Create the level
            int width   = data.Width;
            int height  = data.Height;
            int depth   = data.Depth;
            int xOrigin = data.XOrigin;
            int yOrigin = data.YOrigin;
            int zOrigin = data.ZOrigin;
            var level   = new Level(xOrigin, yOrigin, zOrigin, xOrigin + width, yOrigin + height, zOrigin + depth);

            // Load the information
            level.Info.Path            = data.Path;
            level.Info.ID              = data.ID;
            level.Info.Title           = data.Title;
            level.Info.MusicPath       = data.Music;
            level.Info.SkyPath         = data.Sky;
            level.Info.ScriptPath      = data.Script;
            level.Info.ItemPath        = data.Item;
            level.Info.IntroPath       = data.Intro;
            level.Info.OutroPath       = data.Outro;
            level.Info.TotalPlacements = data.ItemCount;
            level.Info.PlacementsLeft  = data.ItemCount;
            level.Info.EverCompleted   = data.EverCompleted;
            level.Info.CameraPitch     = data.CameraPitch;
            level.Info.CameraYaw       = data.CameraYaw;
            level.Info.CameraDistance  = data.CameraDistance;
            level.Info.InEditor        = options.InEditor;
            level.Info.InMenu          = options.InMenu;

            level.Info.RandomSeed = data.RandomSeed;
            level.Random          = new Random();

            // Populate the level
            var ids   = data.TileIDs;
            var tiles = level.Tiles;

            for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < height; ++y)
                {
                    for (int z = 0; z < depth; ++z)
                    {
                        var id        = data.TileIDs[x, y, z];
                        var direction = data.TileDirections[x, y, z];
                        var tile      = lookup.GetTileFromID(id);
                        if (tile != Dan200.Game.Level.Tiles.Air && tile != Dan200.Game.Level.Tiles.Extension)
                        {
                            var tileCoords = new TileCoordinates(x + tiles.MinX, y + tiles.MinY, z + tiles.MinZ);
                            if (tiles.GetTile(tileCoords) != Dan200.Game.Level.Tiles.Extension)
                            {
                                tiles.SetTile(tileCoords, tile, direction, false);
                            }
                        }
                    }
                }
            }

            return(level);
        }