Created
January 16, 2024 04:05
-
-
Save drewolbrich/2cb9b8b4586aba0a55f0a0a0c375ac43 to your computer and use it in GitHub Desktop.
Failed attempt #3 at Entity/move with a callback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Entity { | |
/// Animated `Entity/move` with a completion handler. | |
/// | |
/// This implementation works but for some reason `timingFunction` appears to be | |
/// ignored and the timing function of the resulting animation is always linear. | |
func move(to target: Transform, relativeTo referenceEntity: Entity?, duration: TimeInterval, delay: TimeInterval = 0, timingFunction: AnimationTimingFunction = .default, completion: @escaping () -> Void) { | |
guard let scene else { | |
completion() | |
return | |
} | |
let animation = FromToByAnimation(name: "Entity/move", to: target, duration: duration, timing: timingFunction, isAdditive: false, bindTarget: .transform, delay: delay) | |
let animationResource: AnimationResource | |
do { | |
animationResource = try .generate(with: animation) | |
} catch { | |
assertionFailure("Could not generate animation: \(error.localizedDescription)") | |
completion() | |
return | |
} | |
let animationPlaybackController = playAnimation(animationResource) | |
let publisher = scene.publisher(for: AnimationEvents.PlaybackCompleted.self) | |
Task { | |
_ = await publisher.values.first(where: { $0.playbackController == animationPlaybackController }) | |
completion() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment