tsuku 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +64 -0
- data/lib/tsuku/tween.rb +18 -3
- data/lib/tsuku/tweener.rb +50 -0
- data/lib/tsuku/version.rb +1 -1
- data/spec/easing_spec.rb +20 -0
- data/spec/tween_spec.rb +46 -8
- data/spec/tweener_spec.rb +34 -0
- metadata +8 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58899b47564289ea7e5d15ddb7b1a07431fa327d52005076d0c28cf010f64998
|
4
|
+
data.tar.gz: 3c4055edc536c72b8e85d76e86d361934c52d2feebcf734038589e5f2b1812f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 41b4c15a7d9d440e317539a34e26bad40377df61911ff94d5b21251c8108433fe1cec65055a8ff2424f709b9829b4ef84b0390af75f74d15613cda306a75323b
|
7
|
+
data.tar.gz: a014b2f37d1cbd55343759fa0160c5f8e7db204410717165fddad6608834019772f9cc836ae2635911463ce2e7e0ab36ffb1e198817cb71b6e8c920cc0afaf03
|
data/README.md
CHANGED
@@ -1,2 +1,66 @@
|
|
1
1
|
# tsuku
|
2
2
|
A tweening tool written in Ruby
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
```
|
9
|
+
gem 'tsuku'
|
10
|
+
```
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install tsuku
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
The recommended way to use Tsuku is to interface with the `Tweener` class.
|
23
|
+
First, make sure to call the `step` method in your main update loop. You will
|
24
|
+
need to provide the number of milliseconds that have elapsed since the previous
|
25
|
+
frame:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
def game_update_loop
|
29
|
+
...
|
30
|
+
|
31
|
+
Tweener.step(delta_ms)
|
32
|
+
|
33
|
+
...
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
Then use `add_tween` to create tweens as needed. For example, this tween changes
|
38
|
+
the player's x and y coordinates over a span of 3000 milliseconds:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
Tweener.add_tween(player, { x: 10, y: 5 }, 3000)
|
42
|
+
```
|
43
|
+
|
44
|
+
Any tweens created with `Tweener` will start and be cleaned up automatically.
|
45
|
+
If you need greater control, you can create a `Tween` manually like so:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
@tween = Tsuku::Tween.new(player, { x: 10, y: 5 }, 3000)
|
49
|
+
@tween.start
|
50
|
+
```
|
51
|
+
|
52
|
+
Now in your game update loop, advance the tween by calling its `step` method:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
@tween.step(delta_ms)
|
56
|
+
```
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
Bug reports and pull requests are welcome on GitHub at
|
61
|
+
https://github.com/jtuttle/tsuku.
|
62
|
+
|
63
|
+
## License
|
64
|
+
|
65
|
+
The gem is available as open source under the terms of the
|
66
|
+
[MIT License](http://opensource.org/licenses/MIT).
|
data/lib/tsuku/tween.rb
CHANGED
@@ -1,17 +1,16 @@
|
|
1
1
|
module Tsuku
|
2
2
|
class Tween
|
3
|
-
def initialize(target, final_property_values, duration_ms, easing
|
3
|
+
def initialize(target, final_property_values, duration_ms, easing = :linear)
|
4
4
|
@target = target
|
5
5
|
@final_property_values = final_property_values
|
6
6
|
@duration_ms = duration_ms
|
7
7
|
@easing = easing
|
8
8
|
|
9
9
|
@running = false
|
10
|
+
@elapsed_ms = 0.0
|
10
11
|
end
|
11
12
|
|
12
13
|
def start
|
13
|
-
@elapsed_ms = 0.0
|
14
|
-
|
15
14
|
@initial_property_values = {}
|
16
15
|
|
17
16
|
@final_property_values.each_key do |k|
|
@@ -34,6 +33,22 @@ module Tsuku
|
|
34
33
|
advance_property_values
|
35
34
|
end
|
36
35
|
|
36
|
+
def pause
|
37
|
+
@running = false
|
38
|
+
end
|
39
|
+
|
40
|
+
def resume
|
41
|
+
@running = true unless completed?
|
42
|
+
end
|
43
|
+
|
44
|
+
def reset
|
45
|
+
@elapsed_ms = 0
|
46
|
+
end
|
47
|
+
|
48
|
+
def completed?
|
49
|
+
@elapsed_ms >= @duration_ms
|
50
|
+
end
|
51
|
+
|
37
52
|
private
|
38
53
|
|
39
54
|
def advance_property_values
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require 'forwardable'
|
3
|
+
|
4
|
+
module Tsuku
|
5
|
+
class Tweener
|
6
|
+
include Singleton
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
super
|
10
|
+
|
11
|
+
@tweens = []
|
12
|
+
@running = true
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_tween(target, final_property_values, duration_ms, easing = :linear)
|
16
|
+
tween = Tween.new(target, final_property_values, duration_ms, easing)
|
17
|
+
tween.start
|
18
|
+
|
19
|
+
@tweens << tween
|
20
|
+
end
|
21
|
+
|
22
|
+
def step(delta_ms)
|
23
|
+
return if !@running
|
24
|
+
|
25
|
+
@tweens.reverse_each do |tween|
|
26
|
+
tween.step(delta_ms)
|
27
|
+
|
28
|
+
if tween.completed?
|
29
|
+
@tweens.delete(tween)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def pause
|
35
|
+
@running = false
|
36
|
+
end
|
37
|
+
|
38
|
+
def resume
|
39
|
+
@running = true
|
40
|
+
end
|
41
|
+
|
42
|
+
class << self
|
43
|
+
extend Forwardable
|
44
|
+
|
45
|
+
# delegate all instance methods to singleton instance to allow calling
|
46
|
+
# methods without '.instance' (i.e. Tweener.add_tween)
|
47
|
+
def_delegators :instance, *Tweener.instance_methods(false)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/tsuku/version.rb
CHANGED
data/spec/easing_spec.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Tsuku::Easing do
|
4
|
+
let(:elapsed_ms) { 500 }
|
5
|
+
let(:duration_ms) { 1000 }
|
6
|
+
let(:initial_value) { 10 }
|
7
|
+
let(:final_value) { 20 }
|
8
|
+
let(:value_delta) { final_value - initial_value }
|
9
|
+
|
10
|
+
it "computes linear easing correctly" do
|
11
|
+
value = Tsuku::Easing.linear(
|
12
|
+
elapsed_ms,
|
13
|
+
initial_value,
|
14
|
+
value_delta,
|
15
|
+
duration_ms
|
16
|
+
)
|
17
|
+
|
18
|
+
expect(value).to eq(15)
|
19
|
+
end
|
20
|
+
end
|
data/spec/tween_spec.rb
CHANGED
@@ -4,31 +4,63 @@ RSpec.describe Tsuku::Tween do
|
|
4
4
|
let(:target) { MockTarget.new(x: 1, y: 2, z: 3) }
|
5
5
|
|
6
6
|
let(:tween) { Tsuku::Tween.new(target, { x: 10, y: -10, z: 3 }, 1000) }
|
7
|
+
|
8
|
+
it "does not advance if start was not called" do
|
9
|
+
tween.step(1000)
|
10
|
+
expect(target.x).to eq(1)
|
11
|
+
end
|
7
12
|
|
8
|
-
it "
|
13
|
+
it "advances correctly when tweening in positive direction" do
|
9
14
|
tween.start
|
10
15
|
tween.step(1000)
|
11
16
|
expect(target.x).to eq(10)
|
12
17
|
end
|
13
18
|
|
14
|
-
it "
|
19
|
+
it "advances correctly when tweening in negative direction" do
|
15
20
|
tween.start
|
16
21
|
tween.step(1000)
|
17
22
|
expect(target.y).to eq(-10)
|
18
23
|
end
|
19
24
|
|
20
|
-
it "
|
25
|
+
it "advances correctly when target and initial values are the same" do
|
21
26
|
tween.start
|
22
27
|
tween.step(1000)
|
23
28
|
expect(target.z).to eq(3)
|
24
29
|
end
|
25
30
|
|
26
|
-
it "does not tween past target value" do
|
31
|
+
it "does not tween past the target value" do
|
27
32
|
tween.start
|
28
33
|
tween.step(5000)
|
29
34
|
expect(target.x).to eq(10)
|
30
35
|
end
|
31
36
|
|
37
|
+
it "does not advance when paused" do
|
38
|
+
tween.start
|
39
|
+
tween.step(500)
|
40
|
+
tween.pause
|
41
|
+
tween.step(500)
|
42
|
+
expect(target.x).to eq(5.5)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "resumes correctly after being paused and resumed" do
|
46
|
+
tween.start
|
47
|
+
tween.pause
|
48
|
+
tween.resume
|
49
|
+
tween.step(500)
|
50
|
+
expect(target.x).to eq(5.5)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "advances correctly after being completed and reset" do
|
54
|
+
tween.start
|
55
|
+
tween.step(1000)
|
56
|
+
|
57
|
+
target.x = 0
|
58
|
+
tween.reset
|
59
|
+
tween.start
|
60
|
+
tween.step(500)
|
61
|
+
expect(target.x).to eq(5)
|
62
|
+
end
|
63
|
+
|
32
64
|
it "tweens correctly when property value changes after tween initialization" do
|
33
65
|
target.x = 50
|
34
66
|
tween.start
|
@@ -36,9 +68,15 @@ RSpec.describe Tsuku::Tween do
|
|
36
68
|
expect(target.x).to eq(10)
|
37
69
|
end
|
38
70
|
|
39
|
-
|
40
|
-
tween
|
41
|
-
|
42
|
-
|
71
|
+
describe "#completed?" do
|
72
|
+
it "returns false when tween is not completed" do
|
73
|
+
expect(tween.completed?).to eq(false)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "returns true when tween is completed" do
|
77
|
+
tween.start
|
78
|
+
tween.step(1000)
|
79
|
+
expect(tween.completed?).to eq(true)
|
80
|
+
end
|
43
81
|
end
|
44
82
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Tsuku::Tweener do
|
4
|
+
let(:target) { MockTarget.new(x: 1, y: 2, z: 3) }
|
5
|
+
|
6
|
+
before do
|
7
|
+
Tsuku::Tweener.add_tween(target, { x: 10 }, 1000)
|
8
|
+
Tsuku::Tweener.add_tween(target, { y: 10 }, 2000)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "advances tweens correctly" do
|
12
|
+
Tsuku::Tweener.step(1000)
|
13
|
+
expect(target.x).to eq(10)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "does not advance tweens when paused" do
|
17
|
+
Tsuku::Tweener.pause
|
18
|
+
Tsuku::Tweener.step(1000)
|
19
|
+
expect(target.x).to eq(1)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "advances tweens when paused and resumed" do
|
23
|
+
Tsuku::Tweener.pause
|
24
|
+
Tsuku::Tweener.resume
|
25
|
+
Tsuku::Tweener.step(1000)
|
26
|
+
expect(target.x).to eq(10)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "advances tweens correctly when earlier tween completes" do
|
30
|
+
Tsuku::Tweener.step(1000)
|
31
|
+
Tsuku::Tweener.step(1000)
|
32
|
+
expect(target.y).to eq(10)
|
33
|
+
end
|
34
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tsuku
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Tuttle
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-11-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -84,11 +84,14 @@ files:
|
|
84
84
|
- lib/tsuku.rb
|
85
85
|
- lib/tsuku/easing.rb
|
86
86
|
- lib/tsuku/tween.rb
|
87
|
+
- lib/tsuku/tweener.rb
|
87
88
|
- lib/tsuku/version.rb
|
89
|
+
- spec/easing_spec.rb
|
88
90
|
- spec/spec_helper.rb
|
89
91
|
- spec/support/mock_target.rb
|
90
92
|
- spec/tsuku_spec.rb
|
91
93
|
- spec/tween_spec.rb
|
94
|
+
- spec/tweener_spec.rb
|
92
95
|
- tsuku.gemspec
|
93
96
|
homepage: https://github.com/jtuttle/tsuku
|
94
97
|
licenses:
|
@@ -109,12 +112,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
112
|
- !ruby/object:Gem::Version
|
110
113
|
version: '0'
|
111
114
|
requirements: []
|
112
|
-
rubygems_version: 3.0.
|
115
|
+
rubygems_version: 3.0.1
|
113
116
|
signing_key:
|
114
117
|
specification_version: 4
|
115
118
|
summary: A tweening library made with Ruby
|
116
119
|
test_files:
|
120
|
+
- spec/easing_spec.rb
|
117
121
|
- spec/spec_helper.rb
|
118
122
|
- spec/support/mock_target.rb
|
119
123
|
- spec/tsuku_spec.rb
|
120
124
|
- spec/tween_spec.rb
|
125
|
+
- spec/tweener_spec.rb
|