Exemplo n.º 1
0
        private bool LoadTSXProperties(BinaryReader reader, ref TMXMapDef map)
        {
            int tsxLoops = 0;

            var allProps = new List <Dictionary <int, TMXProperties> >();

            int prevTsxIndex = -1;

            while (true)
            {
                var tsxIndex = reader.ReadInt32();
                if (tsxIndex == -1)
                {
                    // All done
                    break;
                }

                if (tsxIndex != prevTsxIndex + 1)
                {
                    Debug.LogError("TMX binary files had non-consequitive TSX property sets, TMX import is likely corrupt, please try reimporting " + map.realPathName);
                    return(false);
                }

                var propsSet = new Dictionary <int, TMXProperties>();

                while (true)
                {
                    var tid = reader.ReadInt32();
                    if (tid == -1)
                    {
                        // All done this tsx
                        break;
                    }

                    var props = new TMXProperties();

                    LoadProperties(reader, props);

                    propsSet.Add(tid, props);
                }

                allProps.Add(propsSet);

                prevTsxIndex = tsxIndex;

                // Break out if we loop for too long, TSX data could be corrupt
                tsxLoops++;
                if (tsxLoops > 256)
                {
                    Debug.Log("TSX properties data is invalid, please try to reimport " + map.realPathName);
                    return(false);
                }
            }

            map.allTileProperties = allProps;

            return(true);
        }
Exemplo n.º 2
0
        private void LoadProperties(BinaryReader reader, TMXProperties props)
        {
            var strCount = reader.ReadInt32();

            for (int i = 0; i < strCount; i++)
            {
                var key = reader.ReadString();
                var val = reader.ReadString();
                props.Add(key, val);
            }

            var boolCount = reader.ReadInt32();

            for (int i = 0; i < boolCount; i++)
            {
                var  key = reader.ReadString();
                bool val = reader.ReadBoolean();
                props.Add(key, val);
            }

            var intCount = reader.ReadInt32();

            for (int i = 0; i < intCount; i++)
            {
                var key = reader.ReadString();
                int val = reader.ReadInt32();
                props.Add(key, val);
            }

            var floatCount = reader.ReadInt32();

            for (int i = 0; i < floatCount; i++)
            {
                var   key = reader.ReadString();
                float val = reader.ReadSingle();
                props.Add(key, val);
            }

            var colorCount = reader.ReadInt32();

            for (int i = 0; i < colorCount; i++)
            {
                var     key = reader.ReadString();
                Color32 val = new Color32();

                val.r = reader.ReadByte();
                val.g = reader.ReadByte();
                val.b = reader.ReadByte();
                val.a = reader.ReadByte();

                props.Add(key, val);
            }
        }
