Ejemplo n.º 1
0
        /// <summary>
        /// This must be passed the "SpriteData" node of the JSON
        /// </summary>
        /// <param name="tileSet">The tileset that the data belongs to</param>
        /// <param name="spriteDataToken">The JSON to pull the data from. MUST have a root of "SpriteData"</param>
        /// <returns></returns>
        public bool AddSpriteDataFromJson(Dictionary spriteDict)
        {
            List <SpriteData>        spriteData   = new List <SpriteData>();
            Dictionary               tileSetDict  = this.ValueExtractor.GetValueFromDictionary <Dictionary>(spriteDict, "TileSet");
            string                   tileSetName  = this.ValueExtractor.GetValueFromDictionary <string>(tileSetDict, "Name");
            Array                    tileSetArray = this.ValueExtractor.GetValueFromDictionary <Array>(tileSetDict, "SpriteData");
            ICollection <Dictionary> tileSetDicts =
                this.ValueExtractor.GetCollectionFromArray <Dictionary>(tileSetArray);

            foreach (Dictionary dict in tileSetDicts)
            {
                try
                {
                    List <SpritePart> parts = new List <SpritePart>();

                    string name       = this.ValueExtractor.GetValueFromDictionary <string>(dict, "Name");
                    string state      = this.ValueExtractor.GetValueFromDictionary <string>(dict, "State") ?? "default";
                    Array  partsArray = this.ValueExtractor.GetValueFromDictionary <Array>(dict, "Part");
                    ICollection <Dictionary> partDicts =
                        this.ValueExtractor.GetCollectionFromArray <Dictionary>(partsArray);
                    foreach (Dictionary innerDict in partDicts)
                    {
                        string partName   = this.ValueExtractor.GetValueFromDictionary <string>(innerDict, "Name");
                        int    partFrames = innerDict.Contains("Frames")
                            ? this.ValueExtractor.GetValueFromDictionary <int>(innerDict, "Frames")
                            : 1;
                        string fileName  = this.ValueExtractor.GetValueFromDictionary <string>(innerDict, "Filename");
                        int    sortOrder = this.ValueExtractor.GetValueFromDictionary <int>(innerDict, "SortOrder");
                        int    position  = this.ValueExtractor.GetValueFromDictionary <int>(innerDict, "Position");
                        NinePatchRect.AxisStretchMode stretchMode =
                            GraphicsHelper.ParseStretchMode(
                                this.ValueExtractor.GetValueFromDictionary <string>(innerDict, "FillType"));
                        bool drawCentre = !innerDict.Contains("DrawCentre") ||
                                          this.ValueExtractor.GetValueFromDictionary <bool>(innerDict, "DrawCentre");
                        Array marginArray = this.ValueExtractor.GetValueFromDictionary <Array>(
                            innerDict,
                            "PatchMargins");
                        ICollection <int> patchMargins = marginArray.IsNullOrEmpty()
                            ? new[] { 0, 0, 0, 0 }
                            : this.ValueExtractor.GetCollectionFromArray <int>(marginArray);

                        Array partDataArray = innerDict.Contains("Data")
                            ? this.ValueExtractor.GetValueFromDictionary <Array>(innerDict, "Data")
                            : new Array();
                        ICollection <string> data = this.ValueExtractor.GetCollectionFromArray <string>(partDataArray);
                        Array partColourArray     =
                            this.ValueExtractor.GetValueFromDictionary <Array>(innerDict, "Colour");
                        ICollection <Color>  colours     = new List <Color>();
                        ICollection <string> colourCodes = partColourArray.IsNullOrEmpty()
                            ? new List <string>
                        {
                            "#ffffff"
                        }
                            : this.ValueExtractor.GetCollectionFromArray <string>(partColourArray);
                        foreach (string code in colourCodes)
                        {
                            colours.Add(new Color(code));
                        }

                        Texture texture = GD.Load <Texture>(
                            GlobalConstants.GODOT_ASSETS_FOLDER +
                            GlobalConstants.SPRITES_FOLDER +
                            fileName);

                        Image          image      = texture.GetData();
                        int            frameWidth = texture.GetWidth() / partFrames;
                        List <Texture> frames     = new List <Texture>();
                        for (int i = 0; i < texture.GetWidth(); i += frameWidth)
                        {
                            ImageTexture imageTexture = new ImageTexture();
                            imageTexture.CreateFromImage(image.GetRect(new Rect2(new Vector2(i, 0),
                                                                                 new Vector2(frameWidth, frameWidth))), 2);
                            imageTexture.ResourceLocalToScene = true;
                            frames.Add(imageTexture);
                        }

                        int halfway = frames.Count / 2;
                        if (frames.Count > 1)
                        {
                            for (int i = halfway; i >= halfway; i--)
                            {
                                frames.Add(frames[i]);
                            }
                        }

                        SpriteFrames spriteFrames = new SpriteFrames();
                        if (spriteFrames.GetAnimationNames().Contains(state) == false)
                        {
                            spriteFrames.AddAnimation(state);
                            spriteFrames.SetAnimationLoop(state, true);
                            spriteFrames.SetAnimationSpeed(state, GlobalConstants.FRAMES_PER_SECOND);
                        }

                        for (int i = 0; i < frames.Count; i++)
                        {
                            spriteFrames.AddFrame(state, frames[i], i);
                        }

                        SpritePart part = new SpritePart
                        {
                            m_Data            = data.ToArray(),
                            m_Filename        = fileName,
                            m_Frames          = partFrames,
                            m_FrameSprite     = spriteFrames,
                            m_Name            = partName,
                            m_Position        = position,
                            m_PossibleColours = colours.ToList(),
                            m_SortingOrder    = sortOrder,
                            m_PatchMargins    = patchMargins.ToArray(),
                            m_DrawCentre      = drawCentre,
                            m_StretchMode     = stretchMode
                        };

                        parts.Add(part);
                    }

                    spriteData.Add(
                        new SpriteData
                    {
                        m_Name  = name,
                        m_Parts = parts,
                        m_State = state
                    });
                }
                catch (Exception e)
                {
                    GlobalConstants.ActionLog.Log("Could not load sprite data from JSON. Offending JSON to follow.");
                    GlobalConstants.ActionLog.Log(dict);
                    GlobalConstants.ActionLog.StackTrace(e);
                }
            }

            return(this.AddSpriteDataRange(tileSetName, spriteData));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This must be passed the "SpriteData" node of the JSON
        /// </summary>
        /// <param name="tileSet">The tileset that the data belongs to</param>
        /// <param name="spriteDataToken">The JSON to pull the data from. MUST have a root of "SpriteData"</param>
        /// <returns></returns>
        public bool AddSpriteDataFromJson(Dictionary spriteDict)
        {
            List <SpriteData> spriteData  = new List <SpriteData>();
            Dictionary        tileSetDict = this.ValueExtractor.GetValueFromDictionary <Dictionary>(spriteDict, "TileSet");

            if (tileSetDict.IsNullOrEmpty())
            {
                return(false);
            }

            string tileSetName = this.ValueExtractor.GetValueFromDictionary <string>(tileSetDict, "Name");
            ICollection <Dictionary> tileSetArray =
                this.ValueExtractor.GetArrayValuesCollectionFromDictionary <Dictionary>(tileSetDict, "SpriteData");

            bool isTileSet = this.ValueExtractor.GetValueFromDictionary <bool>(tileSetDict, "UseTileMap");

            foreach (Dictionary dict in tileSetArray)
            {
                try
                {
                    List <SpritePart> parts = new List <SpritePart>();

                    string name = this.ValueExtractor.GetValueFromDictionary <string>(dict, "Name");
                    int    size = this.ValueExtractor.GetValueFromDictionary <int>(dict, "Size");
                    if (size == 0)
                    {
                        size = GlobalConstants.SPRITE_TEXTURE_SIZE;
                    }

                    string state      = this.ValueExtractor.GetValueFromDictionary <string>(dict, "State") ?? "default";
                    Array  partsArray = this.ValueExtractor.GetValueFromDictionary <Array>(dict, "Part");
                    ICollection <Dictionary> partDicts =
                        this.ValueExtractor.GetCollectionFromArray <Dictionary>(partsArray);
                    foreach (Dictionary innerDict in partDicts)
                    {
                        string partName   = this.ValueExtractor.GetValueFromDictionary <string>(innerDict, "Name");
                        int    partFrames = innerDict.Contains("Frames")
                            ? this.ValueExtractor.GetValueFromDictionary <int>(innerDict, "Frames")
                            : 1;
                        string fileName  = this.ValueExtractor.GetValueFromDictionary <string>(innerDict, "Filename");
                        int    sortOrder = this.ValueExtractor.GetValueFromDictionary <int>(innerDict, "SortOrder");
                        int    position  = this.ValueExtractor.GetValueFromDictionary <int>(innerDict, "Position");
                        NinePatchRect.AxisStretchMode stretchMode =
                            GraphicsHelper.ParseStretchMode(
                                this.ValueExtractor.GetValueFromDictionary <string>(innerDict, "FillType"));
                        bool drawCentre = !innerDict.Contains("DrawCentre") ||
                                          this.ValueExtractor.GetValueFromDictionary <bool>(innerDict, "DrawCentre");
                        Array marginArray = this.ValueExtractor.GetValueFromDictionary <Array>(
                            innerDict,
                            "PatchMargins");
                        ICollection <int> patchMargins = marginArray.IsNullOrEmpty()
                            ? new[] { 0, 0, 0, 0 }
                            : this.ValueExtractor.GetCollectionFromArray <int>(marginArray);

                        Array partDataArray = innerDict.Contains("Data")
                            ? this.ValueExtractor.GetValueFromDictionary <Array>(innerDict, "Data")
                            : new Array();
                        ICollection <string> data = this.ValueExtractor.GetCollectionFromArray <string>(partDataArray);
                        Array partColourArray     =
                            this.ValueExtractor.GetValueFromDictionary <Array>(innerDict, "Colour");
                        ICollection <Color>  colours     = new List <Color>();
                        ICollection <string> colourCodes = partColourArray.IsNullOrEmpty()
                            ? new List <string>
                        {
                            Colors.Magenta.ToHtml(false)
                        }
                            : this.ValueExtractor.GetCollectionFromArray <string>(partColourArray);
                        foreach (string code in colourCodes)
                        {
                            colours.Add(new Color(code));
                        }

                        List <Texture> frames = this.ChopSprites(
                            fileName,
                            this.TryGetTextureFromCache(fileName),
                            partFrames,
                            position);

                        int halfway = frames.Count / 2;
                        if (frames.Count > 1)
                        {
                            for (int i = halfway; i >= halfway; i--)
                            {
                                frames.Add(frames[i]);
                            }
                        }

                        SpritePart part = new SpritePart
                        {
                            m_Data            = data.ToArray(),
                            m_Filename        = fileName,
                            m_Frames          = partFrames,
                            m_FrameSprite     = frames,
                            m_Name            = partName,
                            m_Position        = position,
                            m_PossibleColours = colours.ToList(),
                            m_SortingOrder    = sortOrder,
                            m_PatchMargins    = patchMargins.ToArray(),
                            m_DrawCentre      = drawCentre,
                            m_StretchMode     = stretchMode
                        };

                        parts.Add(part);
                    }

                    spriteData.Add(
                        new SpriteData
                    {
                        Name  = name,
                        Parts = parts,
                        State = state,
                        Size  = size
                    });
                }
                catch (Exception e)
                {
                    GlobalConstants.ActionLog.Log("Could not load sprite data from JSON. Offending JSON to follow.");
                    GlobalConstants.ActionLog.Log(dict);
                    GlobalConstants.ActionLog.StackTrace(e);
                }
            }

            return(this.AddSpriteDataRange(tileSetName, spriteData, isTileSet));
        }