Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Created October 31, 2025 05:55
Show Gist options
  • Select an option

  • Save amirrajan/ac45811a3e7c4287048271eec771d5c7 to your computer and use it in GitHub Desktop.

Select an option

Save amirrajan/ac45811a3e7c4287048271eec771d5c7 to your computer and use it in GitHub Desktop.
DragonRuby - Fiber Animation
class MoveOrchestration
attr :camera_center, :camera_scale
def initialize(probe:, celestial_body:)
@probe = probe
@celestial_body = celestial_body
@fiber = build_fiber
@camera_center = { x: @probe.x, y: @probe.y }
@camera_scale = 1.5
end
def build_fiber
Fiber.new do
@probe.action! :moving
source = { x: @probe.x, y: @probe.y }
at = Kernel.tick_count
orbiting_id = @probe.orbiting_id
destination_id = @celestial_body.id
# move to center
loop do
target = @probe.celestial_bodies[orbiting_id]
distance = Geometry.distance(source, target)
duration = 60
perc = Easing.smooth_stop(start_at: at,
duration: duration,
tick_count: Kernel.tick_count, power: 2)
@probe.x = source.x + (target.x - source.x) * perc
@probe.y = source.y + (target.y - source.y) * perc
if at.elapsed_time >= duration
break
end
@camera_center = { x: @probe.x, y: @probe.y }
@camera_scale = 1.5
Fiber.yield
end
at = Kernel.tick_count
# move to target
loop do
target = @probe.celestial_bodies[destination_id]
source = @probe.celestial_bodies[orbiting_id]
distance = Geometry.distance(source, target)
duration = (distance.fdiv(2150) * 60).clamp(60, 300)
perc = Easing.smooth_stop(start_at: at,
duration: duration,
tick_count: Kernel.tick_count, power: 2)
@probe.x = source.x + (target.x - source.x) * perc
@probe.y = source.y + (target.y - source.y) * perc
@camera_center = { x: @probe.x, y: @probe.y }
@camera_scale = 0.5
if at.elapsed_time >= duration
@probe.orbiting_id = destination_id
@probe.orbit_at = Kernel.tick_count
@probe.destination_id = nil
@probe.destination_at = Kernel.tick_count
@probe.orbit_at = Kernel.tick_count
@probe.action! :main_menu
break
end
Fiber.yield
end
end
end
def alive?
@fiber.alive?
end
def resume
@fiber.resume
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment