Skip to content

Instantly share code, notes, and snippets.

@drewolbrich
Created January 16, 2024 04:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drewolbrich/2cb9b8b4586aba0a55f0a0a0c375ac43 to your computer and use it in GitHub Desktop.
Save drewolbrich/2cb9b8b4586aba0a55f0a0a0c375ac43 to your computer and use it in GitHub Desktop.
Failed attempt #3 at Entity/move with a callback
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