Ejemplo n.º 1
0
        private static void LoadMap(string name)
        {
            // == Purge Existing Stage

            Spatial.Clear();
            Pipes.Clear();

            // Kill entities generated by loading map
            foreach (var o in FindEntities <Spawner>())
            {
                RemoveEntity(o);
            }
            foreach (var o in FindEntities <Block>())
            {
                RemoveEntity(o);
            }
            foreach (var o in FindEntities <Pipe>())
            {
                RemoveEntity(o);
            }

            // == Load Phase

            // Get map data
            Map = Assets.GetMap(name);

            // Get Layers
            var groundLayer = Map.GetLayer("ground");
            var spawnLayer  = Map.GetLayer("spawn");
            var pipeLayer   = Map.GetLayer("pipes");

            // Scan map data (generate phase)
            foreach (var(x, y) in Rasterizer.Rectangle(Map.Size))
            {
                var groundTile = groundLayer.GetTile(x, y);
                if (groundTile != null)
                {
                    LoadMapProcessGroundTiles(x, y, groundTile);
                }

                var pipeTile = pipeLayer.GetTile(x, y);
                if (pipeTile != null)
                {
                    LoadMapProcessPipesTiles(x, y, pipeTile);
                }

                var spawnTile = spawnLayer.GetTile(x, y);
                if (spawnTile != null)
                {
                    var position = new Vector(x, y) * 70 + (35, 15);

                    if (spawnTile.Id == 80)
                    {
                        // Move player to start position
                        Player.Transform.Position = position;
                    }
                    else
                    {
                        // Create spawner
                        var spawner = AddEntity(new Spawner(count: 2, period: 6, () => new Slime()));
                        spawner.Transform.Position = position;
                    }
                }
            }

            // Detect initial pipe configuration
            Pipes.EvaluatePipeConfiguration();
        }