/// <param name="storePrevious">If true, you can restore to the previous definition by calling RevertDefinition.</param> public void SetDefinition(SpriteAnimationDefinition definition, bool storePrevious = true, bool resetFrame = false, bool force = false) { if (Definition == definition && !force) { return; } if (storePrevious) { lastAnimationName = Definition.Name; } if (resetFrame) { currentFrame = 0; } else { float currentIndexRatio = (float)currentFrame / (float)Definition.Frames.Length; currentFrame = (int)(definition.Frames.Length * currentIndexRatio); // Set the next animation's frame index relative to the current animation and index. } Definition = definition; playOnce = false; animationDirection = 1; resetWaitTime(); }
/// <summary>Called when sprite using this animation is reloaded, used to transparently switch out to the new definition.</summary> public void ReloadDefinition(SpriteAnimationDefinition animDefinition) { Definition = animDefinition; if (currentFrame >= Definition.Frames.Length) { currentFrame = 0; } }
public SpriteAnimation(SpriteAnimationDefinition definition, SpriteDefinition spriteDefinition) { Definition = definition; SpriteDefinition = spriteDefinition; Paused = definition.Frames.Length <= 1; // Pause the animation if there's only one frame. currentFrame = 0; resetWaitTime(); }
public void PlayOnce(SpriteAnimationDefinition animDefinition) { if (Definition == animDefinition) { return; } SetDefinition(animDefinition, true, true); playOnce = true; }
public void PlayOnce(SpriteAnimationDefinition animDefinition) { if (Definition == animDefinition) return; SetDefinition(animDefinition, true, true); playOnce = true; }
/// <summary>Called when sprite using this animation is reloaded, used to transparently switch out to the new definition.</summary> public void ReloadDefinition(SpriteAnimationDefinition animDefinition) { Definition = animDefinition; if (currentFrame >= Definition.Frames.Length) currentFrame = 0; }
/// <param name="storePrevious">If true, you can restore to the previous definition by calling RevertDefinition.</param> public void SetDefinition(SpriteAnimationDefinition definition, bool storePrevious = true, bool resetFrame = false, bool force = false) { if (Definition == definition && !force) return; if (storePrevious) lastAnimationName = Definition.Name; if (resetFrame) { currentFrame = 0; } else { float currentIndexRatio = (float)currentFrame / (float)Definition.Frames.Length; currentFrame = (int)(definition.Frames.Length * currentIndexRatio); // Set the next animation's frame index relative to the current animation and index. } Definition = definition; playOnce = false; animationDirection = 1; resetWaitTime(); }
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; }
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); }