okami 0.0.985 → 0.0.990

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,8 @@
1
1
  require 'gosu'
2
- require 'okami/window'
3
- require 'okami/keyboard'
4
- require 'okami/mouse'
5
- require 'okami/mouse_trap'
6
- require 'okami/hit_mask'
7
- require 'okami/image'
8
- require 'okami/sprite'
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'
@@ -1,7 +1,7 @@
1
1
  class Okami::HitMask
2
- ## The mask is of the type Image, white is hitpoints, black is not
3
- def initialize mask, parent
4
- @hit_mask = mask
2
+ ## white is hitpoints, black is not
3
+ def initialize image, parent
4
+ @hit_mask = image
5
5
  @parent = parent
6
6
  end
7
7
 
data/lib/okami/image.rb CHANGED
@@ -10,17 +10,24 @@ class Okami::Image < Gosu::Image
10
10
  attr_accessor :tileable
11
11
  # The directory to load the images from
12
12
  attr_accessor :load_path
13
-
13
+
14
+ def load_path=val
15
+ val << "/" unless val[-1] == "/"
16
+ @load_path = val
17
+ end
18
+
19
+ ## Doesn't have anything to do with the caching, is the same as Gosu::Image.new
14
20
  def new path, tileable=@tileable, *src_rect
15
21
  super $window, @load_path + path, tileable, *src_rect
16
22
  end
17
23
  alias load new
18
-
24
+
25
+ ## Doesn't have anything to do with the caching, is the same as Gosu::Image.load_tiles
19
26
  def load_tiles path, tile_width, tile_height, tileable=@tileable
20
27
  super $window, @load_path + path, tile_width, tile_height, tileable
21
28
  end
22
29
 
23
- # require / [] will cache the images so they're only loaded once.
30
+ ## Will cache the images so they're only loaded once.
24
31
  def require path, tileable=@tileable, *src_rect
25
32
  Images[ @load_path + path + src_rect.to_s ] ||= load path, tileable, *src_rect
26
33
  end
@@ -30,6 +37,16 @@ class Okami::Image < Gosu::Image
30
37
  ImageTiles[ "size:#{tile_width},#{tile_height}&" + @load_path + path ] ||= load_tiles path, tile_width, tile_height, tileable
31
38
  end
32
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
+
33
50
  def retrofy
34
51
  Gosu::enable_undocumented_retrofication \
35
52
  rescue puts "Unable to use Gosu::enable_undocumented_retrofication"
@@ -1,4 +1,4 @@
1
- require 'okami/os'
1
+ require_relative 'os'
2
2
 
3
3
  module Okami::Keyboard
