Skip to content

Instantly share code, notes, and snippets.

@xenobrain
Last active August 29, 2021 17:08
Show Gist options
  • Save xenobrain/e305c907a79e0f92825b7d83e09201c0 to your computer and use it in GitHub Desktop.
Save xenobrain/e305c907a79e0f92825b7d83e09201c0 to your computer and use it in GitHub Desktop.
tweening user fibers
module GTK
class OpenEntity
def move_to(target, duration, easing, &block)
Fiber.new do
start_time = $gtk.args.state.tick_count
until self.x == target[:x] && self.y == target[:y]
t = $gtk.args.easing.ease(start_time, $gtk.args.state.tick_count, duration, easing)
self.x += t * (target[:x] - self.x)
self.y += t * (target[:y] - self.y)
Fiber.yield
end
yield(block) if block
end
end
end
end
def initialize(args)
args.state[:actions] = []
args.state[:entities] = []
end
def tick(args)
initialize(args) if args.state.tick_count.zero?
# tick actions and remove dead
args.state[:actions].each(&:resume).select!(&:alive?)
if args.inputs.mouse.down
args.state[:entities] << entity = args.state.new_entity(:entity, {
x: args.inputs.mouse.x, y: args.inputs.mouse.y, h: 50, w: 50, path: 'sprites/square/red.png'
})
# move using quart easing over 1.5 seconds, then change the path to blue when complete
args.state[:actions] << entity.move_to({x: 700, y: 400}, 90, :quart) { entity.path = 'sprites/square/blue.png'}
end
args.outputs.sprites << args.state[:entities]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment