aniruby 0.1.2
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 +7 -0
- data/.yardopts +1 -0
- data/CHANGELOG.md +28 -0
- data/LICENSE +21 -0
- data/README.md +78 -0
- data/Rakefile +33 -0
- data/lib/aniruby/animation.rb +189 -0
- data/lib/aniruby/frame.rb +16 -0
- data/lib/aniruby/frames.rb +31 -0
- data/lib/aniruby/version.rb +9 -0
- data/lib/aniruby.rb +6 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1b0e843e0f081ca0cc57e4e28ed4b37f43574d7d4b50220d608879357f322d44
|
4
|
+
data.tar.gz: e284a38f220483bd15cae1a0ed61f6615296d8b4257838266013d9214bfd327c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e3339f0a1560b4b77adf2b1dadf574d88f80d8315e71c0a1e7d886e94da3ada92968486dfdb48ef06fba66fa572f9f8f1fb95ace2669afba150c233e4cda780a
|
7
|
+
data.tar.gz: 42308936cde744c37f56af5c1fe03e244f0d13634ebf52dd194408603531108e5cf4d804b0da6432f7fe7f3b2a81c02ec27ac4036b0dc51af88c75594d458078
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
lib/**/*.rb - README.md CHANGELOG.md LICENSE
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [Unreleased]
|
9
|
+
|
10
|
+
## 0.1.2 - 2023-08-20
|
11
|
+
|
12
|
+
### Changed
|
13
|
+
|
14
|
+
- Downcased the gem name in the gemspec
|
15
|
+
|
16
|
+
## 0.1.1 - 2023-08-20
|
17
|
+
|
18
|
+
### Added
|
19
|
+
|
20
|
+
- Add .yardopts to included files
|
21
|
+
|
22
|
+
### Fixed
|
23
|
+
|
24
|
+
- Required ruby not being used correctly in gemspec
|
25
|
+
|
26
|
+
## 0.1.0 - 2023-08-20
|
27
|
+
|
28
|
+
Initial release
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023 Chadow
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
# AniRuby
|
2
|
+
|
3
|
+
Make sprite animations on Gosu simple and easy.
|
4
|
+
|
5
|
+
## Summary
|
6
|
+
|
7
|
+
This library will provide you with a nice n easy interface to do sprite animations
|
8
|
+
on [Gosu](https://www.libgosu.org/), while being as nifty as possible
|
9
|
+
|
10
|
+
The library is made in pure Ruby with no dependencies at all (except Gosu, of course)
|
11
|
+
|
12
|
+
## Install
|
13
|
+
|
14
|
+
You can install the gem with the following command:
|
15
|
+
|
16
|
+
`gem install aniruby`
|
17
|
+
|
18
|
+
or use it with a bundle:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
# Somewhere in a Gemfile
|
22
|
+
|
23
|
+
gem 'aniruby'
|
24
|
+
```
|
25
|
+
|
26
|
+
## Getting Started
|
27
|
+
|
28
|
+
Using the less code possible:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
|
32
|
+
require 'gosu'
|
33
|
+
require 'aniruby'
|
34
|
+
|
35
|
+
class MyWindow < Gosu::Window
|
36
|
+
def initialize
|
37
|
+
super(800, 600, false)
|
38
|
+
|
39
|
+
@animation = AniRuby::Animation.new('my_spritesheet.png', 32, 32,
|
40
|
+
false, true,
|
41
|
+
150)
|
42
|
+
end
|
43
|
+
|
44
|
+
def update
|
45
|
+
@animation.update
|
46
|
+
end
|
47
|
+
|
48
|
+
def draw
|
49
|
+
@animation.draw(0, 0)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
### Explanation
|
55
|
+
|
56
|
+
When you create a animation, you'll need to have an *spritesheet*, where you have
|
57
|
+
each sprite of the animation, that's the first argument to `Animation#new`, then
|
58
|
+
you'll need the dimensions of each individual sprite on your spritesheet, in the
|
59
|
+
example provided each sprite in the spritesheet is 32x32.
|
60
|
+
|
61
|
+
That is the bare minimum, you can specify filter type (retro in gosu's API), looping
|
62
|
+
and duration (for each individual frame too!).
|
63
|
+
|
64
|
+
provided you have the initilization right, you need to `update` your animation for it
|
65
|
+
to come to live!
|
66
|
+
|
67
|
+
NOTE: `Animation#draw` mimics the API of gosu' `Image#draw`, so you can give it
|
68
|
+
much more than single coordinates. there's `Animation#draw_rot` too!
|
69
|
+
|
70
|
+
## Roadmap
|
71
|
+
|
72
|
+
- more fine-grained control of animation
|
73
|
+
- being able to make an animation stitching `Gosu::Image`'s together
|
74
|
+
- mirroring
|
75
|
+
|
76
|
+
## License
|
77
|
+
|
78
|
+
[MIT](LICENSE)
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
|
2
|
+
require 'fileutils'
|
3
|
+
require "minitest/test_task"
|
4
|
+
|
5
|
+
require_relative 'lib/aniruby/version'
|
6
|
+
|
7
|
+
Minitest::TestTask.create
|
8
|
+
|
9
|
+
task :default => :build
|
10
|
+
|
11
|
+
desc 'Build the gem'
|
12
|
+
task :build do
|
13
|
+
Dir.mkdir('pkg') unless Dir.exist?('pkg')
|
14
|
+
|
15
|
+
sh 'gem build aniruby.gemspec'
|
16
|
+
|
17
|
+
FileUtils.mv("aniruby-#{AniRuby::VERSION}.gem", 'pkg')
|
18
|
+
end
|
19
|
+
|
20
|
+
desc 'Clean the build enviroment'
|
21
|
+
task :clean do
|
22
|
+
FileUtils.rm_rf('pkg')
|
23
|
+
end
|
24
|
+
|
25
|
+
desc 'Push the gem to RubyGems'
|
26
|
+
task :push do
|
27
|
+
sh "gem push pkg/aniruby-#{AniRuby::VERSION}.gem"
|
28
|
+
end
|
29
|
+
|
30
|
+
desc 'Use YARD to generate documentation'
|
31
|
+
task :doc do
|
32
|
+
sh 'yardoc'
|
33
|
+
end
|
@@ -0,0 +1,189 @@
|
|
1
|
+
|
2
|
+
module AniRuby
|
3
|
+
# Has a AniRuby::Frames colletion, with a simple counter to keep track of
|
4
|
+
# current frame plus looping and pausing functionality
|
5
|
+
class Animation
|
6
|
+
# @return [AniRuby::Frames] The collection of frames this animation uses
|
7
|
+
attr_accessor :frames
|
8
|
+
# @return [Integer] The current frame index of the animation
|
9
|
+
attr_accessor :current_frame
|
10
|
+
# @return [Boolean] The loop status of the animation
|
11
|
+
attr_accessor :loop
|
12
|
+
|
13
|
+
# Create a new animation
|
14
|
+
#
|
15
|
+
# @param spritesheet [String] Path to the spritesheet file
|
16
|
+
# @param frame_w [Integer] The width of each individual frame
|
17
|
+
# @param frame_h [Integer] The height of each individual frame
|
18
|
+
# @param retro [Boolean] If true, the animation will not be interpolated when scaled
|
19
|
+
# @param loop [Boolean] If true, the animation will loop indefinitely
|
20
|
+
# @param durations [Integer] The duration of the frames in milliseconds (500
|
21
|
+
# is half a second, 1000 a second, etc). If
|
22
|
+
# there's more than one duration, then they will
|
23
|
+
# be mapped to each frame of the animation, the
|
24
|
+
# frames with no specified duration will default to 100
|
25
|
+
#
|
26
|
+
# @return [Animation] A new animation ready to play
|
27
|
+
def initialize(spritesheet, frame_w, frame_h, retro = false, loop = true, *durations)
|
28
|
+
@frame_w = frame_w
|
29
|
+
@frame_h = frame_h
|
30
|
+
|
31
|
+
@loop = loop
|
32
|
+
|
33
|
+
@current_frame = 0
|
34
|
+
@pause = false
|
35
|
+
|
36
|
+
@frames = AniRuby::Frames.new(Gosu::Image.load_tiles(spritesheet,
|
37
|
+
@frame_w,
|
38
|
+
@frame_h,
|
39
|
+
retro: retro))
|
40
|
+
|
41
|
+
# TODO: Maybe I could shorten this, adding an extra argument to
|
42
|
+
# AniRuby::Frames
|
43
|
+
if durations.one?
|
44
|
+
@frames.each { |frame| frame.duration = durations[0]}
|
45
|
+
else
|
46
|
+
@frames.each_with_index do |frame, idx|
|
47
|
+
# Set each frame to the duration provided, if there's no provide
|
48
|
+
# duration for all frames then we'll leave it at the default
|
49
|
+
frame.duration = durations[idx] unless durations[idx].nil?
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Get the width of the current frame's image
|
55
|
+
#
|
56
|
+
# @return [Integer]
|
57
|
+
def width
|
58
|
+
@frames[@current_frame].sprite.width
|
59
|
+
end
|
60
|
+
|
61
|
+
# Get the height of the current frame's image
|
62
|
+
#
|
63
|
+
# @return [Integer]
|
64
|
+
def height
|
65
|
+
@frames[@current_frame].sprite.height
|
66
|
+
end
|
67
|
+
|
68
|
+
# Update the animation, advancing the frame counter. Note that this won't do
|
69
|
+
# do anything if the animation is paused or has finished
|
70
|
+
def update
|
71
|
+
return if done? || paused?
|
72
|
+
|
73
|
+
@current_frame += 1 if frame_expired?
|
74
|
+
end
|
75
|
+
|
76
|
+
# Draw the animation
|
77
|
+
#
|
78
|
+
# @param x [Integer] The X coordinate
|
79
|
+
# @param y [Integer] The Y coordinate
|
80
|
+
# @param z [Integer] The Z order
|
81
|
+
# @param scale_x [Float] The horizontal scale factor
|
82
|
+
# @param scale_y [Float] The vertical scale factor
|
83
|
+
# @param color [Gosu::Color] The color to usw when drawing
|
84
|
+
# @param mode [:default, :additive] The blending mode
|
85
|
+
#
|
86
|
+
# (see {draw_rot})
|
87
|
+
def draw(x, y, z = 0,
|
88
|
+
scale_x = 1,
|
89
|
+
scale_y = 1,
|
90
|
+
color = Gosu::Color::WHITE,
|
91
|
+
mode = :default)
|
92
|
+
frame = @frames[@current_frame]
|
93
|
+
|
94
|
+
frame.sprite.draw(x, y, z, scale_x, scale_y, color, mode)
|
95
|
+
|
96
|
+
@current_frame = 0 if @loop && done?
|
97
|
+
end
|
98
|
+
|
99
|
+
# Draw the animation rotated, with its rotational center at (x, y).
|
100
|
+
#
|
101
|
+
# @param x [Integer] The X coordinate
|
102
|
+
# @param y [Integer] The Y coordinate
|
103
|
+
# @param z [Integer] The Z order
|
104
|
+
# @param angle [Float] The angle. in degrees
|
105
|
+
# @param center_x [Float] the relative horizontal rotation origin
|
106
|
+
# @param center_y [Float] the relative vertical rotation origin
|
107
|
+
# @param scale_x [Float] The horizontal scale factor
|
108
|
+
# @param scale_y [Float] The vertical scale factor
|
109
|
+
# @param color [Gosu::Color] The color to usw when drawing
|
110
|
+
# @param mode [:default, :additive] The blending mode
|
111
|
+
#
|
112
|
+
# (see {draw})
|
113
|
+
def draw_rot(x, y, z = 0,
|
114
|
+
angle = 0,
|
115
|
+
center_x = 0.5,
|
116
|
+
center_y = 0.5,
|
117
|
+
scale_x = 1,
|
118
|
+
scale_y = 1,
|
119
|
+
color = Gosu::Color::WHITE,
|
120
|
+
mode = :default)
|
121
|
+
frame = @frames[@current_frame]
|
122
|
+
|
123
|
+
frame.sprite.draw_rot(x, y, z, angle, center_x, center_y, scale_x, scale_y, color, mode)
|
124
|
+
|
125
|
+
# Loop the animation
|
126
|
+
@current_frame = 0 if @loop && done?
|
127
|
+
end
|
128
|
+
|
129
|
+
# Pause the animation
|
130
|
+
#
|
131
|
+
# (see {resume})
|
132
|
+
def pause
|
133
|
+
@pause = true
|
134
|
+
end
|
135
|
+
|
136
|
+
# Resume the animation
|
137
|
+
#
|
138
|
+
# (see {pause})
|
139
|
+
def resume
|
140
|
+
@pause = false
|
141
|
+
end
|
142
|
+
|
143
|
+
# Set the animation to the beginning frame
|
144
|
+
def reset
|
145
|
+
@current_frame = 0
|
146
|
+
end
|
147
|
+
|
148
|
+
# Set the duration for all frames in the animation
|
149
|
+
#
|
150
|
+
# @param ms [Integer] The new duration in milliseconds
|
151
|
+
def duration(ms)
|
152
|
+
@frames.each { |frame| frame.duration = ms}
|
153
|
+
end
|
154
|
+
|
155
|
+
# Is the animation finished?
|
156
|
+
#
|
157
|
+
# @return [Boolean]
|
158
|
+
# @note This method will return true in intervals if the animation loops
|
159
|
+
def done?
|
160
|
+
true if @current_frame == @frames.size - 1
|
161
|
+
end
|
162
|
+
|
163
|
+
# Is the animation paused?
|
164
|
+
#
|
165
|
+
# @return [Boolean]
|
166
|
+
def paused?
|
167
|
+
return true if @pause
|
168
|
+
|
169
|
+
false
|
170
|
+
end
|
171
|
+
|
172
|
+
# Get the current frame
|
173
|
+
#
|
174
|
+
# @return [AniRuby::Frame]
|
175
|
+
def get_current_frame
|
176
|
+
@frames[@current_frame % @frames.size]
|
177
|
+
end
|
178
|
+
|
179
|
+
# Has the current frame's duration expired?
|
180
|
+
def frame_expired?
|
181
|
+
now = Gosu.milliseconds
|
182
|
+
@last_frame ||= now
|
183
|
+
|
184
|
+
if (now - @last_frame) > @frames[@current_frame].duration
|
185
|
+
@last_frame = now
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
|
2
|
+
module AniRuby
|
3
|
+
# A frame has a sprite and a duration in milliseconds
|
4
|
+
class Frame
|
5
|
+
attr_accessor :duration, :sprite
|
6
|
+
attr_reader :w, :h
|
7
|
+
|
8
|
+
def initialize(sprite, duration = 100)
|
9
|
+
@sprite = sprite
|
10
|
+
@duration = duration
|
11
|
+
|
12
|
+
@w = @sprite.width
|
13
|
+
@h = @sprite.height
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
|
2
|
+
module AniRuby
|
3
|
+
# Collection of frames, with Enumerable included to add iteration functions
|
4
|
+
class Frames
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
# Create a new collection of frames
|
8
|
+
#
|
9
|
+
# @param sprites [Array<Gosu::Image>]
|
10
|
+
def initialize(sprites)
|
11
|
+
@frames = sprites.map do |sprite|
|
12
|
+
AniRuby::Frame.new(sprite)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# @param index [Integer]
|
17
|
+
def [](index)
|
18
|
+
@frames[index]
|
19
|
+
end
|
20
|
+
|
21
|
+
def each(&block)
|
22
|
+
@frames.each do |frame|
|
23
|
+
if block_given?
|
24
|
+
block.call(frame)
|
25
|
+
else
|
26
|
+
yield frame
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/aniruby.rb
ADDED
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aniruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chadow
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-08-20 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: 1.4.6
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.4.6
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 5.17.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 5.17.0
|
41
|
+
description: |
|
42
|
+
Library for painless sprite animations on Gosu, with a easy
|
43
|
+
and nifty API, made in pure Ruby with no dependencies at all!
|
44
|
+
email:
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".yardopts"
|
50
|
+
- CHANGELOG.md
|
51
|
+
- LICENSE
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/aniruby.rb
|
55
|
+
- lib/aniruby/animation.rb
|
56
|
+
- lib/aniruby/frame.rb
|
57
|
+
- lib/aniruby/frames.rb
|
58
|
+
- lib/aniruby/version.rb
|
59
|
+
homepage: https://github.com/Chadowo/aniruby
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: 2.5.0
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubygems_version: 3.4.19
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Make sprite animations on Gosu simple and easy
|
82
|
+
test_files: []
|