Exemplo n.º 3
0
        private bool LoadTMXTileLayerDef(BinaryReader reader, ref TMXMapDef map)
        {
            TMXLayerDef layerDef     = new TMXLayerDef();
            string      name         = reader.ReadString();
            int         layerWidth   = reader.ReadInt32();
            int         layerHeight  = reader.ReadInt32();
            int         layerOffsetX = reader.ReadInt32();
            int         layerOffsetY = reader.ReadInt32();
            bool        layerVisible = reader.ReadBoolean();
            byte        layerAlpha   = reader.ReadByte();
            int         chunkCount   = 0;

            // Load properties if available
            bool propsAvailable = reader.ReadBoolean();

            if (propsAvailable)
            {
                var props = new TMXProperties();
                LoadProperties(reader, props);

                layerDef.SetProperties(props);
            }

            if (map.infinite)
            {
                chunkCount = reader.ReadInt32();
            }

            layerDef.chunkCount = chunkCount;
            layerDef.SetSize(new Vector2i(layerWidth, layerHeight));
            layerDef.SetOffset(new Vector2i(layerOffsetX, layerOffsetY));
            layerDef.SetVisible(layerVisible);
            layerDef.SetAlpha(layerAlpha);

            map.layers[name] = layerDef;

            return(true);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Set custom properties
 /// </summary>
 /// <param name="props">Custom properties</param>
 public void SetProperties(TMXProperties props)
 {
     mProperties = props;
 }
Exemplo n.º 5
0
        /// <summary>
        /// Load a map definition from a parsed binary TMX file
        /// </summary>
        /// <param name="fileName">Filename</param>
        /// <returns>Map definition</returns>
        public TMXMap LoadTMX(string fileName)
        {
            var map = new TMXMapDef(fileName);

            if (fileName == null)
            {
                return(null);
            }

            map.realPathName = Path.GetDirectoryName(fileName) + "/" + Path.GetFileNameWithoutExtension(fileName) + ".tmx.rb/";
            map.realPathName = map.realPathName.Replace('\\', '/');

            string infoFileName = map.realPathName + "info";
            var    tmxFile      = Resources.Load <TextAsset>(infoFileName);

            if (tmxFile == null)
            {
                Debug.Log("Can't find TMX map at " + fileName + ". If TMX file exists then please try re-importing your TMX file.");
                return(null);
            }

            try
            {
                var reader = new BinaryReader(new MemoryStream(tmxFile.bytes));

                var magicNum = reader.ReadUInt16();
                var version  = reader.ReadUInt16();

                if (magicNum != RetroBlit_TMX_MAGIC)
                {
                    Debug.Log(fileName + " is not a TMX file");
                    Debug.Log("Magic: " + magicNum + " expected " + RetroBlit_TMX_MAGIC);
                    return(null);
                }

                if (version > RetroBlit_TMX_VERSION)
                {
                    Debug.Log(fileName + " is of a newer version than this version of RetroBlit supports, try reimporting your TMX file into Unity.");
                    return(null);
                }

                byte type = reader.ReadByte();

                if (type != RetroBlit_TMX_TYPE_MAP)
                {
                    Debug.Log(fileName + " is a RetroBlit TMX file but it is of the wrong type.");
                    return(null);
                }

                int  mapWidth  = reader.ReadInt32();
                int  mapHeight = reader.ReadInt32();
                byte r         = reader.ReadByte();
                byte g         = reader.ReadByte();
                byte b         = reader.ReadByte();
                byte a         = reader.ReadByte();
                bool infinite  = reader.ReadBoolean();

                map.SetSize(new Vector2i(mapWidth, mapHeight));
                map.SetBackgroundColor(new Color32(r, g, b, a));
                map.SetInfinite(infinite);

                int chunkWidth  = reader.ReadInt32();
                int chunkHeight = reader.ReadInt32();

                map.chunkSize = new Vector2i(chunkWidth, chunkHeight);

                // Load properties if available
                bool propsAvailable = reader.ReadBoolean();
                if (propsAvailable)
                {
                    var props = new TMXProperties();
                    LoadProperties(reader, props);

                    map.SetProperties(props);
                }

                if (!LoadTMXSections(reader, ref map))
                {
                    Debug.Log("Failed to load TMX sections from " + fileName);
                    return(null);
                }
            }
            catch (IOException e)
            {
                Debug.Log("Failed to load TMX from file " + fileName + ", " + e.ToString());
                return(null);
            }

            return(map);
        }
Exemplo n.º 6
0
        private bool LoadTMXObjectGroupDef(BinaryReader reader, ref TMXMapDef map)
        {
            var objectGroup = new TMXObjectGroupDef();

            var name    = reader.ReadString();
            var r       = reader.ReadByte();
            var g       = reader.ReadByte();
            var b       = reader.ReadByte();
            var a       = reader.ReadByte();
            var alpha   = reader.ReadByte();
            var visible = reader.ReadBoolean();
            var offsetX = reader.ReadInt32();
            var offsetY = reader.ReadInt32();

            objectGroup.SetName(name);
            objectGroup.SetColor(new Color32(r, g, b, a));
            objectGroup.SetAlpha(alpha);
            objectGroup.SetVisible(visible);
            objectGroup.SetOffset(new Vector2i(offsetX, offsetY));

            // Load properties if available
            bool propsAvailable = reader.ReadBoolean();

            if (propsAvailable)
            {
                var props = new TMXProperties();
                LoadProperties(reader, props);

                objectGroup.SetProperties(props);
            }

            // Now load objects
            var objects     = new List <TMXObject>();
            var objectCount = reader.ReadInt32();

            for (int i = 0; i < objectCount; i++)
            {
                var objName    = reader.ReadString();
                var objType    = reader.ReadString();
                var rectX      = reader.ReadInt32();
                var rectY      = reader.ReadInt32();
                var rectWidth  = reader.ReadInt32();
                var rectHeight = reader.ReadInt32();
                var rotation   = reader.ReadSingle();
                var objVisible = reader.ReadBoolean();
                var shape      = reader.ReadInt32();

                var points      = new List <Vector2i>();
                var pointsCount = reader.ReadInt32();
                for (int j = 0; j < pointsCount; j++)
                {
                    var pointX = reader.ReadInt32();
                    var pointY = reader.ReadInt32();
                    points.Add(new Vector2i(pointX, pointY));
                }

                var tmxObject = new TMXObjectDef();
                tmxObject.SetName(objName);
                tmxObject.SetType(objType);
                tmxObject.SetShape((TMXObject.Shape)shape);
                tmxObject.SetRect(new Rect2i(rectX, rectY, rectWidth, rectHeight));
                tmxObject.SetRotation(rotation);
                tmxObject.SetVisible(objVisible);
                tmxObject.SetPoints(points);

                // Load properties if available
                propsAvailable = reader.ReadBoolean();
                if (propsAvailable)
                {
                    var props = new TMXProperties();
                    LoadProperties(reader, props);

                    tmxObject.SetProperties(props);
                }

                objects.Add(tmxObject);
            }

            objectGroup.SetObjects(objects);

            map.objectGroups[name] = objectGroup;

            return(true);
        }