Example #1
0
        private Area LoadFromJson(string name)
        {
            string rootPath = Path.Combine(Environment.CurrentDirectory, "Maps");

            using (Stream stream = File.OpenRead(rootPath + "\\" + name + ".json"))
            {
                using (StreamReader sr = new StreamReader(stream))
                {
                    string   json   = sr.ReadToEnd();
                    FileArea result = JsonConvert.DeserializeObject <FileArea>(json);
                    Area     area   = new Area(result.layers.length, result.width, result.height);
                    area.Name       = name;
                    area.Background = new Color(128, 128, 128);
                    if (!string.IsNullOrEmpty(result.backgroundcolor))
                    {
                        area.Background = new Color(Convert.ToInt32(result.backgroundcolor.Substring(1, 2), 16),
                                                    Convert.ToInt32(result.backgroundcolor.Substring(3, 2), 16),
                                                    Convert.ToInt32(result.backgroundcolor.Substring(5, 2), 16));
                    }
                    for (int i = 0; i < result.tilesets.Length; i++)
                    {
                        FileTileset tileset = result.tilesets[i];
                        int         start   = tileset.firstgid;
                        int         perRow  = tileset.imagewidth / tileset.tilewidth;
                        int         width   = tileset.tilewidth;
                        for (int j = 0; j < tileset.tilecount; j++)
                        {
                            int  x     = j % perRow;
                            int  y     = j / perRow;
                            bool block = false;
                            if (tileset.tileproperties != null)
                            {
                                FileTileProperty property;
                                if (tileset.tileproperties.TryGetValue(j, out property))
                                {
                                    block = property.Block;
                                }
                            }

                            Tile tile = new Tile()
                            {
                                Texture         = tileset.image,
                                SourceRectangle = new Rectangle(x * width, y * width, width, width),
                                Blocked         = block
                            };
                            area.Tiles.Add(start + j, tile);
                        }
                    }

                    for (int l = 0; l < result.layers.Length; l++)
                    {
                        FileLayer layer = result.layers[l];
                        for (int i = 0; i < layer.data.Length; i++)
                        {
                            int x = i % area.Width;
                            int y = i / area.Width;
                            area.Layers[l].Tiles[x, y] = layer.data[i];
                        }
                    }
                    return(area);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Lädt die angegebene Datei in der Hoffnung um eine Area.
        /// </summary>
        public static Area LoadFromJson(string file, ref int nextId)
        {
            FileInfo info = new FileInfo(file);

            using (Stream stream = File.OpenRead(file))
            {
                using (StreamReader sr = new StreamReader(stream))
                {
                    // json Datei auslesen
                    string json = sr.ReadToEnd();

                    // Deserialisieren
                    FileArea result = JsonConvert.DeserializeObject <FileArea>(json);

                    // Neue Area öffnen und mit den Root-Daten füllen
                    FileLayer[] tileLayer   = result.layers.Where(l => l.type == "tilelayer").ToArray();
                    FileLayer   objectLayer = result.layers.Where(l => l.type == "objectgroup").FirstOrDefault();
                    Area        area        = new Area(tileLayer.Length, result.width, result.height);
                    area.Name = info.Name.Substring(0, info.Name.Length - 5);

                    // Song auslesen
                    if (result.properties != null)
                    {
                        area.Song = result.properties.Song;
                    }

                    // Hintergrundfarbe interpretieren
                    area.Background = new Color(128, 128, 128);
                    if (!string.IsNullOrEmpty(result.backgroundcolor))
                    {
                        // Hexwerte als Farbwert parsen
                        area.Background = new Color(
                            Convert.ToInt32(result.backgroundcolor.Substring(1, 2), 16),
                            Convert.ToInt32(result.backgroundcolor.Substring(3, 2), 16),
                            Convert.ToInt32(result.backgroundcolor.Substring(5, 2), 16));
                    }

                    // Tiles zusammen suchen
                    for (int i = 0; i < result.tilesets.Length; i++)
                    {
                        FileTileset tileset = result.tilesets[i];

                        int start  = tileset.firstgid;
                        int perRow = tileset.imagewidth / tileset.tilewidth;
                        int width  = tileset.tilewidth;

                        for (int j = 0; j < tileset.tilecount; j++)
                        {
                            int x = j % perRow;
                            int y = j / perRow;

                            // Block-Status ermitteln
                            bool block = false;
                            if (tileset.tileproperties != null)
                            {
                                FileTileProperty property;
                                if (tileset.tileproperties.TryGetValue(j, out property))
                                {
                                    block = property.Block;
                                }
                            }

                            // Tile erstellen
                            Tile tile = new Tile()
                            {
                                Texture         = tileset.image,
                                SourceRectangle = new Rectangle(x * width, y * width, width, width),
                                Blocked         = block
                            };

                            // In die Auflistung aufnehmen
                            area.Tiles.Add(start + j, tile);
                        }
                    }

                    // TileLayer erstellen
                    for (int l = 0; l < tileLayer.Length; l++)
                    {
                        FileLayer layer = tileLayer[l];

                        for (int i = 0; i < layer.data.Length; i++)
                        {
                            int x = i % area.Width;
                            int y = i / area.Width;
                            area.Layers[l].Tiles[x, y] = layer.data[i];
                        }
                    }

                    // Object Layer analysieren
                    if (objectLayer != null)
                    {
                        // Portals - Übertragungspunkte zu anderen Karten
                        foreach (var portal in objectLayer.objects.Where(o => o.type == "Portal"))
                        {
                            Rectangle box = new Rectangle(
                                portal.x / result.tilewidth,
                                portal.y / result.tileheight,
                                portal.width / result.tilewidth,
                                portal.height / result.tileheight
                                );

                            area.Portals.Add(new Portal()
                            {
                                DestinationArea = portal.name, Box = box
                            });
                        }

                        // Items (Spielelemente)
                        foreach (var item in objectLayer.objects.Where(o => o.type == "Item"))
                        {
                            Vector2 pos = new Vector2(
                                (item.x + (item.width / 2f)) / result.tilewidth,
                                (item.y + (item.height / 2f)) / result.tileheight);

                            switch (item.name)
                            {
                            case "coin": area.Items.Add(new Coin(nextId++)
                                {
                                    Position = pos
                                }); break;

                            case "goldencoin": area.Items.Add(new GoldenCoin(nextId++)
                                {
                                    Position = pos
                                }); break;

                            case "decard": area.Items.Add(new Decard(nextId++)
                                {
                                    Position = pos
                                }); break;

                            case "heidi": area.Items.Add(new Heidi(nextId++)
                                {
                                    Position = pos
                                }); break;

                            case "orc": area.Items.Add(new Orc(nextId++)
                                {
                                    Position = pos
                                }); break;

                            case "trader":
                                Trader trader = new Trader(nextId++)
                                {
                                    Position = pos
                                };
                                trader.Inventory.Add(new IronSword(nextId++)
                                {
                                });
                                trader.Inventory.Add(new WoodSword(nextId++)
                                {
                                });
                                trader.Inventory.Add(new Gloves(nextId++)
                                {
                                });
                                area.Items.Add(trader);
                                break;
                            }
                        }

                        // Player (Startpunkte)
                        foreach (var player in objectLayer.objects.Where(o => o.type == "Player"))
                        {
                            Vector2 pos = new Vector2(
                                (player.x + (player.width / 2)) / result.tilewidth,
                                (player.y + (player.height / 2)) / result.tileheight);

                            area.Startpoints.Add(pos);
                        }
                    }

                    return(area);
                }
            }
        }