示例#1
0
 public MapLayer(GameEnvironment env, Tiled.Map map, string layer, float zindex)
 {
     Environment = env;
     m_map       = map;
     m_layer     = layer;
     Zindex      = zindex;
 }
示例#2
0
 public MapLayer(GameScreen screen, Tiled.Map map, string layer, float zindex)
     : base(screen)
 {
     Screen = screen;
     m_map = map;
     m_layer = layer;
     Zindex = zindex;
 }
示例#3
0
        private Tiled.Tileset.TilePropertyList GetTileProperties(Tiled.Map map, int tileId)
        {
            foreach (Tiled.Tileset t in map.Tilesets.Values)
            {
                Tiled.Tileset.TilePropertyList props = t.GetTileProperties(tileId);
                if (props != null)
                {
                    return(props);
                }
            }

            return(null);
        }
示例#4
0
        /// <summary>
        /// Load a map from file and create collision objects for it.  Appends map horizontally if one exists.
        /// </summary>
        /// <param name="filename">File to load map from.</param>
        public void LoadOrExtendMap(string filename, bool spawnPlayer = false)
        {
            Tiled.Map map = Tiled.Map.Load(Path.Combine(Controller.Content.RootDirectory, filename), Controller.Content);

            // Destroy and re-create collision body for map.
            if (CollisionBody == null)
            {
                CreateCollisionBody(CollisionWorld, Physics.Dynamics.BodyType.Static);
            }

            Vector2 tileHalfSize = new Vector2(map.TileWidth, map.TileHeight) / 2;
            Vector2 tileSize     = new Vector2(map.TileWidth, map.TileHeight);

            bool[,] levelCollision = new bool[map.Width, map.Height];

            float defaultZVal = ZSettings.Ground;

            // 2 = collision. 1 = no collision. 0 = unknown.
            List <byte> collision = new List <byte>();

            foreach (KeyValuePair <string, Tiled.Layer> layer in map.Layers)
            {
                defaultZVal -= 0.001f;

                for (int x = 0; x < layer.Value.Width; ++x)
                {
                    for (int y = 0; y < layer.Value.Height; ++y)
                    {
                        int tileId = layer.Value.GetTile(x, y);

                        if (tileId >= collision.Count || collision[tileId] == 0)
                        {
                            Tiled.Tileset.TilePropertyList props = GetTileProperties(map, tileId);

                            // The only way to add new elements at arbitrary indices is to fill up the rest of the array.  Do so.
                            for (int i = collision.Count; i < tileId + 1; ++i)
                            {
                                collision.Add(0);
                            }

                            if (props != null && props.ContainsKey("collision"))
                            {
                                collision[tileId] = (byte)(props["collision"].Equals("true", StringComparison.OrdinalIgnoreCase) ? 2 : 1);
                            }
                            else
                            {
                                collision[tileId] = 1;
                            }
                        }

                        levelCollision[x, y] |= (collision[tileId] > 1);
                    }
                }

                float z = defaultZVal;

                if (layer.Value.Properties.ContainsKey("zindex"))
                {
                    if (!float.TryParse(layer.Value.Properties["zindex"], out z))
                    {
                        z = defaultZVal;
                    }
                }

                MapLayer ml = new MapLayer(this, map, layer.Key, z);
                ml.Position = new Vector2(m_mapStart, 0.0f);
                AddChild(ml);
            }

            // Go through collision and try to create large horizontal collision shapes.
            for (int y = 0; y < map.Height; ++y)
            {
                int  firstX       = 0;
                bool hasCollision = false;

                for (int x = 0; x < map.Width; ++x)
                {
                    if (levelCollision[x, y])
                    {
                        if (hasCollision)
                        {
                            continue;
                        }
                        else
                        {
                            hasCollision = true;
                            firstX       = x;
                        }
                    }
                    else
                    {
                        if (hasCollision)
                        {
                            hasCollision = false;
                            int tilesWide = x - firstX;
                            if (tilesWide == 1)
                            {
                                continue;
                            }

                            for (int i = firstX; i <= x; ++i)
                            {
                                levelCollision[i, y] = false;
                            }

                            AddCollisionRectangle(
                                tileHalfSize * new Vector2(tilesWide, 1.0f)
                                , new Vector2(tileSize.X * (x - (float)tilesWide / 2) + m_mapStart, tileSize.Y * (y + 0.5f))
                                );
                        }
                    }
                }

                // Create final collision.
                if (hasCollision)
                {
                    for (int i = firstX; i < map.Width; ++i)
                    {
                        levelCollision[i, y] = false;
                    }

                    int tilesWide = map.Width - firstX;
                    AddCollisionRectangle(
                        tileHalfSize * new Vector2(tilesWide, 1.0f)
                        , new Vector2(tileSize.X * (map.Width - (float)tilesWide / 2) + m_mapStart, tileSize.Y * (y + 0.5f))
                        );
                }
            }

            // Go through collision and try to create large vertical collision shapes.
            for (int x = 0; x < map.Width; ++x)
            {
                int  firstY       = 0;
                bool hasCollision = false;

                for (int y = 0; y < map.Height; ++y)
                {
                    if (levelCollision[x, y])
                    {
                        if (hasCollision)
                        {
                            continue;
                        }
                        else
                        {
                            hasCollision = true;
                            firstY       = y;
                        }
                    }
                    else
                    {
                        if (hasCollision)
                        {
                            hasCollision = false;
                            int tilesTall = y - firstY;

                            AddCollisionRectangle(
                                tileHalfSize * new Vector2(1.0f, tilesTall)
                                , new Vector2(tileSize.X * (x + 0.5f), tileSize.Y * (y - (float)tilesTall / 2))
                                );
                        }
                    }
                }

                // Create final collision.
                if (hasCollision)
                {
                    int tilesTall = map.Height - firstY;
                    AddCollisionRectangle(
                        tileHalfSize * new Vector2(1.0f, tilesTall)
                        , new Vector2(tileSize.X * (x + 0.5f), tileSize.Y * (map.Height - (float)tilesTall / 2))
                        );
                }
            }

            SpawnController.CreateSpawnPoints(map.ObjectGroups.Values, new Vector2(m_mapStart, 0.0f), spawnPlayer);
            m_mapStart += map.Width * map.TileWidth;
        }
