예제 #1
0
        public static SpriteDefinition LoadSpriteDefinition(string spritePath)
        {
            spritePath = Utility.GetContentDir("sprites/" + spritePath);

            if (!File.Exists(spritePath))
                return null;

            var definition = new SpriteDefinition();
            string jsonDefinition = File.ReadAllText(spritePath);
            var jDefinition = JsonConvert.DeserializeObject<JToken>(Utility.JsonRemoveComments(jsonDefinition));

            definition.DefaultAnimationFPS = jDefinition["defaultFps"].Value<float>();
            definition.Scale = jDefinition["scale"].Value<float>();
            definition.AnimationDefinitions = new Dictionary<string, SpriteAnimationDefinition>();

            // Add animations
            var jAnimations = jDefinition["animations"].Value<JArray>();
            foreach (JObject jAnimation in jAnimations)
            {
                var animDefinition = new SpriteAnimationDefinition();

                animDefinition.Name = jAnimation["name"].Value<string>();
                animDefinition.FPS = jAnimation["fps"] != null ? jAnimation["fps"].Value<float>() : definition.DefaultAnimationFPS;

                var jType = jAnimation["type"];
                if (jType != null)
                    animDefinition.AnimationType = (SpriteAnimationType)Enum.Parse(typeof (SpriteAnimationType), jType.Value<string>(), true);

                var jOffsetArray = jAnimation["origin"] as JArray;
                if (jOffsetArray != null)
                {
                    if (jOffsetArray.Count != 2)
                        throw new InvalidOperationException("Expected 2 value array in animations' origin. (" + animDefinition.Name + ")");

                    animDefinition.Origin = new Vector2(jOffsetArray[0].Value<float>(), jOffsetArray[1].Value<float>());
                }

                var jFrames = jAnimation["frames"].Value<JArray>();
                animDefinition.Frames = new SpriteAnimFrame[jFrames.Count];

                for (int i = 0; i < jFrames.Count; i++)
                {
                    var jFrame = jFrames[i].Value<JArray>();
                    var animFrame = new SpriteAnimFrame();

                    int x = jFrame[0].Value<int>();
                    int y = jFrame[1].Value<int>();
                    int w = jFrame[2].Value<int>();
                    int h = jFrame[3].Value<int>();
                    animFrame.SubRectangle = new Rectangle(x, y, w, h);

                    if (jFrame.Count > 4)
                        animFrame.WaitTime = jFrame[4].Value<float>();

                    animDefinition.Frames[i] = animFrame;
                }

                if (animDefinition.Frames.Length == 0)
                    Console.Error.WriteLine("Warning: Sprite definition animation contains no frames! (" + spritePath + ", " + animDefinition.Name + ")");

                definition.AnimationDefinitions.Add(animDefinition.Name, animDefinition);
            }

            if (!definition.AnimationDefinitions.ContainsKey("idle"))
                throw new InvalidOperationException("Error: Sprite definition animation does not contain an idle frame!");

            return definition;
        }
예제 #2
0
        public static SpriteDefinition LoadSpriteDefinition(string spritePath)
        {
            spritePath = Utility.GetContentDir("sprites/" + spritePath);

            if (!File.Exists(spritePath))
            {
                return(null);
            }

            var    definition     = new SpriteDefinition();
            string jsonDefinition = File.ReadAllText(spritePath);
            var    jDefinition    = JsonConvert.DeserializeObject <JToken>(Utility.JsonRemoveComments(jsonDefinition));

            definition.DefaultAnimationFPS = jDefinition["defaultFps"].Value <float>();
            definition.Scale = jDefinition["scale"].Value <float>();
            definition.AnimationDefinitions = new Dictionary <string, SpriteAnimationDefinition>();

            // Add animations
            var jAnimations = jDefinition["animations"].Value <JArray>();

            foreach (JObject jAnimation in jAnimations)
            {
                var animDefinition = new SpriteAnimationDefinition();

                animDefinition.Name = jAnimation["name"].Value <string>();
                animDefinition.FPS  = jAnimation["fps"] != null ? jAnimation["fps"].Value <float>() : definition.DefaultAnimationFPS;

                var jType = jAnimation["type"];
                if (jType != null)
                {
                    animDefinition.AnimationType = (SpriteAnimationType)Enum.Parse(typeof(SpriteAnimationType), jType.Value <string>(), true);
                }

                var jOffsetArray = jAnimation["origin"] as JArray;
                if (jOffsetArray != null)
                {
                    if (jOffsetArray.Count != 2)
                    {
                        throw new InvalidOperationException("Expected 2 value array in animations' origin. (" + animDefinition.Name + ")");
                    }

                    animDefinition.Origin = new Vector2(jOffsetArray[0].Value <float>(), jOffsetArray[1].Value <float>());
                }

                var jFrames = jAnimation["frames"].Value <JArray>();
                animDefinition.Frames = new SpriteAnimFrame[jFrames.Count];

                for (int i = 0; i < jFrames.Count; i++)
                {
                    var jFrame    = jFrames[i].Value <JArray>();
                    var animFrame = new SpriteAnimFrame();

                    int x = jFrame[0].Value <int>();
                    int y = jFrame[1].Value <int>();
                    int w = jFrame[2].Value <int>();
                    int h = jFrame[3].Value <int>();
                    animFrame.SubRectangle = new Rectangle(x, y, w, h);

                    if (jFrame.Count > 4)
                    {
                        animFrame.WaitTime = jFrame[4].Value <float>();
                    }

                    animDefinition.Frames[i] = animFrame;
                }

                if (animDefinition.Frames.Length == 0)
                {
                    Console.Error.WriteLine("Warning: Sprite definition animation contains no frames! (" + spritePath + ", " + animDefinition.Name + ")");
                }

                definition.AnimationDefinitions.Add(animDefinition.Name, animDefinition);
            }

            if (!definition.AnimationDefinitions.ContainsKey("idle"))
            {
                throw new InvalidOperationException("Error: Sprite definition animation does not contain an idle frame!");
            }

            return(definition);
        }