4
4
  DefaultKeySymbols = {
@@ -95,18 +95,16 @@ module Okami::Keyboard
95
95
  end
96
96
 
97
97
  def remove_key_up_listener listener
98
- if listener.class == Method
99
- @@key_up_listeners.delete @@key_up_listeners.key(listener)
100
- else
101
- @@key_up_listeners.delete listener
98
+ case listener
99
+ when Method; @@key_up_listeners.delete @@key_up_listeners.key(listener)
100
+ else; @@key_up_listeners.delete listener
102
101
  end
103
102
  end
104
103
 
105
104
  def remove_key_down_listener listener
106
- if listener.class == Method
107
- @@key_down_listeners.delete @@key_down_listeners.key(listener)
108
- else
109
- @@key_down_listeners.delete listener
105
+ case listener
106
+ when Method; @@key_down_listeners.delete @@key_down_listeners.key(listener)
107
+ else; @@key_down_listeners.delete listener
110
108
  end
111
109
  end
112
110
 
@@ -120,7 +118,6 @@ module Okami::Keyboard
120
118
  else
121
119
  $window.button_down? @@key_symbols.key( key_symbol )
122
120
  end
123
-
124
121
  end
125
122
 
126
123
  ## Returns true if all key_symbol arguments is down
@@ -143,11 +140,35 @@ module Okami::Keyboard
143
140
 
144
141
  def button_down id
145
142
  key = @@key_symbols[id]
146
- @@key_down_listeners.each { |listener, method| method.call key } if key
143
+ case key
144
+ when :left_alt, :right_alt then call_key_down_listeners :alt
145
+ when :left_shift, :right_shift then call_key_down_listeners :shift
146
+ when :left_ctrl, :right_ctrl then call_key_down_listeners :ctrl
147
+ when :left_cmd, :right_cmd then call_key_down_listeners :cmd
148
+ end
149
+ call_key_down_listeners key
147
150
  end
148
151
 
149
152
  def button_up id
150
153
  key = @@key_symbols[id]
154
+ case key
155
+ when :left_alt, :right_alt; call_key_up_listeners :alt
156
+ when :left_shift, :right_shift; call_key_up_listeners :shift
157
+ when :left_ctrl, :right_ctrl; call_key_up_listeners :ctrl
158
+ when :left_cmd, :right_cmd; call_key_up_listeners :cmd
159
+ end
160
+ call_key_up_listeners key
161
+ end
162
+
163
+
164
+ protected
165
+
166
+
167
+ def call_key_down_listeners key
168
+ @@key_down_listeners.each { |listener, method| method.call key } if key
169
+ end
170
+
171
+ def call_key_up_listeners key
151
172
  @@key_up_listeners.each { |listener, method| method.call key } if key
152
173
  end
153
174
  end
data/lib/okami/mouse.rb CHANGED
@@ -21,10 +21,10 @@ module Okami::Mouse
21
21
  def y=value; $window.mouse_y = value end
22
22
 
23
23
  def onscreen?
24
- if x > 0
25
- if y > 0
26
- if x < $window.width
27
- if y < $window.height
24
+ if x < 0
25
+ if y < 0
26
+ if x > $window.width
27
+ if y > $window.height
28
28
  return true
29
29
  end
30
30
  end
@@ -50,18 +50,16 @@ module Okami::Mouse
50
50
  end
51
51
 
52
52
  def remove_key_up_listener listener
53
- if listener.class == Method
54
- @@key_up_listeners.delete @@key_up_listeners.key(listener)
55
- else
56
- @@key_up_listeners.delete listener
53
+ case listener
54
+ when Method; @@key_up_listeners.delete @@key_up_listeners.key(listener)
55
+ else; @@key_up_listeners.delete listener
57
56
  end
58
57
  end
59
58
 
60
59
  def remove_key_down_listener listener
61
- if listener.class == Method
62
- @@key_down_listeners.delete @@key_down_listeners.key(listener)
63
- else
64
- @@key_down_listeners.delete listener
60
+ case listener
61
+ when Method; @@key_down_listeners.delete @@key_down_listeners.key(listener)
62
+ else; @@key_down_listeners.delete listener
65
63
  end
66
64
  end
67
65
 
@@ -94,14 +92,12 @@ module Okami::Mouse
94
92
 
95
93
  def button_down id
96
94
  key = @@key_symbols[id]
97
- return unless key
98
- @@key_down_listeners.each { |listener, method| method.call key }
95
+ @@key_down_listeners.each { |listener, method| method.call key } if key
99
96
  end
100
97
 
101
98
  def button_up id
102
99
  key = @@key_symbols[id]
103
- return unless key
104
- @@key_up_listeners.each { |listener, method| method.call key }
100
+ @@key_up_listeners.each { |listener, method| method.call key } if key
105
101
  end
106
102
  end
107
103
 
data/lib/okami/sprite.rb CHANGED
@@ -3,16 +3,19 @@ class Okami::Sprite
3
3
 
4
4
  attr_reader :current_image, :images, :last_frame_index
5
5
 
6
- def initialize options={}
6
+ def initialize options
7
+ raise_possible_errors options
8
+
7
9
  @images = options[:images]
8
10
 
9
11
  @last_frame_index = @images.length - 1
10
- @add = ( options[:fps] || 0 ) / 60.0
11
- self.mode = options[:mode] || :loop
12
+ @add = ( options[:fps] || 0 ) / 60.0
13
+ @direction = options[:direction]
14
+ self.mode = options[:mode] || :loop
12
15
 
13
16
  case @direction
14
17
  when :forward then @frame_index = 0
15
- when :backward then @frame_index = @last_frame_index+0.99
18
+ when :backward then @frame_index = @last_frame_index+0.999
16
19
  end
17
20
 
18
21
  update_current_image
@@ -21,19 +24,6 @@ class Okami::Sprite
21
24
  def fps; @add * 60.0 end
22
25
  def fps= fps; @add = fps / 60.0 end
23
26
 
24
- attr_reader :frame_index
25
- def frame_index= i
26
- @frame_index = i
27
- @frame_index = @last_frame_index if @frame_index.abs > @last_frame_index
28
- @frame_index = 0 if @frame_index < 0
29
- update_current_image
30
- end
31
-
32
- def random_frame_index
33
- (rand()*@images.length) % @images.length
34
- end
35
-
36
-
37
27
  ## Draw it just like a Gosu::Image !
38
28
  def draw *args; @current_image.draw *args end
39
29
  def draw_as_quad *args; @current_image.draw_as_quad *args end
@@ -91,33 +81,39 @@ class Okami::Sprite
91
81
  @mode = symbol
92
82
  end
93
83
 
84
+ attr_reader :frame_index
85
+ def frame_index= i
86
+ @frame_index = i
87
+ @frame_index = @last_frame_index if @frame_index.abs > @last_frame_index
88
+ @frame_index = 0 if @frame_index < 0
89
+ update_current_image
90
+ end
91
+
92
+ def random_frame_index
93
+ (rand()*@images.length)
94
+ end
95
+
94
96
 
95
97
 
96
- protected
98
+ private
97
99
 
98
100
 
99
101
 
100
102
  def use_update method; @update_method = method end
103
+ def update_current_image; @current_image = @images[@frame_index] end
104
+
105
+ def update_loop; @frame_index %= @images.length end
106
+ def update_backward_loop; @frame_index %= @images.length end
101
107
 
102
- def update_current_image
103
- @current_image = @images[ @frame_index ]
108
+ def update_backward
109
+ @frame_index = 0 if @frame_index < 0
104
110
  end
105
111
 
106
112
  def update_forward
107
113
  @frame_index = @last_frame_index if @frame_index > @last_frame_index
108
114
  end
109
115
 
110
- def update_loop
111
- @frame_index %= @images.length# if @frame_index.abs > @last_frame_index
112
- end
113
-
114
- def update_backward
115
- @frame_index = 0 if @frame_index < 0
116
- end
117
116
 
118
- def update_backward_loop
119
- @frame_index = @last_frame_index+0.99 if @frame_index < 0
120
- end
121
117
 
122
118
  def update_ping_pong
123
119
  case @direction
@@ -129,26 +125,19 @@ class Okami::Sprite
129
125
 
130
126
  when :backward
131
127
  if @frame_index < 0
132
- @frame_index = 1
128
+ @frame_index = 0
133
129
  @direction = :forward
134
130
  end
135
131
 
136
132
  end
137
133
  end
138
- end # Okami::Sprite
139
-
140
- =begin
141
- ## Example ##
142
- a = AnimatedSprite.new images: [1, 2, 3, 4, 5, 6, 7, 8], fps: 60, :mode => :forward
143
- loop do
144
- #if rand(10) == 1
145
- # modes = AnimatedSprite.modes
146
- # a.mode = modes[rand(modes.length)]
147
- # puts a.mode
148
- #end
149
- p a.current_image
150
- p a.finished?
151
- a.update
152
- sleep 0.3
153
- end
154
- =end
134
+
135
+ def raise_possible_errors options
136
+ raise ArgumentError, "Sprite.new options must contain option :images" unless options[:images]
137
+ raise TypeError, "Sprite.new option :images must be of type Array" unless options[:images].kind_of? Array
138
+ raise "Sprite.new option :images is empty" if options[:images].empty?
139
+
140
+ raise ArgumentError, "Sprite.new options must contain option :fps" unless options[:fps]
141
+ raise TypeError, "Sprite.new option :fps must be of type Numeric" unless options[:fps].kind_of? Numeric
142
+ end
143
+ end # Okami::Sprite
data/lib/okami/window.rb CHANGED
@@ -1,12 +1,9 @@
1
1
  module Okami
2
2
  class Window < Gosu::Window
3
- include Gosu
4
- include Okami
5
3
  attr_reader :dt, :current_time
6
4
  attr_accessor :cursor_visible
7
5
 
8
6
  def initialize *args
9
- @previous_time = Gosu::milliseconds
10
7
  $window = super *args
11
8
  end
12
9
 
@@ -24,10 +21,14 @@ module Okami
24
21
  Okami::Mouse.button_up id
25
22
  end
26
23
 
27
- def fill color, z=0
24
+ def color_fill color, z=0
28
25
  color = case color
29
26
  when :white then 0xFFFFFFFF
30
27
  when :black then 0xFF000000
28
+ when Symbol
29
+ raise ArgumentError, "Unknown color #{color.inspect}"
30
+ else
31
+ color
31
32
  end
32
33
 
33
34
  draw_quad 0, 0, color,
@@ -38,15 +39,18 @@ module Okami
38
39
  end
39
40
 
40
41
  def calculate_dt
41
- @current_time = Gosu::milliseconds/1000.0
42
- @dt = @current_time - @previous_time
43
- @previous_time = @current_time
42
+ @current_time = Gosu::milliseconds/1000.0
43
+ @previous_time ||= @current_time - update_interval/1000.0
44
+ @dt = @current_time - @previous_time
45
+ @previous_time = @current_time
44
46
  end
45
47
 
46
48
  def self.inherited(subclass)
47
49
  def subclass.method_added(name)
48
50
  if name == :update
51
+ ## Prevent infinite loop
49
52
  return if caller.first.match(/^(.+?):(\d+)(?::in `(.*)')?/)[1].match /eval/i
53
+ ## Calculate_dt before update
50
54
  class_eval "def update_hook\n calculate_dt\n update_without_hook\nend"
51
55
  class_eval "alias update_without_hook update"
52
56
  class_eval "alias update update_hook"
@@ -56,4 +60,3 @@ module Okami
56
60
 
57
61
  end # Window
58
62
  end # Okami
59
-
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.985
4
+ version: 0.0.990
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: