Example #1
0
 /// <summary>
 /// Load animations from file.
 /// </summary>
 /// <param name="uri">The directory to search for files</param>
 private void LoadAnimations(string uri)
 {
     Game.Log.Load(" - Searching: " + uri);
     foreach (var fileUri in Directory.GetFiles(uri, "*.adat", SearchOption.AllDirectories))
     {
         using (var file = File.OpenRead(fileUri)) {
             try {
                 var animation = JsonSpriteAnimation.FromStream(file);
                 if (animation == null)
                 {
                     Game.Log.Error("Failed to load sprite sheet from file: " + fileUri);
                 }
                 else if (animations.ContainsKey(animation.Key))
                 {
                     Game.Log.Error(" - Attempted to add animation with existing key: " + animation.Key);
                     Game.Log.Error(" -  - When loading file: " + fileUri);
                 }
                 else
                 {
                     Game.Log.Debug(String.Format("Loaded '{0}' from {1}", animation.Key, fileUri));
                     animation.Initialize(this);
                     animations[animation.Key] = animation;
                 }
             } catch (Exception ex) {
                 Game.Log.Error("Exception loading animation: " + ExceptionToS(ex));
             }
         }
     }
 }
 /// <summary>
 /// Parse an Animation from file
 /// </summary>
 /// <param name="fileUri">The file to parse from</param>
 /// <param name="errors">Any resulting errors</param>
 /// <returns>The parsed Animation</returns>
 public static JsonSpriteAnimation ParseAnimation(string fileUri, IList <string> errors)
 {
     try {
         var res = new JsonSpriteAnimation();
         using (var file = File.OpenText(fileUri)) {
             var            parsedObject = JObject.Parse(file.ReadToEnd());
             IList <string> parseErrors;
             parsedObject.IsValid(Schemas.GetAnimationSchema(), out parseErrors);
             if (parseErrors.Count > 0)
             {
                 foreach (var error in parseErrors)
                 {
                     errors.Add(String.Format("Parsing animation: {0}", error));
                 }
                 return(null);
             }
             JArray version = (JArray)parsedObject["version"];
             (int major, int minor) = ((int)version[0], (int)version[1]);
             // TODO: Check animation file version
             res.Key            = (string)parsedObject["key"];
             res.spriteSheetKey = (string)parsedObject["spritesheet"];
             bool bounce = false;
             if (parsedObject.ContainsKey("bounce"))
             {
                 bounce = (bool)parsedObject["bounce"];
             }
             if (parsedObject.ContainsKey("frame_length"))
             {
                 int               frameStart  = (int)parsedObject["frame_start"];
                 int               frameStop   = (int)parsedObject["frame_stop"];
                 int               count       = Math.Abs(frameStop - frameStart) + 1;
                 double            frameLength = (double)parsedObject["frame_length"];
                 IEnumerable <int> range       = Enumerable.Range(Math.Min(frameStart, frameStop), count);
                 if (frameStop < frameStart)
                 {
                     range = range.Reverse();
                 }
                 var frames = new List <(int, double)>();
                 foreach (int frame in range)
                 {
                     frames.Add((frame, frameLength));
                 }
                 res.Frames = JsonSpriteAnimation.CreateFrames(bounce, frames.ToArray());
             }
             else
             {
                 var frames     = new List <(int, double)>();
                 var frameArray = (JArray)parsedObject["frames"];
                 foreach (var frameNode in frameArray)
                 {
                     var    frameObj = frameNode as JObject;
                     int    frame    = (int)frameObj["frame"];
                     double length   = (double)frameObj["length"];
                     frames.Add((frame, length));
                 }
                 res.Frames = JsonSpriteAnimation.CreateFrames(bounce, frames.ToArray());
             }
         }
         return(res);
     } catch (Exception ex) {
         errors.Add(String.Format("Exception parsing animation: <{0}:{2}> {1}", ex.GetType().Name, ex.Message, ex.Source));
         return(null);
     }
 }