okami 0.0.993 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/okami.rb CHANGED
@@ -1,8 +1,18 @@
1
1
  require 'gosu'
2
- require_relative 'okami/window'
3
- require_relative 'okami/keyboard'
4
- require_relative 'okami/mouse'
5
- require_relative 'okami/mouse_trap'
6
- require_relative 'okami/hit_mask'
7
- require_relative 'okami/image'
8
- require_relative 'okami/sprite'
2
+
3
+ module Okami
4
+ require_relative 'okami/window'
5
+ require_relative 'okami/keyboard'
6
+ require_relative 'okami/mouse'
7
+ require_relative 'okami/mouse_trap'
8
+ require_relative 'okami/hit_mask'
9
+ require_relative 'okami/image'
10
+ require_relative 'okami/image_tiles_cache'
11
+ require_relative 'okami/image_cache'
12
+ require_relative 'okami/sprite'
13
+
14
+ def self.retrofy
15
+ Gosu::enable_undocumented_retrofication \
16
+ rescue puts "Unable to use Gosu::enable_undocumented_retrofication"
17
+ end
18
+ end
@@ -0,0 +1,73 @@
1
+ require_relative 'wrapping_angle'
2
+
3
+ module Okami::Drawable
4
+ @default_center_x = 0
5
+ @default_center_y = 0
6
+
7
+ class << self
8
+ attr_accessor :default_center_x, :default_center_y
9
+ end
10
+
11
+ attr_accessor :x, :y, :z
12
+ attr_accessor :center_x, :center_y
13
+ attr_accessor :color, :draw_mode
14
+
15
+ def init_drawable
16
+ @factor_x = 1
17
+ @factor_y = 1
18
+ self.center_x = Okami::Drawable.default_center_x
19
+ self.center_y = Okami::Drawable.default_center_y
20
+ @x = 0
21
+ @y = 0
22
+ @z = 0
23
+ @wrapping_angle = WrappingAngle.new
24
+ @wrapping_angle.degrees = 0
25
+ @degrees = @wrapping_angle.degrees
26
+ @color = 0xFFFFFFFF
27
+ @draw_mode = :default
28
+ @color = Gosu::Color.argb(255, 255, 255, 255)
29
+ @alpha = 1.0
30
+ end
31
+
32
+ def draw
33
+ @image.draw_rot @x, @y, @z, @degrees, @center_x, @center_y, @factor_x, @factor_y, @color, @draw_mode
34
+ end
35
+
36
+ def rotate_radians a; @wrapping_angle.rotate_radians a; @degrees = @wrapping_angle.degrees end
37
+ def rotate_fraction a; @wrapping_angle.rotate_fraction a; @degrees = @wrapping_angle.degrees end
38
+ def rotate_degrees a; @wrapping_angle.rotate_degrees a; @degrees = @wrapping_angle.degrees end
39
+
40
+ def radians; @wrapping_angle.radians end
41
+ def fraction; @wrapping_angle.fraction end
42
+ def degrees; @wrapping_angle.degrees end
43
+
44
+ def radians= a; @wrapping_angle.radians = a; @degrees = @wrapping_angle.degrees end
45
+ def fraction= a; @wrapping_angle.fraction = a; @degrees = @wrapping_angle.degrees end
46
+ def degrees= a; @wrapping_angle.degrees = a; @degrees = @wrapping_angle.degrees end
47
+
48
+ attr_reader :alpha
49
+ def alpha=a
50
+ @alpha = a
51
+ @color.alpha = a*255
52
+ end
53
+
54
+ def argb= a, r, g, b
55
+ @color.alpha = a * 255
56
+ @color.red = r * 255
57
+ @color.green = g * 255
58
+ @color.blue = b * 255
59
+ end
60
+
61
+ def rgba= r, g, b, a
62
+ @color.red = r * 255
63
+ @color.green = g * 255
64
+ @color.blue = b * 255
65
+ @color.alpha = a * 255
66
+ end
67
+
68
+ def rgb=
69
+ @color.red = r * 255
70
+ @color.green = g * 255
71
+ @color.blue = b * 255
72
+ end
73
+ end
data/lib/okami/image.rb CHANGED
@@ -1,11 +1,17 @@
1
- class Okami::Image < Gosu::Image
1
+ require_relative 'drawable'
2
+
3
+ class Okami::Image
4
+ include Okami::Drawable
5
+
6
+ def initialize image_path
7
+ @image = Okami::ImageCache[image_path]
8
+ init_drawable
9
+ end
10
+
2
11
  @tileable = true
3
12
  @load_path = ""
4
13
 
5
14
  class << self
6
- Images = {}
7
- ImageTiles = {}
8
-
9
15
  # Set this for a default value for wether the images should be tileable or not
10
16
  attr_accessor :tileable
11
17
  # The directory to load the images from
@@ -15,42 +21,5 @@ class Okami::Image < Gosu::Image
15
21
  val << "/" unless val[-1] == "/"
16
22
  @load_path = val
17
23
  end
18
-
19
- ## Doesn't have anything to do with the caching, is the same as Gosu::Image.new
20
- def new path, tileable=@tileable, *src_rect
21
- super $window, @load_path + path, tileable, *src_rect
22
- end
23
- alias load new
24
-
25
- ## Doesn't have anything to do with the caching, is the same as Gosu::Image.load_tiles
26
- def load_tiles path, tile_width, tile_height, tileable=@tileable
27
- super $window, @load_path + path, tile_width, tile_height, tileable
28
- end
29
-
30
- ## Will cache the images so they're only loaded once.
31
- def require path, tileable=@tileable, *src_rect
32
- Images[ @load_path + path + src_rect.to_s ] ||= load path, tileable, *src_rect
33
- end
34
- alias [] require
35
-
36
- def require_tiles path, tile_width, tile_height, tileable=@tileable
37
- ImageTiles[ "size:#{tile_width},#{tile_height}&" + @load_path + path ] ||= load_tiles path, tile_width, tile_height, tileable
38
- end
39
-
40
- ## Will update the cache
41
- def reload path, tileable=@tileable, *src_rect
42
- Images[ @load_path + path + src_rect.to_s ] = load path, tileable, *src_rect
43
- end
44
- alias [] require
45
-
46
- def reload_tiles path, tile_width, tile_height, tileable=@tileable
47
- ImageTiles[ "size:#{tile_width},#{tile_height}&" + @load_path + path ] = load_tiles path, tile_width, tile_height, tileable
48
- end
49
-
50
- def retrofy
51
- Gosu::enable_undocumented_retrofication \
52
- rescue puts "Unable to use Gosu::enable_undocumented_retrofication"
53
- end
54
-
55
24
  end
56
- end
25
+ end
@@ -0,0 +1,18 @@
1
+ Okami::Images = {}
2
+
3
+ class Okami::ImageCache
4
+ class << self
5
+ ## Will cache the images so they're only loaded once.
6
+ def require path, tileable=Okami::Image.tileable, *src_rect
7
+ path = Okami::Image.load_path + path
8
+ Okami::Images[ path + src_rect.to_s ] ||= Gosu::Image.new $window, path, tileable, *src_rect
9
+ end
10
+ alias [] require
11
+
12
+ ## Will load and cache even if it's already cached
13
+ def load path, tileable=Okami::Image.tileable, *src_rect
14
+ path = Okami::Image.load_path + path
15
+ Okami::Images[ path + src_rect.to_s ] = Gosu::Image.new $window, path, tileable, *src_rect
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ Okami::ImageTiles = {}
2
+
3
+ class Okami::ImageTilesCache
4
+ class << self
5
+ def require path, tile_width, tile_height, tileable=Okami::Image.tileable
6
+ path = Okami::Image.load_path + path
7
+ Okami::ImageTiles[ "size:#{tile_width},#{tile_height}&" + path ] ||= Gosu::Image.load_tiles $window, path, tile_width, tile_height, tileable
8
+ end
9
+ alias [] require
10
+
11
+ ## Will load and cache even if it's already cached
12
+ def load path, tile_width, tile_height, tileable=Okami::Image.tileable
13
+ path = Okami::Image.load_path + path
14
+ Okami::ImageTiles[ "size:#{tile_width},#{tile_height}&" + path ] = Gosu::Image.load_tiles $window, path, tile_width, tile_height, tileable
15
+ end
16
+ end
17
+ end
@@ -71,7 +71,7 @@ module Okami::Keyboard
71
71
  Gosu::KbZ => :z
72
72
  }
73
73
 
74
- if OS.mac?
74
+ if Okami::OS.mac?
75
75
  DefaultKeySymbols[55] = :left_cmd
76
76
  DefaultKeySymbols[54] = :right_cmd
77
77
  end
data/lib/okami/os.rb CHANGED
@@ -1,4 +1,4 @@
1
- module OS
1
+ module Okami::OS
2
2
  @@system =
3
3
  case RUBY_PLATFORM
4
4
  when /darwin/i then :mac
data/lib/okami/sprite.rb CHANGED
@@ -1,39 +1,31 @@
1
+ require_relative 'drawable'
2
+
1
3
  class Okami::Sprite
2
- Modes = [:forward, :loop, :backward, :backward_loop, :ping_pong]
4
+ include Okami::Drawable
5
+ AnimationModes = [:forward, :loop, :backward, :backward_loop, :ping_pong]
3
6
 
4
- attr_reader :current_image, :images, :last_frame_index
7
+ attr_reader :image, :images, :last_frame_index
5
8
 
6
9
  def initialize options
7
10
  raise_possible_errors options
11
+ options[:animation_mode] ||= :loop
8
12
 
9
- @images = options[:images]
10
-
11
- @last_frame_index = @images.length - 1
12
- @add = ( options[:fps] || 0 ) / 60.0
13
- @direction = options[:direction]
14
- self.mode = options[:mode] || :loop
15
-
16
- case @direction
17
- when :forward then @frame_index = 0
18
- when :backward then @frame_index = @last_frame_index+0.999
13
+ options.each do |key, value|
14
+ send "#{key}=", value
19
15
  end
20
16
 
17
+ set_initial_frame_index
18
+ init_drawable
21
19
  update_current_image
22
20
  end
23
21
 
24
- def fps; @add * 60.0 end
25
- def fps= fps; @add = fps / 60.0 end
26
-
27
- ## Draw it just like a Gosu::Image !
28
- def draw *args; @current_image.draw *args end
29
- def draw_as_quad *args; @current_image.draw_as_quad *args end
30
- def draw_rot *args; @current_image.draw_rot *args end
31
-
22
+ def fps; @delta_frame_index * 60.0 end
23
+ def fps=fps; @delta_frame_index = fps / 60.0 end
32
24
 
33
25
  def update
34
26
  case @direction
35
- when :forward then @frame_index += @add*$window.dt*60.0
36
- when :backward then @frame_index -= @add*$window.dt*60.0
27
+ when :forward then @frame_index += @delta_frame_index*$window.dt*60.0
28
+ when :backward then @frame_index -= @delta_frame_index*$window.dt*60.0
37
29
  end
38
30
 
39
31
  @update_method.call
@@ -42,15 +34,15 @@ class Okami::Sprite
42
34
 
43
35
 
44
36
  def finished?
45
- case @mode
37
+ case @animation_mode
46
38
  when :forward; @frame_index == @last_frame_index
47
39
  when :backward; @frame_index == 0
48
40
  end
49
41
  end
50
42
  alias animation_finished? finished?
51
43
 
52
- attr_reader :mode
53
- def mode= symbol
44
+ attr_reader :animation_mode
45
+ def animation_mode= symbol
54
46
  case symbol
55
47
  when :loop
56
48
  @direction = :forward
@@ -73,12 +65,12 @@ class Okami::Sprite
73
65
  use_update method :update_ping_pong
74
66
 
75
67
  else
76
- puts "Supported AnimatedSprite modes:"
77
- puts self.class.modes
78
- raise ArgumentError, "AnimatedSprite, mode #{symbol.inspect} not supported"
68
+ puts "Supported AnimatedSprite animation modes:"
69
+ puts AnimationModes
70
+ raise ArgumentError, "AnimatedSprite, animation_mode #{symbol.inspect} not supported"
79
71
  end
80
72
 
81
- @mode = symbol
73
+ @animation_mode = symbol
82
74
  end
83
75
 
84
76
  attr_reader :frame_index
@@ -93,14 +85,28 @@ class Okami::Sprite
93
85
  (rand()*@images.length)
94
86
  end
95
87
 
88
+ protected
96
89
 
90
+ def images=images
91
+ @images = images
92
+ @last_frame_index = @images.length - 1
93
+ end
97
94
 
98
- private
95
+ def direction=direction
96
+ @direction = direction
97
+ end
99
98
 
99
+ private
100
100
 
101
+ def set_initial_frame_index
102
+ case @direction
103
+ when :forward then @frame_index = 0
104
+ when :backward then @frame_index = @last_frame_index+0.999
105
+ end
106
+ end
101
107
 
102
108
  def use_update method; @update_method = method end
103
- def update_current_image; @current_image = @images[@frame_index] end
109
+ def update_current_image; @image = @images[@frame_index] end
104
110
 
105
111
  def update_loop; @frame_index %= @images.length end
106
112
  def update_backward_loop; @frame_index %= @images.length end
@@ -112,20 +118,18 @@ class Okami::Sprite
112
118
  def update_forward
113
119
  @frame_index = @last_frame_index if @frame_index > @last_frame_index
114
120
  end
115
-
116
-
117
-
121
+
118
122
  def update_ping_pong
119
123
  case @direction
120
124
  when :forward
121
- if @frame_index >= @last_frame_index
125
+ if @frame_index >= @last_frame_index+0.5
122
126
  @direction = :backward
123
- @frame_index = @last_frame_index
127
+ @frame_index = @last_frame_index+0.5
124
128
  end
125
129
 
126
130
  when :backward
127
- if @frame_index < 0
128
- @frame_index = 0
131
+ if @frame_index < 0.5
132
+ @frame_index = 0.5
129
133
  @direction = :forward
130
134
  end
131
135
 
data/lib/okami/window.rb CHANGED
@@ -21,16 +21,7 @@ module Okami
21
21
  Okami::Mouse.button_up id
22
22
  end
23
23
 
24
- def color_fill color, z=0
25
- color = case color
26
- when :white then 0xFFFFFFFF
27
- when :black then 0xFF000000
28
- when Symbol
29
- raise ArgumentError, "Unknown color #{color.inspect}"
30
- else
31
- color
32
- end
33
-
24
+ def fill color, z=0
34
25
  draw_quad 0, 0, color,
35
26
  width, 0, color,
36
27
  0, height, color,
@@ -0,0 +1,44 @@
1
+ class WrappingAngle
2
+ Tau = Math::PI * 2
3
+
4
+ def initialize
5
+ @c = Complex(0,1)
6
+ end
7
+
8
+ def rotate_radians a; @c *= Complex(0,1)**(a/Tau * 4) end
9
+ def rotate_fraction a; @c *= Complex(0,1)**(a*4) end
10
+ def rotate_degrees a; @c *= Complex(0,1)**(a/90.0) end
11
+
12
+ def radians; @c.angle end
13
+ def fraction; @c.angle / Tau end
14
+ def degrees; @c.angle / Tau * 360 end
15
+
16
+ def radians= a; @c = Complex(0,1)**(a/Tau * 4) end
17
+ def fraction= a; @c = Complex(0,1)**(a*4.0) end
18
+ def degrees= a; @c = Complex(0,1)**(a/90.0) end
19
+
20
+ ### Displaying
21
+
22
+ begin
23
+ require 'fraction'
24
+ def pretty_angle unit=@standard_unit
25
+ case unit
26
+ when :degrees
27
+ "#{angle(:degrees).round}°"
28
+ when :fraction, :radians
29
+ nom, den = angle(:fraction).fraction
30
+ return "0τ" if nom == 0
31
+ return "#{nom}/#{den}τ"
32
+ end
33
+ end
34
+ rescue LoadError
35
+ def pretty_angle unit=@standard_unit
36
+ case unit
37
+ when :degrees; "#{angle(:degrees).round}°"
38
+ when :radians; "#{angle(:radians)} radians"
39
+ when :fraction; "#{angle(:fraction)}"
40
+ end
41
+ end
42
+ end
43
+
44
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: okami
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.993
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -50,14 +50,18 @@ extensions: []
50
50
  extra_rdoc_files: []
51
51
  files:
52
52
  - lib/okami.rb
53
+ - lib/okami/drawable.rb
53
54
  - lib/okami/window.rb
54
55
  - lib/okami/keyboard.rb
55
56
  - lib/okami/mouse.rb
56
57
  - lib/okami/mouse_trap.rb
57
58
  - lib/okami/image.rb
59
+ - lib/okami/image_cache.rb
60
+ - lib/okami/image_tiles_cache.rb
58
61
  - lib/okami/hit_mask.rb
59
62
  - lib/okami/sprite.rb
60
63
  - lib/okami/os.rb
64
+ - lib/okami/wrapping_angle.rb
61
65
  homepage: https://github.com/Aerotune/Okami
62
66
  licenses: []
63
67
  post_install_message: