3 using System.Collections.Generic;
5 using SiliconStudio.Core;
6 using SiliconStudio.Paradox.DataModel;
7 using SiliconStudio.Paradox.Games;
9 namespace SiliconStudio.
Paradox.Engine
16 private readonly HashSet<SpriteComponent> playingSprites =
new HashSet<SpriteComponent>();
17 private readonly HashSet<SpriteComponent> spritesToStop =
new HashSet<SpriteComponent>();
22 public float DefaultFramesPerSecond {
get; set; }
33 DefaultFramesPerSecond = 30;
47 var elapsedTime = gameTime.Elapsed.TotalSeconds;
49 foreach (var sprite
in playingSprites)
54 sprite.ElapsedTime += elapsedTime;
57 while (sprite.Animations.Count > 0)
59 var animationInfo = sprite.Animations.Peek();
60 var oneFrameTime = 1 / animationInfo.FramePerSeconds;
63 while (sprite.ElapsedTime >= oneFrameTime && (animationInfo.ShouldLoop || sprite.CurrentIndexIndex < animationInfo.SpriteIndices.Count-1))
65 sprite.ElapsedTime -= oneFrameTime;
66 sprite.CurrentIndexIndex = (sprite.CurrentIndexIndex + 1) % animationInfo.SpriteIndices.Count;
70 sprite.CurrentFrame = animationInfo.SpriteIndices[sprite.CurrentIndexIndex];
73 if (sprite.ElapsedTime >= oneFrameTime)
75 sprite.ElapsedTime -= oneFrameTime;
76 sprite.RecycleFirstAnimation();
85 if (sprite.Animations.Count == 0)
86 spritesToStop.Add(sprite);
90 foreach (var spriteComponent
in spritesToStop)
92 playingSprites.Remove(spriteComponent);
93 spriteComponent.CurrentIndexIndex = 0;
94 spriteComponent.ElapsedTime = 0;
96 spritesToStop.Clear();
110 if(spriteComponent == null)
113 var animationInfo =
new SpriteComponent.AnimationInfo
115 ShouldLoop = repeatMode == AnimationRepeatMode.LoopInfinite,
116 SpriteIndices = SpriteComponent.GetNewSpriteIndicesList(),
117 FramePerSeconds = framesPerSeconds > 0 ? framesPerSeconds : DefaultFramesPerSecond,
120 for (
int i = startIndex; i <= endIndex; i++)
121 animationInfo.SpriteIndices.Add(i);
123 spriteComponent.RecycleFirstAnimation();
124 spriteComponent.Animations.Enqueue(animationInfo);
125 var queuedAnimationsCount = spriteComponent.Animations.Count - 1;
126 for (
int i = 0; i < queuedAnimationsCount; i++)
128 var queuedAnimation = spriteComponent.Animations.Dequeue();
129 if(!clearQueuedAnimations)
130 spriteComponent.Animations.Enqueue(queuedAnimation);
133 playingSprites.Add(spriteComponent);
134 spriteComponent.ElapsedTime = 0;
135 spriteComponent.CurrentIndexIndex = 0;
136 spriteComponent.IsPaused =
false;
149 if (spriteComponent == null)
152 var animationInfo =
new SpriteComponent.AnimationInfo
154 ShouldLoop = repeatMode == AnimationRepeatMode.LoopInfinite,
155 SpriteIndices = SpriteComponent.GetNewSpriteIndicesList(),
156 FramePerSeconds = framesPerSeconds > 0 ? framesPerSeconds : DefaultFramesPerSecond,
159 foreach (var i
in indices)
160 animationInfo.SpriteIndices.Add(i);
162 spriteComponent.RecycleFirstAnimation();
163 spriteComponent.Animations.Enqueue(animationInfo);
164 var queuedAnimationsCount = spriteComponent.Animations.Count - 1;
165 for (
int i = 0; i < queuedAnimationsCount; i++)
167 var queuedAnimation = spriteComponent.Animations.Dequeue();
168 if (!clearQueuedAnimations)
169 spriteComponent.Animations.Enqueue(queuedAnimation);
172 playingSprites.Add(spriteComponent);
173 spriteComponent.ElapsedTime = 0;
174 spriteComponent.CurrentIndexIndex = 0;
175 spriteComponent.IsPaused =
false;
188 if (spriteComponent == null)
191 var animationInfo =
new SpriteComponent.AnimationInfo
193 ShouldLoop = repeatMode == AnimationRepeatMode.LoopInfinite,
194 FramePerSeconds = framesPerSeconds > 0 ? framesPerSeconds : DefaultFramesPerSecond,
195 SpriteIndices = SpriteComponent.GetNewSpriteIndicesList()
198 for (
int i = startIndex; i <= endIndex; i++)
199 animationInfo.SpriteIndices.Add(i);
201 spriteComponent.Animations.Enqueue(animationInfo);
203 playingSprites.Add(spriteComponent);
215 if (spriteComponent == null)
218 var animationInfo =
new SpriteComponent.AnimationInfo
220 ShouldLoop = repeatMode == AnimationRepeatMode.LoopInfinite,
221 FramePerSeconds = framesPerSeconds > 0 ? framesPerSeconds : DefaultFramesPerSecond,
222 SpriteIndices = SpriteComponent.GetNewSpriteIndicesList()
225 foreach(var i
in indices)
226 animationInfo.SpriteIndices.Add(i);
228 spriteComponent.Animations.Enqueue(animationInfo);
230 playingSprites.Add(spriteComponent);
239 spriteComponent.IsPaused =
true;
248 spriteComponent.IsPaused =
false;
257 spriteComponent.ElapsedTime = 0;
258 spriteComponent.CurrentIndexIndex = 0;
259 spriteComponent.ClearAnimations();
260 playingSprites.Remove(spriteComponent);
void Stop(SpriteComponent spriteComponent)
Stops the animation of the provided sprite component.
A system in charge of animating the sprites
override void Draw(GameTime gameTime)
Draws this instance.
void Queue(SpriteComponent spriteComponent, int[] indices, AnimationRepeatMode repeatMode, float framesPerSeconds=0)
Queue the sprite animation defined by the provided sequence of indices at the end of the animation qu...
void Play(SpriteComponent spriteComponent, int startIndex, int endIndex, AnimationRepeatMode repeatMode, float framesPerSeconds=0, bool clearQueuedAnimations=true)
Play the sprite animation starting at index startIndex and ending at endIndex .
A service registry is a IServiceProvider that provides methods to register and unregister services...
SpriteAnimationSystem(IServiceRegistry registry)
Creates a new instance of SpriteAnimationSystem and register it in the services.
void Queue(SpriteComponent spriteComponent, int startIndex, int endIndex, AnimationRepeatMode repeatMode, float framesPerSeconds=0)
Queue the sprite animation starting at index startIndex and ending at endIndex at the end of the an...
Add a Sprite to an Entity. It could be an animated sprite.
AnimationRepeatMode
Enumeration describing how an animation should be repeated.
Current timing used for variable-step (real time) or fixed-step (game time) games.
override void Initialize()
This method is called when the component is added to the game.
void Play(SpriteComponent spriteComponent, int[] indices, AnimationRepeatMode repeatMode, float framesPerSeconds=0, bool clearQueuedAnimations=true)
Play the sprite animation defined by the provided sequence of indices.
void Pause(SpriteComponent spriteComponent)
Pauses the animation of the provided sprite component.
void Resume(SpriteComponent spriteComponent)
Resumes a previously paused animation.