Skip to content

Commit

Permalink
First approach to implement Tweens
Browse files Browse the repository at this point in the history
  • Loading branch information
fguillen committed Aug 26, 2024
1 parent 28f0549 commit cea136a
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/fantasy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 4 additions & 1 deletion lib/fantasy/global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -36,6 +36,8 @@ def initialize(screen_width, screen_height)
@tile_maps = []
@clocks = []
@shapes = []
@tweens = []

@last_frame_at = Time.now
@debug = false

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/fantasy/loop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 0 additions & 13 deletions lib/fantasy/tween.rb

This file was deleted.

35 changes: 35 additions & 0 deletions lib/fantasy/tweeni.rb
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions test/tweeni_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cea136a

Please sign in to comment.