diff --git a/lib/fantasy.rb b/lib/fantasy.rb index f10f8ee..ff78e4a 100644 --- a/lib/fantasy.rb +++ b/lib/fantasy.rb @@ -15,7 +15,7 @@ require_relative "fantasy/includes/jumper" require_relative "fantasy/includes/user_inputs" require_relative "fantasy/includes/indexable" -require_relative "fantasy/tween" +require_relative "fantasy/tweeni" require_relative "fantasy/draggable" require_relative "fantasy/color" require_relative "fantasy/shape" diff --git a/lib/fantasy/global.rb b/lib/fantasy/global.rb index ea90c9b..85d6b75 100644 --- a/lib/fantasy/global.rb +++ b/lib/fantasy/global.rb @@ -6,7 +6,7 @@ module Global class << self include Log - attr_accessor :actors, :hud_texts, :hud_images, :animations, :backgrounds, :tile_maps, :clocks, :shapes + attr_accessor :actors, :hud_texts, :hud_images, :animations, :backgrounds, :tile_maps, :clocks, :shapes, :tweens attr_accessor :debug attr_accessor :setup_proc, :loop_proc, :button_proc attr_accessor :presentation_proc, :game_proc, :end_proc @@ -36,6 +36,8 @@ def initialize(screen_width, screen_height) @tile_maps = [] @clocks = [] @shapes = [] + @tweens = [] + @last_frame_at = Time.now @debug = false @@ -150,6 +152,7 @@ def clear_entities @backgrounds.clear @tile_maps.clear @shapes.clear + @tweens.clear @clocks.reject(&:persistent?).each do |clock| clock.stop unless clock.thread == Thread.current # no stop current Thread diff --git a/lib/fantasy/loop.rb b/lib/fantasy/loop.rb index 499f277..50017e6 100644 --- a/lib/fantasy/loop.rb +++ b/lib/fantasy/loop.rb @@ -67,6 +67,7 @@ def update Global.hud_images.each(&:move) Global.hud_images.each(&:move) Global.animations.each(&:update) + Global.tweens.each(&:update) Camera.main.move Global.loop_proc&.call diff --git a/lib/fantasy/tween.rb b/lib/fantasy/tween.rb deleted file mode 100644 index db39818..0000000 --- a/lib/fantasy/tween.rb +++ /dev/null @@ -1,13 +0,0 @@ -# frozen_string_literal: true - -# module Tween -# def self.move_towards(from:, to:, speed:) -# direction = to - from -# step = [direction.length, speed * Global.frame_time].min - -# return to if step.zero? - -# direction = direction.normalize -# from + (direction * step) -# end -# end diff --git a/lib/fantasy/tweeni.rb b/lib/fantasy/tweeni.rb new file mode 100644 index 0000000..5aa9cbd --- /dev/null +++ b/lib/fantasy/tweeni.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require "tween" + +class Tweeni + class << self + def start(from:, to:, seconds:, ease: Tween::Linear, on_end: nil, &block) + Tweeni.new(from:, to:, seconds:, ease:, on_end:, &block) + end + end + + def initialize(from:, to:, seconds:, ease: Tween::Linear, on_end: nil, &block) + @from = from + @to = to + @seconds = seconds + @ease = ease + @block = block + @on_end = on_end + + @init_at = Global.seconds_in_scene + @tween = Tween.new(@from, @to, @ease, @seconds) + + Global.tweens&.push(self) + end + + def update + @tween.update(Global.seconds_in_scene - @init_at) + @block.call(@tween.value) + + if(@tween.done) + Global.tweens.delete(self) + @on_end.call + end + end +end diff --git a/test/tweeni_test.rb b/test/tweeni_test.rb new file mode 100644 index 0000000..fb57db4 --- /dev/null +++ b/test/tweeni_test.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require "test_helper" + +class ActorTest < Minitest::Test + def setup + Global.game_proc = Proc.new {} + Global.game = Game.new(100, 100) + end + + def test_tweeni + Global.expects(:seconds_in_scene).returns(0) + + num = 0 + tween = + Tweeni.start(from: 0, to: 10, seconds: 1) do |value| + num = value + end + + Global.expects(:seconds_in_scene).returns(0.1) + tween.update + assert_equal(1, num) + + Global.expects(:seconds_in_scene).returns(0.5) + tween.update + assert_equal(6, num) + + Global.expects(:seconds_in_scene).returns(1) + tween.update + assert_equal(10, num) + end +end