/// <summary> /// Load animation from config asset, based on animation identifier. /// </summary> /// <param name="identifier">Animation identifier.</param> /// <param name="config">Config asset to load from.</param> /// <remarks>Config must contain a section called 'anim_xxx', where xxx is this animation's unique identifier. /// Under this section, we should have the following keys: /// - repeats = true / false - does this animation loops, or remain stuck on last step after done? /// - steps_count = how many steps we have in this animation. /// - setp_x_duration[x is step index] = duration, in seconds, of this animation step. /// - setp_x_source[x is step index] = index in spritesheet file, format is: "x,y". /// For more info, check out demo_spritesheet.ini in test assets folder.</remarks> public SpriteAnimation(string identifier, ConfigAsset config) { // set identifier Identifier = identifier; // get general stuff string section = "anim_" + identifier; Repeats = config.GetBool(section, "repeats"); int steps = config.GetInt(section, "steps_count", -1); // sanity if (steps < 0) { throw new Exception($"Missing animation {identifier} section in config file, or missing 'steps_count' key!"); } // load all steps for (int i = 0; i < steps; ++i) { string prefix = "step_" + i.ToString(); float duration = config.GetFloat(section, prefix + "_duration"); PointI index = PointI.FromString(config.GetStr(section, prefix + "_source")); AddStep(new SpriteAnimationStep() { Duration = duration, Index = index }); } }
/// <summary> /// Load animation from config asset, based on animation identifier. /// </summary> /// <param name="identifier">Animation identifier.</param> /// <param name="config">Config asset to load from.</param> /// <remarks>Config must contain a section called 'anim_xxx', where xxx is this animation's unique identifier. /// Under this section, we should have the following keys: /// - repeats = true / false - does this animation loops, or remain stuck on last step after done? /// - steps_count = how many steps we have in this animation. /// - setp_x_duration [x is step index] = duration, in seconds, of this animation step. /// - setp_x_source [x is step index] = index in spritesheet file, format is: "x,y". /// - step_x_tag [x is step index] = optional tag to attach to this step. /// For more info, check out demo_spritesheet.ini in test assets folder.</remarks> public SpriteAnimation(string identifier, ConfigAsset config) { // set identifier Identifier = identifier; // get general stuff string section = "anim_" + identifier; Repeats = config.GetBool(section, "repeats"); int steps = config.GetInt(section, "steps_count", -1); // sanity if (steps < 0) { throw new Exception($"Missing animation {identifier} section in config file, or missing 'steps_count' key!"); } // load all steps for (int i = 0; i < steps; ++i) { string prefix = "step_" + i.ToString(); // read duration float duration = config.GetFloat(section, prefix + "_duration", -100f); if (duration == -100f) { throw new FormatException($"Missing or invalid duration value for step {i} in animation '{identifier}'."); } // read source index PointI index = config.GetPointI(section, prefix + "_source", new PointI(-100, -100)); if (index.X == -100f) { throw new FormatException($"Missing or invalid source value for step {i} in animation '{identifier}'."); } // read optional tag and add step string tag = config.GetStr(section, prefix + "_tag", null); AddStep(new SpriteAnimationStep() { Duration = duration, Index = index, Tag = tag }); } }