示例#5
0
        public void LoadMap(string mapname)
        {
            this.Loading = true;
            map          = Squared.Tiled.Map.Load(String.Format("Content\\{0}", mapname), content);
            MapWidth     = map.Width * map.TileWidth;
            MapHeight    = map.Height * map.TileHeight;
            MapWidth     = (MapWidth > screenw) ? MapWidth : screenw;
            MapHeight    = (MapHeight > screenh) ? MapWidth : screenh;
            renderTarget = new RenderTarget2D(
                device,
                MapWidth,
                MapHeight,
                //1024,
                //1024,
                false,
                device.PresentationParameters.BackBufferFormat,
                DepthFormat.Depth24);


            Random random = new Random();

            foreach (var grp in map.ObjectGroups)
            {
                //ObjectGroup grp = kvpair.Value;
                foreach (var tiledobj in grp.Objects)
                {
                    if (tiledobj.Name.Equals("Start"))
                    {
                        Console.WriteLine("object: {0}, x {1} y {2} width {3} height {4}", tiledobj.Name, tiledobj.X, tiledobj.Y, tiledobj.Width, tiledobj.Height);
                        Player.Location = new Vector2(tiledobj.X + tiledobj.Width / 2, tiledobj.Y + tiledobj.Width / 2);
                    }
                    else if (tiledobj.Name.Equals("SpawnPortal"))
                    {
                        Portals.Add(new SpawnPortal()
                        {
                            CreatureTypes = new Creature.Types[] { Creature.Types.SMALL, Creature.Types.MEDIUM },
                            Location      = new Vector2(tiledobj.X + tiledobj.Width / 2, tiledobj.Y + tiledobj.Height / 2),
                            Size          = random.Next(1, 6),
                            isOpen        = false
                        });
                    }
                    else if (tiledobj.Name.Equals("BossSpawn"))
                    {
                        var bosstype = tiledobj.Properties["BossType"];
                        switch (bosstype)
                        {
                        case "FirstBoss":
                            Creatures.Add(new Creature()
                            {
                                Attack   = 10,
                                Health   = 500,
                                Range    = 128,
                                Location = new Vector2(tiledobj.X + tiledobj.Width / 2, tiledobj.Y + tiledobj.Height / 2),
                                ID       = Creatures.Count,
                                Type     = Creature.Types.BOSS,
                                Speed    = 4,
                                AIScript = Creature.ChargePlayerIfInRange
                            });
                            break;

                        default:
                            throw new NotImplementedException();
                        }
                    }
                    else if (tiledobj.Name.Equals("Location"))
                    {
                        if (tiledobj.Properties.ContainsKey("LocationType"))
                        {
                            Locations.Add(new Location()
                            {
                                X      = tiledobj.X + tiledobj.Width / 2,
                                Y      = tiledobj.Y + tiledobj.Height / 2,
                                Type   = tiledobj.Properties["LocationType"],
                                Width  = tiledobj.Width,
                                Height = tiledobj.Height,
                            });
                        }
                    }
                }
                MapChanged = true;
            }

            //this.Viewport = new Vector2(-(map.Width*map.TileWidth / 2f), - (map.Height*map.TileHeight / 2f));
            this.Viewport = new Vector2(0, 0);
            //X = this.MapWidth / 2;
            //Y = this.MapHeight / 2;
            X            = this.Viewport.X / 2;
            Y            = this.Viewport.Y / 2;
            this.Loading = false;
        }
