gosu-spritesheet 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f816d42837a8669558a069096ade4bf64d232df70079bbefdcb2c085aba8e5e8
4
+ data.tar.gz: 381319501277c0c07fa7cc47c891fc97226e9d32f0cac57adcdb0eb87af7a4cf
5
+ SHA512:
6
+ metadata.gz: 007a14d835267d101a4cd70731a006582733c54db30572477b6322b785b3e0aee0bdeb81b2995da23382af5f2b79e85437c9ed7e4e81fc6e130b9166bce2de9b
7
+ data.tar.gz: 0b5810e298823daeabefd5aeda2d7cb2e13e7c8c0914c6054d92b055036a4e32a5f68b9ac34adb069aac2e3289665c0edd142950152fa5b383dee6bc16212da2
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 pogist
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,153 @@
1
+ # Installation
2
+
3
+ Add this line to your application's Gemfile:
4
+
5
+ ```ruby
6
+ gem 'gosu-spritesheet'
7
+ ```
8
+
9
+ And then execute:
10
+
11
+ $ bundle
12
+
13
+ Or install it yourself as:
14
+
15
+ $ gem install gosu-spritesheet
16
+
17
+ # Usage
18
+
19
+ ### Creating a simple spritesheet animation
20
+
21
+ ```ruby
22
+ require 'gosu'
23
+ require 'gosu/spritesheet'
24
+
25
+ class Game < Gosu::Window
26
+ def initialize
27
+ tiles = Gosu::Image.load_tiles 'path/to/your/tiles.png', 10, 10
28
+ @spritesheet = Gosu::Spritesheet.new { :tiles => tiles }
29
+ end
30
+
31
+ def draw
32
+ x, y, z = #...the position of your animation on screen.
33
+
34
+ # Just draws the next animation frame on each iteration.
35
+ @spritesheet.animation(:default).step.draw x, y, z
36
+ end
37
+ end
38
+
39
+ Game.new.show
40
+ ```
41
+
42
+ ### A more complex example using custom animations
43
+
44
+ Consider that your game object may have more than one animation with different
45
+ frames each, but those frames are on the same image. That way, you can define
46
+ different animations for each set of frames:
47
+
48
+ ```ruby
49
+ require 'gosu'
50
+ require 'gosu/spritesheet'
51
+
52
+ class Character
53
+ # x, y, z are our character's initial position.
54
+ def initialize(x, y, z)
55
+ @x, @y, @z = x, y, z
56
+ tiles = Gosu::Image.load_tiles 'path/to/your/tiles.png', 10, 10
57
+
58
+ # Suppose your tile has 8 frames.
59
+ #
60
+ # The first 4 frames are your character walking left.
61
+ # And the other 4 your character walking right.
62
+ @spritesheet = Gosu::Spritesheet.new({
63
+ :tiles => tiles,
64
+ :animations => {
65
+ # Here we are taking the first 4 frames as the walking left
66
+ # animation frames.
67
+ :walk_left => { range: [0..3], duration: 0.2 },
68
+ # The other 4 are the frames of walking right.
69
+ :walk_right => { range: [4..7], duration: 0.2 }
70
+ }
71
+ })
72
+
73
+ # By default our character starts facing left and stopped.
74
+ @move_direction = :left
75
+ @moving = false
76
+ end
77
+
78
+ def draw
79
+ # Let's decide which animation to draw.
80
+ animation_key = @move_direction == :left ? :walk_left : :walk_right
81
+
82
+ if @moving
83
+ # If our character is moving let's animate it!
84
+ @spritesheet.animation(animation_key).step.draw @x, @y, @z
85
+ else
86
+ # When our character isn't moving let's just draw its stopped frame.
87
+ @spritesheet.animation(animation_key).stop.draw @x, @y, @z
88
+ end
89
+ end
90
+
91
+ def walk(direction, speed)
92
+ @moving = true
93
+ @x += direction == :right ? speed : -speed
94
+ end
95
+
96
+ def stop_moving
97
+ @moving = false
98
+ end
99
+ end
100
+ ```
101
+
102
+ Now that we have our character class ready let's use it!
103
+
104
+ ```ruby
105
+ class Game < Gosu::Window
106
+ def initialize
107
+ super 800, 600
108
+ # Let's start our character at the center of the screen.
109
+ @char = Character.new 400, 300
110
+ end
111
+
112
+ def draw
113
+ @char.draw
114
+ end
115
+
116
+ def update
117
+ if pressing_right?
118
+ @char.walk :right, 2
119
+ end
120
+
121
+ if pressing_left?
122
+ @char.walk :left, 2
123
+ end
124
+ end
125
+
126
+ def button_up(id)
127
+ # When the user stops pressing any button let's tell our character to stop
128
+ # moving.
129
+ @char.stop_moving
130
+ end
131
+
132
+ # Let's define some helper functions
133
+ def pressing_right?
134
+ Gosu::button_down?(Gosu::KbRight) or Gosu::button_down?(Gosu::GpRight)
135
+ end
136
+
137
+ def pressing_left?
138
+ Gosu::button_down?(Gosu::KbLeft) or Gosu::button_down?(Gosu::GpLeft)
139
+ end
140
+ end
141
+
142
+ Game.new.show
143
+ ```
144
+
145
+ That's it.
146
+
147
+ ## Contributing
148
+
149
+ Just open a pull request. I'll read it, I promise.
150
+
151
+ ## License
152
+
153
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,40 @@
1
+ require File.expand_path("../lib/gosu/spritesheet/version.rb", __FILE__)
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "gosu-spritesheet"
5
+ spec.version = Gosu::Spritesheet.version
6
+ spec.authors = ["pogist"]
7
+ spec.email = ["murilo.paixao.2@gmail.com"]
8
+
9
+ spec.summary = "Useful spritesheet extension for Gosu."
10
+ spec.description = "A useful and yet simple spritesheet extension for Gosu."
11
+ spec.homepage = "https://github.com/pogist/gosu-spritesheet"
12
+ spec.license = "MIT"
13
+ spec.files = Dir['lib/**/*.rb'] + [
14
+ "README.md",
15
+ "LICENSE.md",
16
+ "Rakefile",
17
+ "gosu-spritesheet.gemspec"
18
+ ]
19
+ spec.test_files = Dir['test/**/*.rb', 'test/*.rb']
20
+
21
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
22
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
23
+ if spec.respond_to?(:metadata)
24
+ spec.metadata = {
25
+ "homepage_uri" => spec.homepage,
26
+ "source_code_uri" => spec.homepage
27
+ }
28
+ else
29
+ raise "RubyGems 2.0 or newer is required to protect against " \
30
+ "public gem pushes."
31
+ end
32
+
33
+ spec.add_dependency "gosu", "~> 0.14"
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.17"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "minitest", "~> 5.0"
38
+ spec.add_development_dependency "minitest-reporters", "~> 1.3"
39
+ spec.add_development_dependency "byebug", "~> 10.0"
40
+ end
@@ -0,0 +1,51 @@
1
+ require "gosu/spritesheet/version"
2
+ require "gosu/spritesheet/animation_block"
3
+
4
+ module Gosu
5
+ class MissingAnimationKeys < StandardError; end
6
+ class UnknownAnimationKeys < StandardError; end
7
+
8
+ class Spritesheet
9
+ def initialize(tiles: [], animations: nil, duration: 0.2)
10
+ @tiles = tiles
11
+ @default_duration = duration
12
+ setup_animations(animations)
13
+ end
14
+
15
+ def animation(anim_key)
16
+ @animations[anim_key.to_sym]
17
+ end
18
+
19
+ private
20
+ def setup_animations(animations)
21
+ @animations = Hash.new
22
+
23
+ if animations.nil?
24
+ @animations[:default] = Gosu::AnimationBlock.new(
25
+ @tiles,
26
+ @default_duration
27
+ )
28
+ else
29
+ animations.each do |key, value|
30
+ @animations[key] = animation_from_spec(value)
31
+ end
32
+ end
33
+ end
34
+
35
+ def animation_from_spec(spec)
36
+ frames_range = spec.fetch(:range, nil)
37
+ duration = spec.fetch(:duration, nil)
38
+
39
+ missing_keys = []
40
+ missing_keys << ":range" if frames_range.nil?
41
+ missing_keys << ":duration" if duration.nil?
42
+
43
+ if not missing_keys.empty?
44
+ message = "Missing #{missing_keys.join(' and ')} on block: #{spec}"
45
+ raise MissingAnimationKeys, message
46
+ end
47
+
48
+ Gosu::AnimationBlock.new(@tiles[frames_range.first], duration)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,25 @@
1
+ require 'gosu'
2
+
3
+ module Gosu
4
+ class AnimationBlock
5
+ attr_reader :frames, :duration, :duration_in_millisecs
6
+
7
+ def initialize(frames, duration)
8
+ @frames = frames
9
+ @duration = duration
10
+ @duration_in_millisecs = @duration * 1000
11
+ end
12
+
13
+ ##
14
+ # Calculates the next animation frame.
15
+ #
16
+ def step
17
+ @frames[Gosu::milliseconds / @duration_in_millisecs % @frames.size]
18
+ end
19
+
20
+ def stop
21
+ # Just gets back to the first frame.
22
+ @frames[0]
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ module Gosu
2
+ class Spritesheet
3
+ def self.version
4
+ "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ require "test_helper"
2
+ require "gosu"
3
+
4
+ describe Gosu::AnimationBlock do
5
+ let(:dude_tiles) {
6
+ path = File.expand_path('../resources/dude.png', __FILE__)
7
+ Gosu::Image.load_tiles path, 32, 48
8
+ }
9
+
10
+ before do
11
+ @animation = Gosu::AnimationBlock.new(dude_tiles, 0.2)
12
+ end
13
+
14
+ after do
15
+ @animation = nil
16
+ end
17
+
18
+ it "calculates the next animation frame" do
19
+ # When
20
+ next_frame = @animation.step
21
+
22
+ # Then
23
+ next_frame.must_equal dude_tiles[
24
+ Gosu::milliseconds / @animation.duration_in_millisecs % dude_tiles.size
25
+ ]
26
+ end
27
+ end
@@ -0,0 +1,117 @@
1
+ require "test_helper"
2
+ require "gosu"
3
+
4
+ describe Gosu::Spritesheet do
5
+ let(:dude_tiles) {
6
+ path = File.expand_path('../resources/dude.png', __FILE__)
7
+ Gosu::Image.load_tiles path, 32, 48
8
+ }
9
+
10
+ describe "when initialized from a tile set" do
11
+ describe "and a custom animation block is provided" do
12
+ let (:spritesheet) {
13
+ Gosu::Spritesheet.new({
14
+ :tiles => dude_tiles,
15
+ :animations => {
16
+ :my_custom_animation => { range: [0..3], duration: 0.3 }
17
+ }
18
+ })
19
+ }
20
+
21
+ it "must be able to fetch the custom animation" do
22
+ spritesheet.animation(:my_custom_animation).wont_be_nil
23
+ spritesheet.animation(:my_custom_animation).frames.must_equal dude_tiles[0..3]
24
+ spritesheet.animation(:my_custom_animation).duration.must_equal 0.3
25
+ end
26
+ end
27
+
28
+ describe "and no custom animation blocks are provided" do
29
+ let(:spritesheet) { Gosu::Spritesheet.new(:tiles => dude_tiles) }
30
+
31
+ it "has a default animation block that animates all tiles in sequence" do
32
+ spritesheet.animation(:default).wont_be_nil
33
+ spritesheet.animation(:default).frames.must_equal dude_tiles
34
+ spritesheet.animation(:default).duration.must_equal 0.2
35
+ end
36
+ end
37
+
38
+ describe "and a duration is provided for the default animation" do
39
+ let(:spritesheet) {
40
+ Gosu::Spritesheet.new({
41
+ :tiles => dude_tiles,
42
+ :duration => 1.0
43
+ })
44
+ }
45
+
46
+ it "must specify the default animation duration" do
47
+ spritesheet.animation(:default).duration.must_equal 1.0
48
+ end
49
+ end
50
+
51
+ describe "and a animation block called default is provided" do
52
+ let(:spritesheet) {
53
+ Gosu::Spritesheet.new({
54
+ :tiles => dude_tiles,
55
+ :animations => {
56
+ :default => { range: [0..3], duration: 0.3 }
57
+ }
58
+ })
59
+ }
60
+
61
+ # Default animation options set during `default`'s initialization
62
+ let(:default_frames) { dude_tiles }
63
+ let(:default_duration) { 0.2 }
64
+
65
+ it "wont override the new block called `default`" do
66
+ spritesheet.animation(:default).frames.wont_equal default_frames
67
+ spritesheet.animation(:default).duration.wont_equal default_duration
68
+ end
69
+ end
70
+
71
+ describe "and a custom animation block is in a completely wrong format" do
72
+ let(:spritesheet) {
73
+ Gosu::Spritesheet.new({
74
+ :tiles => dude_tiles,
75
+ :animations => {
76
+ :my_custom_animation => { frames: [0..3], anim_duration: 0.2 }
77
+ }
78
+ })
79
+ }
80
+
81
+ it "must raise that both keys are missing" do
82
+ error = proc { spritesheet }.must_raise Gosu::MissingAnimationKeys
83
+ error.message.must_match(/Missing :range and :duration on block:/)
84
+ end
85
+ end
86
+
87
+ describe "and a custom animation block is missing one key" do
88
+ let(:missing_range) {
89
+ Gosu::Spritesheet.new({
90
+ :tiles => dude_tiles,
91
+ :animations => {
92
+ :my_custom_animation => { duration: 0.2 }
93
+ }
94
+ })
95
+ }
96
+
97
+ let(:missing_duration) {
98
+ Gosu::Spritesheet.new({
99
+ :tiles => dude_tiles,
100
+ :animations => {
101
+ :my_custom_animation => { range: [0..3] }
102
+ }
103
+ })
104
+ }
105
+
106
+ it "must say that range key is missing" do
107
+ error = proc { missing_range }.must_raise Gosu::MissingAnimationKeys
108
+ error.message.must_match(/Missing :range on block:/)
109
+ end
110
+
111
+ it "must say that duration key is missing" do
112
+ error = proc { missing_duration }.must_raise Gosu::MissingAnimationKeys
113
+ error.message.must_match(/Missing :duration on block:/)
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
+ require "gosu/spritesheet"
3
+
4
+ require "minitest/autorun"
5
+ require "minitest/reporters"
6
+
7
+ Minitest::Reporters.use! [Minitest::Reporters::ProgressReporter.new(:color => true)]
8
+
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gosu-spritesheet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - pogist
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-02-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gosu
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.14'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest-reporters
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ description: A useful and yet simple spritesheet extension for Gosu.
98
+ email:
99
+ - murilo.paixao.2@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - LICENSE.md
105
+ - README.md
106
+ - Rakefile
107
+ - gosu-spritesheet.gemspec
108
+ - lib/gosu/spritesheet.rb
109
+ - lib/gosu/spritesheet/animation_block.rb
110
+ - lib/gosu/spritesheet/version.rb
111
+ - test/gosu/animation_block_test.rb
112
+ - test/gosu/spritesheet_test.rb
113
+ - test/test_helper.rb
114
+ homepage: https://github.com/pogist/gosu-spritesheet
115
+ licenses:
116
+ - MIT
117
+ metadata:
118
+ homepage_uri: https://github.com/pogist/gosu-spritesheet
119
+ source_code_uri: https://github.com/pogist/gosu-spritesheet
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubygems_version: 3.0.1
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Useful spritesheet extension for Gosu.
139
+ test_files:
140
+ - test/gosu/spritesheet_test.rb
141
+ - test/gosu/animation_block_test.rb
142
+ - test/test_helper.rb