Example #1
0
        private void Initalize()
        {
            foreach (var layerDesc in this.Descriptor.Layers.Values)
            {
                Layer layer = new Layer(layerDesc);

                for (int x = 0; x < this.Descriptor.Dimensions.X; x++)
                {
                    for (int y = 0; y < this.Descriptor.Dimensions.Y; y++)
                    {
                        Tile tile = layerDesc.Tiles[x, y] != null ? new Tile(layerDesc.Tiles[x, y]) : new Tile(new Vector(x * EngineConstants.TILE_WIDTH, y * EngineConstants.TILE_HEIGHT));

                        layer.SetTile(x, y, tile);
                    }
                }

                layer.NPCSpawnerEvent += (sender, args) =>
                {
                    var npcDesc = Server.ServiceLocator.GetService <NPCManager>().GetNPC(args.Name);

                    if (npcDesc == null)
                    {
                        Logger.LogEvent($"Error spawning NPC: {args.Name} does not exist!", LogTypes.ERROR, Environment.StackTrace);
                        return;
                    }

                    NPC npc = new NPC(npcDesc, this)
                    {
                        Layer = (Layer)sender
                    };
                    npc.WarpTo(args.Position);

                    // This allows the tile spawner to keep track of npcs that exist, and respawn if neccessary (i.e., they die).
                    args.HeartbeatListener.NPCs.Add(npc);
                };

                _layers.Add(layerDesc.Name, layer);
            }

            // Look for spawnpoints
            foreach (var layer in this.Layers)
            {
                for (int x = 0; x < this.Descriptor.Dimensions.X; x++)
                {
                    for (int y = 0; y < this.Descriptor.Dimensions.Y; y++)
                    {
                        if (layer.GetTile(x, y) != null && layer.GetTile(x, y).Descriptor.Attribute == TileAttributes.PlayerSpawn)
                        {
                            this.AddPlayerStartArea(new Vector(x * Settings.TileSize, y * Settings.TileSize), layer);
                        }
                    }
                }
            }
        }
Example #2
0
        public static Map Load(string path)
        {
            var map = new Map();

            using (var fileStream = new FileStream(path, FileMode.Open))
            {
                using (var bR = new BinaryReader(fileStream))
                {
                    // Load the tileset information
                    int tilesetCount = bR.ReadInt32();
                    for (int i = 0; i < tilesetCount; i++)
                    {
                        // We can throw this information away as it is used only in the editor suite.
                        string tilesetPath = bR.ReadString();
                    }

                    map.Name       = bR.ReadString();
                    map.Dimensions = new Vector(bR.ReadInt32(), bR.ReadInt32());
                    map.Dark       = bR.ReadBoolean();

                    map.Bounds = new Rect(0, 0, (int)map.Dimensions.X, (int)map.Dimensions.Y);

                    int layerCount = bR.ReadInt32();
                    for (int i = 0; i < layerCount; i++)
                    {
                        string layerName = bR.ReadString();
                        int    lIndex    = bR.ReadInt32();

                        var layer = new Layer(map.Dimensions, layerName, lIndex);
                        layer.Load(bR);
                        layer.NPCSpawnerEvent += (sender, args) =>
                        {
                            var npcDesc = Server.ServiceLocator.GetService <NPCManager>().GetNPC(args.Name);
                            NPC npc     = new NPC(npcDesc, map)
                            {
                                Layer = (Layer)sender
                            };
                            npc.WarpTo(args.Position);

                            // This allows the tile spawner to keep track of npcs that exist, and respawn if neccessary (i.e., they die).
                            args.HeartbeatListener.NPCs.Add(npc);
                        };

                        map.AddLayer(layerName, layer);
                    }
                }
            }

            // Look for spawnpoints
            foreach (var layer in map.Layers)
            {
                for (int x = 0; x < map.Dimensions.X; x++)
                {
                    for (int y = 0; y < map.Dimensions.Y; y++)
                    {
                        if (layer.GetTile(x, y) != null && layer.GetTile(x, y).Attribute == TileAttributes.PlayerSpawn)
                        {
                            map.AddPlayerStartArea(new Vector(x * Settings.TileSize, y * Settings.TileSize), layer);
                        }
                    }
                }
            }

            return(map);
        }