示例#6
0
        /// <summary>
        /// Load a map from file and create collision objects for it.
        /// </summary>
        /// <param name="filename">File to load map from.</param>
        public void LoadMap(string filename)
        {
            m_map = Tiled.Map.Load(Path.Combine(Controller.Content.RootDirectory, filename), Controller.Content);

            // Destroy and re-create collision body for map.
            DestroyCollisionBody();
            CreateCollisionBody(CollisionWorld, Physics.Dynamics.BodyType.Static);

            Vector2 tileHalfSize = new Vector2(m_map.TileWidth, m_map.TileHeight) / 2;
            Vector2 tileSize = new Vector2(m_map.TileWidth, m_map.TileHeight);

            bool[,] levelCollision = new bool[m_map.Width, m_map.Height];

            foreach (Tiled.Layer layer in m_map.Layers.Values) {
                for (int x = 0; x < layer.Width; ++x)
                for (int y = 0; y < layer.Height; ++y) {
                    int tileId = layer.GetTile(x, y) - 1;
                    if (tileId < 0) continue;

                    int row = tileId / 15;
                    int col = tileId - row * 15;
                    if (row >= 11 || col >= 15) continue;

                    levelCollision[x, y] = (collision[row, col] != 0);
                }
            }

            // Go through collision and try to create large horizontal collision shapes.
            for (int y = 0; y < m_map.Height; ++y) {
                int firstX = 0;
                bool hasCollision = false;

                for (int x = 0; x < m_map.Width; ++x) {
                    if (levelCollision[x, y]) {
                        if (hasCollision) continue;
                        else {
                            hasCollision = true;
                            firstX = x;
                        }
                    } else {
                        if (hasCollision) {
                            hasCollision = false;
                            int tilesWide = x - firstX;
                            if (tilesWide == 1) continue;

                            for (int i = firstX; i <= x; ++i) levelCollision[i, y] = false;

                            AddCollisionRectangle(
                                tileHalfSize * new Vector2(tilesWide, 1.0f)
                                , new Vector2(tileSize.X * (x - (float) tilesWide / 2), tileSize.Y * (y + 0.5f))
                            );
                        }
                    }
                }

                // Create final collision.
                if (hasCollision) {
                    for (int i = firstX; i < m_map.Width; ++i) levelCollision[i, y] = false;

                    int tilesWide = m_map.Width - firstX;
                    AddCollisionRectangle(
                        tileHalfSize * new Vector2(tilesWide, 1.0f)
                        , new Vector2(tileSize.X * (m_map.Width - (float) tilesWide / 2), tileSize.Y * (y + 0.5f))
                    );
                }
            }

            // Go through collision and try to create large vertical collision shapes.
            for (int x = 0; x < m_map.Width; ++x) {
                int firstY = 0;
                bool hasCollision = false;

                for (int y = 0; y < m_map.Height; ++y) {
                    if (levelCollision[x, y]) {
                        if (hasCollision) continue;
                        else {
                            hasCollision = true;
                            firstY = y;
                        }
                    } else {
                        if (hasCollision) {
                            hasCollision = false;
                            int tilesTall = y - firstY;

                            AddCollisionRectangle(
                                tileHalfSize * new Vector2(1.0f, tilesTall)
                                , new Vector2(tileSize.X * (x + 0.5f), tileSize.Y * (y - (float) tilesTall / 2))
                            );
                        }
                    }
                }

                // Create final collision.
                if (hasCollision) {
                    int tilesTall = m_map.Height - firstY;
                    AddCollisionRectangle(
                        tileHalfSize * new Vector2(1.0f, tilesTall)
                        , new Vector2(tileSize.X * (x + 0.5f), tileSize.Y * (m_map.Height - (float) tilesTall / 2))
                    );
                }
            }

            SpawnController = new SpawnController(this, m_map.ObjectGroups.Values);
        }
示例#7
0
 public void LoadMap(string filename)
 {
     m_map = Tiled.Map.Load(Path.Combine(m_controller.Content.RootDirectory, filename), m_controller.Content);
 }