Exemple #1
0
        public static SpriteAnimation parseFrom(StreamReader input, SpriteSheetCollection spriteSheets)
        {
            if (input == null || spriteSheets == null)
            {
                return(null);
            }

            SpriteAnimation animation;
            VariableSystem  animationProperties = new VariableSystem();

            // store all of the animation properties
            String   data;
            Variable property;

            do
            {
                data = input.ReadLine();
                if (data == null)
                {
                    return(null);
                }

                data = data.Trim();
                if (data.Length == 0)
                {
                    continue;
                }

                property = Variable.parseFrom(data);
                if (property == null)
                {
                    return(null);
                }

                animationProperties.add(property);
            } while(animationProperties.size() < 7);

            // parse the animation name
            String animationName = animationProperties.getValue("Animation Name");

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

            // parse the animation type
            SpriteAnimationType animationType = parseType(animationProperties.getValue("Animation Type"));

            if (animationType == SpriteAnimationType.Invalid)
            {
                return(null);
            }

            // parse the duration of the animation
            float duration;

            try {
                duration = float.Parse(animationProperties.getValue("Animation Duration"));
            }
            catch (Exception) { return(null); }
            if (duration <= 0)
            {
                return(null);
            }

            // parse the number of frames in the animation
            int numberOfFrames;

            try {
                numberOfFrames = int.Parse(animationProperties.getValue("Number of Frames"));
            }
            catch (Exception) { return(null); }
            if (numberOfFrames <= 0)
            {
                return(null);
            }

            // get the sprite sheet name
            String spriteSheetName = animationProperties.getValue("SpriteSheet Name");

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

            // get the sprite name
            String spriteName = animationProperties.getValue("Sprite Name");

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

            string startIndex  = animationProperties.getValue("Start Index");
            int    iStartIndex = 1;

            if (startIndex != null)
            {
                iStartIndex = int.Parse(startIndex);
            }

            animation = new SpriteAnimation(animationName, duration, animationType);

            // get the sprite sheet which contains the animation
            SpriteSheet spriteSheet = spriteSheets.getSpriteSheet(spriteSheetName);

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

            // obtain and add the sprites to the sprite animation
            Sprite sprite;

            for (int i = 0; i < numberOfFrames; i++)
            {
                sprite = spriteSheet.getSprite(spriteName + " " + (i + iStartIndex));
                if (sprite == null)
                {
                    return(null);
                }

                animation.addSprite(sprite);
            }

            return(animation);
        }