/// <summary> /// Load spritesheet properties from config asset. /// Note: this will replace all existing settings. /// </summary> /// <param name="config">Config file to load from.</param> /// <remarks>Config must contain the following section: /// [general] /// - sprites_count = how many sprites there are in this spritesheet, format is: "x,y". /// - animations = list of comma-separated animations found in this spritesheet config. /// for every animation listed here, you need to also include a section with animation data. /// check out 'SpriteAnimation' constructor for more info. /// /// [bookmarks] /// - optional, contains a list of values where every key is a bookmark identifier and value is sprite index "x,y". /// later, you can use this to set sprites from spritesheet by names. for example: sheet.SetSprite(sprite, "item_sword"); /// /// For more info, check out demo_spritesheet.ini in test assets folder. /// </remarks> public void LoadFromConfig(ConfigAsset config) { // load general settings SpritesCount = config.GetPointI("general", "sprites_count", PointI.Zero); // load bookmarks var bookmarks = config.Keys("bookmarks"); foreach (var book in bookmarks) { var value = config.GetPointI("bookmarks", book, PointI.Zero); _bookmarks.Add(book, value); } // load animations var animations = config.GetStr("general", "animations", "").Split(','); foreach (var anim in animations) { var animationName = anim.Trim(); var spriteAnimation = new SpriteAnimation(animationName, config); _animations.Add(animationName, spriteAnimation); } }
/// <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 }); } }