public IEnumerator AnimateEntity(string animation, string then = null) { // Clear the current animation animationLocked = false; currentAnimation = null; playQueue.Clear(); // Find the animation var isoAnimation = isoAnimations.Find(n => n.name.Equals(animation, StringComparison.InvariantCultureIgnoreCase)); if (isoAnimation == null || isoAnimation.isoAnimation == null) { yield return(null); } // Find a sheet IsoDecoration sheet = isoAnimation.isoAnimation.overrideSheet; if (sheet == null) { NameIsoDecoration namedSheet = sheets.Find(s => { return(s.name.Equals(isoAnimation.isoAnimation.sheet, StringComparison.InvariantCultureIgnoreCase)); }); if (namedSheet != null) { sheet = namedSheet.isoDecoration; } } if (sheet == null) { yield return(null); } var ge = Current; // Stop all other animations foreach (var kv in run) { run[kv.Key] = false; } // Add the new one run.Add(ge, true); // Perform the animation foreach (var frame in isoAnimation.isoAnimation.frames) { SetFrame(sheet, frame.column); yield return(new WaitForSeconds(frame.duration)); if (!run[ge]) { break; } } var desiredRun = run[ge]; run.Remove(ge); if (desiredRun && !string.IsNullOrEmpty(then)) { Play(then); } }
public override void Update() { var timeLeft = Time.deltaTime; var desiredFrame = currentFrame; var desiredAnimation = currentAnimation; var lastValidAnimation = currentAnimation; var lastValidFrame = currentFrame; if (run.Count == 0 && (desiredAnimation != null || playQueue.Count > 0)) { // While there's time left to reproduce while (timeLeft > 0 && !animationLocked) { // Look for the next playable animation while (desiredAnimation == null && playQueue.Count > 0) { var animationToPlay = playQueue.Dequeue(); var foundAnimation = isoAnimations.Find(n => n.name.Equals(animationToPlay, StringComparison.InvariantCultureIgnoreCase)); if (foundAnimation == null || foundAnimation.isoAnimation == null || foundAnimation.isoAnimation.frames.Count == 0) { continue; } currentSheet = foundAnimation.isoAnimation.overrideSheet; if (currentSheet == null) { var foundSheet = sheets.Find(n => n.name.Equals(foundAnimation.isoAnimation.sheet, StringComparison.InvariantCultureIgnoreCase)); if (foundSheet != null) { currentSheet = foundSheet.isoDecoration; } } if (currentSheet == null) { continue; } lastValidAnimation = desiredAnimation = foundAnimation.isoAnimation; lastValidFrame = desiredFrame = 0; timeInCurrentFrame = 0f; } // And if we have animation we play it if (desiredAnimation != null) { var looped = false; var previousLoopedTimeLeft = 0f; // We move through the frames untill we run out of time or frames while (desiredAnimation != null && timeLeft > 0 && desiredFrame < desiredAnimation.frames.Count) { timeInCurrentFrame += timeLeft; if (timeInCurrentFrame > desiredAnimation.frames[desiredFrame].duration) { timeLeft = timeInCurrentFrame - desiredAnimation.frames[desiredFrame].duration; lastValidFrame = desiredFrame++; timeInCurrentFrame = 0f; } else { timeLeft = 0; } if (desiredFrame == desiredAnimation.frames.Count) { desiredFrame = 0; // In case of loop if (desiredAnimation.loop) { lastValidFrame = 0; // In case we already looped and time didn't decrease, this is an infinite loop if (looped && previousLoopedTimeLeft == timeLeft) { animationLocked = true; break; } previousLoopedTimeLeft = timeLeft; looped = true; } else { desiredAnimation = null; } } } } // But otherwise we exit else { break; } } } if (currentAnimation != desiredAnimation || currentAnimation != lastValidAnimation || currentFrame != desiredFrame || currentFrame != lastValidFrame) { currentAnimation = desiredAnimation; currentFrame = desiredFrame; if (lastValidAnimation != null && lastValidFrame < lastValidAnimation.frames.Count) { SetFrame(currentSheet, lastValidAnimation.frames[lastValidFrame].column); } } }