// Draw an animation during at the given time stamp and position. public void DrawAnimation(Animation animation, int variantID, float time, float x, float y, Color color, float depth = 0.0f) { if (animation.LoopMode == LoopMode.Repeat) { if (animation.Duration == 0) { time = 0; } else { time %= animation.Duration; } } x = GMath.Round(x); y = GMath.Round(y); for (int i = 0; i < animation.Frames.Count; ++i) { AnimationFrame frame = animation.Frames[i]; if (time < frame.StartTime) { return; } if (time < frame.StartTime + frame.Duration || (time >= animation.Duration && frame.StartTime + frame.Duration == animation.Duration)) { DrawSprite(frame.Sprite, variantID, x, y, color, depth); } } }
//----------------------------------------------------------------------------- // Mutators //----------------------------------------------------------------------------- public void AddFrame(AnimationFrame frame) { int index = 0; while (index < frames.Count && frame.StartTime > frames[index].StartTime) { ++index; } frames.Insert(index, frame); duration = Math.Max(duration, frame.StartTime + frame.Duration); }
public AnimationFrame(AnimationFrame copy) { this.startTime = copy.startTime; this.duration = copy.duration; this.sprite = new Sprite(copy.sprite); }
//----------------------------------------------------------------------------- // Mutators //----------------------------------------------------------------------------- public void AddFrame(AnimationFrame frame) { int index = 0; while (index < frames.Count && frame.StartTime > frames[index].StartTime) ++index; frames.Insert(index, frame); duration = Math.Max(duration, frame.StartTime + frame.Duration); }
//----------------------------------------------------------------------------- // Midifications //----------------------------------------------------------------------------- public AnimationBuilder RepeatPreviousFrames(int numFrames, int numRepeats) { int start = animation.Frames.Count - numFrames; for (int i = 0; i < numRepeats; i++) { for (int j = 0; j < numFrames; j++) { AnimationFrame frame = new AnimationFrame(animation.Frames[start + j]); frame.StartTime = animation.Duration; animation.AddFrame(frame); } } return this; }
public AnimationBuilder MakeQuad() { int numFrames = animation.Frames.Count; AnimationFrame[] frames = new AnimationFrame[numFrames]; for (int i = 0; i < numFrames; ++i) frames[i] = new AnimationFrame(animation.Frames[i]); for (int i = 0; i < numFrames; ++i) { for (int x = 0; x < 2; ++x) { for (int y = 0; y < 2; ++y) { if (x > 0 || y > 0) { frames[i].Sprite.DrawOffset = new Point2I(8 * x, 8 * y); animation.AddFrame(new AnimationFrame(frames[i])); } } } } return this; }
public AnimationBuilder MakeFlicker(int alternateDelayTicks, bool startOn = true) { Animation newAnimation = new Animation(); for (int i = 0; i < animation.Frames.Count; i++) { AnimationFrame frame = animation.Frames[i]; int beginSection = frame.StartTime / (alternateDelayTicks * 2); int endSection = frame.EndTime / (alternateDelayTicks * 2); if (frame.EndTime % (alternateDelayTicks * 2) == 0) endSection--; for (int section = beginSection; section <= endSection; section++) { int t = section * alternateDelayTicks * 2; if (frame.StartTime < t + alternateDelayTicks && frame.EndTime > t) { AnimationFrame newFrame = new AnimationFrame(); newFrame.Sprite = frame.Sprite; newFrame.StartTime = Math.Max(frame.StartTime, t); newFrame.Duration = Math.Min(frame.EndTime, t + alternateDelayTicks) - newFrame.StartTime; newAnimation.AddFrame(newFrame); } } } animation.Frames = newAnimation.Frames; return this; }
public AnimationBuilder MakeDynamic(int numSubStrips, int offsetX, int offsetY) { Animation subStrip = animation; Point2I offset = new Point2I(offsetX, offsetY); for (int i = 1; i < numSubStrips; i++) { subStrip.NextStrip = new Animation(); subStrip = subStrip.NextStrip; subStrip.LoopMode = animation.LoopMode; for (int j = 0; j < animation.Frames.Count; j++) { AnimationFrame frame = new AnimationFrame(animation.Frames[j]); frame.Sprite.SourceRect = new Rectangle2I( frame.Sprite.SourceRect.Point + (i * ((sheet.CellSize + sheet.Spacing) * offset)), frame.Sprite.SourceRect.Size ); subStrip.AddFrame(frame); } } animation = subStrip; return this; }