public override void Update(GameTime gameTime) { if (currentRegion == null) return; if (AutoAnimated) { DateTime currentFrameProcessDate = DateTime.Now; if (oldProcessFrameDate == null) oldProcessFrameDate = oldProcessFrameDate.Subtract(TimeSpan.FromMilliseconds(AnimationSpeed)); if (AnimationSpeed == 0 || currentFrameProcessDate.Subtract(oldProcessFrameDate).TotalMilliseconds >= AnimationSpeed) { oldProcessFrameDate = currentFrameProcessDate; currentFrame++; if (currentFrame >= currentRegion.Columns) { if (AnimateAllRegions) { var nextRegion = regions.FirstOrDefault(x => x.Row == currentRegion.Row + 1); currentRegion = (nextRegion != null) ? nextRegion : regions.First(x => x.Row == 1); } currentFrame = 0; } } } destRect = new Rectangle((int)Position.X, (int)Position.Y, currentRegion.FrameWidth, currentRegion.FrameHeight); sourceRect = new Rectangle(currentRegion.FrameWidth * currentFrame, currentRegion.FrameHeight * (currentRegion.Row - 1), currentRegion.FrameWidth, currentRegion.FrameHeight); base.Update(gameTime); }
public void SetRegion(string regionName) { if (string.IsNullOrWhiteSpace(regionName)) throw new UtilityException("region name cannot be null or empty!"); if (currentRegion == null || !string.Equals(currentRegion.Name, regionName)) { currentRegion = regions.FirstOrDefault(x => string.Equals(x.Name, regionName)); currentFrame = 0; } }