rubysketch 0.5.2 → 0.5.4

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.
@@ -1,101 +1,409 @@
1
1
  module RubySketch
2
2
 
3
3
 
4
+ # Sprite object.
5
+ #
4
6
  class Sprite
5
7
 
6
- extend Forwardable
8
+ # Initialize sprite object.
9
+ #
10
+ # @overload new(image: img)
11
+ # pos: [0, 0], size: [image.width, image.height]
12
+ # @param [Image] img sprite image
13
+ #
14
+ # @overload new(x, y, image: img)
15
+ # pos: [x, y], size: [image.width, image.height]
16
+ # @param [Numeric] x x of sprite position
17
+ # @param [Numeric] y y of sprite position
18
+ # @param [Image] img sprite image
19
+ #
20
+ # @overload new(x, y, w, h)
21
+ # pos(x, y), size: [w, h]
22
+ # @param [Numeric] x x of sprite position
23
+ # @param [Numeric] y y of sprite position
24
+ # @param [Numeric] w width of sprite
25
+ # @param [Numeric] h height of sprite
26
+ #
27
+ # @overload new(x, y, w, h, image: img, offset: off)
28
+ # pos: [x, y], size: [w, h], offset: [offset.x, offset.x]
29
+ # @param [Numeric] x x of sprite position
30
+ # @param [Numeric] y y of sprite position
31
+ # @param [Numeric] w width of sprite
32
+ # @param [Numeric] h height of sprite
33
+ # @param [Image] img sprite image
34
+ # @param [Vector] off offset of sprite image
35
+ #
36
+ def initialize(x = 0, y = 0, w = nil, h = nil, image: nil, offset: nil)
37
+ w ||= (image&.width || 0)
38
+ h ||= (image&.height || 0)
39
+ raise 'invalid size' unless w >= 0 && h >= 0
40
+ raise 'invalid image' if image && !image.getInternal__.is_a?(Rays::Image)
7
41
 
8
- def initialize(x = 0, y = 0, w = nil, h = nil, image: nil, offset: nil, dynamic: false)
9
- w ||= image&.width || 0
10
- h ||= image&.height || 0
11
- raise 'invalid size' unless w >= 0 && h >= 0
42
+ @view__ = SpriteView.new(self, x: x, y: y, w: w, h: h, back: :white)
12
43
 
13
- @image__, @offset__ = image, offset
14
- @view__ = View.new self, x: x, y: y, w: w, h: h, back: :white, dynamic: dynamic
44
+ self.image = image if image
45
+ self.offset = offset if offset
15
46
  end
16
47
 
17
- def_delegators :@view__,
18
- :x, :x=,
19
- :y, :y=,
20
- :w, :h,
21
- :width, :height,
22
- :dynamic?, :dynamic=,
23
- :density, :density=,
24
- :friction, :friction=,
25
- :restitution, :restitution=
48
+ # Returns the position of the sprite.
49
+ #
50
+ # @return [Vector] position
51
+ #
52
+ def position()
53
+ @view__.position.toVector
54
+ end
26
55
 
27
- def update(&block)
28
- @view__.update = block
56
+ # Sets the position of the sprite.
57
+ #
58
+ # @overload position=(vec)
59
+ # @param [Vector] vec position vector
60
+ #
61
+ # @overload position=(ary)
62
+ # @param [Array<Numeric>] ary positionX, positionY
63
+ #
64
+ # @return [Vector] position
65
+ #
66
+ def position=(arg)
67
+ @view__.position = arg.is_a?(Vector) ? arg.getInternal__ : arg
68
+ arg
29
69
  end
30
70
 
31
- def contact(&block)
32
- @view__.contact = block
71
+ # Returns the x-coordinate position of the sprite.
72
+ #
73
+ # @return [Numeric] sprite position x
74
+ #
75
+ def x()
76
+ @view__.x
33
77
  end
34
78
 
35
- def position()
36
- @view__.position.toVector
79
+ # Set the x-coordinate position of the sprite.
80
+ #
81
+ # @param [Numeric] n sprite position x
82
+ #
83
+ # @return [Numeric] sprite position x
84
+ #
85
+ def x=(n)
86
+ @view__.x = n
87
+ n
37
88
  end
38
89
 
39
- def position=(vector)
40
- @view__.position = vector.getInternal__
90
+ # Returns the y-coordinate position of the sprite.
91
+ #
92
+ # @return [Numeric] sprite position y
93
+ #
94
+ def y()
95
+ @view__.y
41
96
  end
42
97
 
98
+ # Set the y-coordinate position of the sprite.
99
+ #
100
+ # @param [Numeric] n sprite position y
101
+ #
102
+ # @return [Numeric] sprite position y
103
+ #
104
+ def y=(n)
105
+ @view__.y = n
106
+ n
107
+ end
108
+
109
+ alias pos position
110
+ alias pos= position=
111
+
112
+ # Returns the size of the sprite.
113
+ #
114
+ # @return [Vector] size
115
+ #
43
116
  def size()
44
117
  @view__.size.toVector
45
118
  end
46
119
 
120
+ # Returns the width of the sprite.
121
+ #
122
+ # @return [Numeric] width
123
+ #
124
+ def width()
125
+ @view__.width
126
+ end
127
+
128
+ # Returns the height of the sprite.
129
+ #
130
+ # @return [Numeric] height
131
+ #
132
+ def height()
133
+ @view__.height
134
+ end
135
+
136
+ alias w width
137
+ alias h height
138
+
139
+ # Returns the velocity of the sprite.
140
+ #
141
+ # @return [Vector] velocity
142
+ #
143
+ def velocity()
144
+ @view__.velocity.toVector
145
+ end
146
+
147
+ # Sets the velocity of the sprite.
148
+ #
149
+ # @overload velocity=(vec)
150
+ # @param [Vector] vec velocity vector
151
+ #
152
+ # @overload velocity=(ary)
153
+ # @param [Array<Numeric>] ary velocityX, velocityY
154
+ #
155
+ # @return [Vector] velocity
156
+ #
157
+ def velocity=(arg)
158
+ @view__.velocity = arg.is_a?(Vector) ? arg.getInternal__ : arg
159
+ arg
160
+ end
161
+
162
+ # Returns the x-axis velocity of the sprite.
163
+ #
164
+ # @return [Numeric] velocity.x
165
+ #
166
+ def vx()
167
+ @view__.velocity.x
168
+ end
169
+
170
+ # Sets the x-axis velocity of the sprite.
171
+ #
172
+ # @param [Numeric] n x-axis velocity
173
+ #
174
+ # @return [Numeric] velocity.x
175
+ #
176
+ def vx=(n)
177
+ @view__.velocity = @view__.velocity.tap {|v| v.x = n}
178
+ n
179
+ end
180
+
181
+ # Returns the y-axis velocity of the sprite.
182
+ #
183
+ # @return [Numeric] velocity.y
184
+ #
185
+ def vy()
186
+ @view__.velocity.y
187
+ end
188
+
189
+ # Sets the y-axis velocity of the sprite.
190
+ #
191
+ # @param [Numeric] n y-axis velocity
192
+ #
193
+ # @return [Numeric] velocity.y
194
+ #
195
+ def vy=(n)
196
+ @view__.velocity = @view__.velocity.tap {|v| v.y = n}
197
+ n
198
+ end
199
+
200
+ alias vel velocity
201
+ alias vel= velocity=
202
+
203
+ # Returns the image of the sprite.
204
+ #
205
+ # @return [Image] sprite image
206
+ #
47
207
  def image()
48
208
  @image__
49
209
  end
50
210
 
211
+ # Sets the sprite image.
212
+ #
213
+ # @param [Image] img sprite image
214
+ #
215
+ # @return [Image] sprite image
216
+ #
217
+ def image=(img)
218
+ @image__ = img
219
+ end
220
+
221
+ # Returns the offset of the sprite image.
222
+ #
223
+ # @return [Vector] offset of the sprite image
224
+ #
51
225
  def offset()
52
226
  @offset__
53
227
  end
54
228
 
55
- def velocity()
56
- @view__.velocity.toVector
229
+ def offset=(arg)
230
+ @offset__ =
231
+ case arg
232
+ when Vector then arg
233
+ when Array then Vector.new(*arg[0, 2])
234
+ when nil then nil
235
+ else raise ArgumentError
236
+ end
237
+ end
238
+
239
+ # Returns whether the sprite is movable by the physics engine.
240
+ #
241
+ # @return [Boolean] true if dynamic
242
+ #
243
+ def dynamic?()
244
+ @view__.dynamic?
57
245
  end
58
246
 
59
- def velocity=(vector)
60
- @view__.velocity = vector.getInternal__
247
+ # Sets whether the sprite is movable by the physics engine.
248
+ #
249
+ # @param [Boolean] bool movable or not
250
+ #
251
+ # @return [Boolean] true if dynamic
252
+ #
253
+ def dynamic=(bool)
254
+ @view__.dynamic = bool
255
+ bool
256
+ end
257
+
258
+ # Returns the density of the sprite.
259
+ #
260
+ # @return [Numeric] density
261
+ #
262
+ def density()
263
+ @view__.density
264
+ end
265
+
266
+ # Sets the density of the sprite.
267
+ #
268
+ # @param [Numeric] n density
269
+ #
270
+ # @return [Numeric] density
271
+ #
272
+ def density=(n)
273
+ @view__.density = n
274
+ n
275
+ end
276
+
277
+ # Returns the friction of the sprite.
278
+ #
279
+ # @return [Numeric] friction
280
+ #
281
+ def friction()
282
+ @view__.friction
283
+ end
284
+
285
+ # Sets the friction of the sprite.
286
+ #
287
+ # @param [Numeric] n friction
288
+ #
289
+ # @return [Numeric] friction
290
+ #
291
+ def friction=(n)
292
+ @view__.friction = n
293
+ n
294
+ end
295
+
296
+ # Returns the restitution of the sprite.
297
+ #
298
+ # @return [Numeric] restitution
299
+ #
300
+ def restitution()
301
+ @view__.restitution
302
+ end
303
+
304
+ # Sets the restitution of the sprite.
305
+ #
306
+ # @param [Numeric] n restitution
307
+ #
308
+ # @return [Numeric] restitution
309
+ #
310
+ def restitution=(n)
311
+ @view__.restitution = n
312
+ n
61
313
  end
62
314
 
63
- alias pos position
64
- alias pos= position=
65
- alias vel velocity
66
- alias vel= velocity=
67
315
  alias dens density
68
316
  alias dens= density=
69
317
  alias fric friction
70
- alias fric= friction
318
+ alias fric= friction=
71
319
  alias rest restitution
72
320
  alias rest= restitution=
73
321
 
322
+ # Defines update block.
323
+ #
324
+ # @example vx is updated every frame
325
+ # sprite.update do
326
+ # self.vx *= 0.9
327
+ # end
328
+ #
329
+ # @return [nil] nil
330
+ #
331
+ def update(&block)
332
+ @view__.update = block
333
+ end
334
+
335
+ # Defines contact block.
336
+ #
337
+ # @example Score increases when the player sprite touches a coin
338
+ # playerSprite.contact do |o|
339
+ # score += 1 if o.coin?
340
+ # end
341
+ #
342
+ # @return [nil] nil
343
+ #
344
+ def contact(&block)
345
+ @view__.contact = block
346
+ end
347
+
348
+ # Defines contact? block.
349
+ #
350
+ # @example only collide with an enemy
351
+ # playerSprite.contact? do |o|
352
+ # o.enemy?
353
+ # end
354
+ #
355
+ # @return [nil] nil
356
+ #
357
+ def contact?(&block)
358
+ @view__.will_contact = block
359
+ end
360
+
361
+ # @private
362
+ def on_draw__(x, y, w, h)
363
+ img, off = frame, @image__, @offset__
364
+ if img && off
365
+ copy img, off.x, off.y, f.w, h, x, y, w, h
366
+ elsif img
367
+ image img, x, y
368
+ else
369
+ rect x, y, w, h
370
+ end
371
+ end
372
+
74
373
  # @private
75
374
  def getInternal__()
76
375
  @view__
77
376
  end
78
377
 
79
- class View < Reflex::View
80
- attr_accessor :update, :contact
81
- attr_reader :sprite
378
+ end# Sprite
82
379
 
83
- def initialize(sprite, *a, **k, &b)
84
- @sprite = sprite
85
- super(*a, **k, &b)
86
- end
87
380
 
88
- def on_update(e)
89
- @update.call if @update
90
- end
381
+ # @private
382
+ class SpriteView < Reflex::View
91
383
 
92
- def on_contact(e)
93
- v = e.view
94
- @contact.call v.sprite, e.action if @contact && v.is_a?(View)
95
- end
384
+ attr_accessor :update, :contact, :will_contact
385
+ attr_reader :sprite
386
+
387
+ def initialize(sprite, *a, **k, &b)
388
+ @sprite = sprite
389
+ super(*a, **k, &b)
96
390
  end
97
391
 
98
- end# Sprite
392
+ def on_update(e)
393
+ @update.call if @update
394
+ end
395
+
396
+ def on_contact(e)
397
+ v = e.view
398
+ @contact.call v.sprite, e.action if @contact && v.is_a?(SpriteView)
399
+ end
400
+
401
+ def will_contact?(v)
402
+ return true if !@will_contact || !v.is_a?(SpriteView)
403
+ @will_contact.call v.sprite
404
+ end
405
+
406
+ end# SpriteView
99
407
 
100
408
 
101
409
  end# RubySketch
@@ -0,0 +1,14 @@
1
+ module RubySketch
2
+
3
+
4
+ # @private
5
+ class Window < Processing::Window
6
+
7
+ def on_update(e)
8
+ Beeps.process_streams!
9
+ end
10
+
11
+ end# Window
12
+
13
+
14
+ end# RubySketch
data/lib/rubysketch.rb CHANGED
@@ -2,7 +2,7 @@ require 'rubysketch/all'
2
2
 
3
3
 
4
4
  module RubySketch
5
- WINDOW = Processing::Window.new {start}
5
+ WINDOW = RubySketch::Window.new {start}
6
6
  CONTEXT = RubySketch::Context.new WINDOW
7
7
 
8
8
  refine Object do
@@ -25,6 +25,6 @@ begin
25
25
  w.__send__ :begin_draw
26
26
  at_exit do
27
27
  w.__send__ :end_draw
28
- Processing::App.new {w.show}.start if c.hasDrawBlock__ && !$!
28
+ Processing::App.new {w.show}.start if c.hasUserBlocks__ && !$!
29
29
  end
30
30
  end
data/pod.rake ADDED
@@ -0,0 +1,35 @@
1
+ # -*- mode: ruby -*-
2
+
3
+
4
+ github = 'https://github.com/xord'
5
+ renames = {reflexion: 'reflex'}
6
+ regexp = /add\w+dependency.*['"](\w+)['"].*['"]\s*~>\s*([\d\.]+)\s*['"]/
7
+ repos = File.readlines('rubysketch.gemspec', chomp: true)
8
+ .map {|s| regexp.match(s)&.values_at 1, 2}
9
+ .compact
10
+ .to_h
11
+ .transform_keys {|name| renames[name.to_sym].then {|s| s || name}}
12
+
13
+
14
+ task :clobber do
15
+ sh %( rm -rf #{repos.keys.join ' '} )
16
+ end
17
+
18
+ task :setup
19
+
20
+ repos.each do |repo, ver|
21
+ rakefile = "#{repo}/Rakefile"
22
+ opts = [
23
+ '-c advice.detachedHead=false',
24
+ '--no-single-branch',
25
+ '--depth 1',
26
+ "--branch #{ENV['RUBYSKETCH_BRANCH'] || ('v' + ver)}"
27
+ ]
28
+
29
+ task :setup => rakefile
30
+
31
+ file rakefile do
32
+ sh %( git clone #{opts.join ' '} #{github}/#{repo} )
33
+ sh %( cd #{repo} && VENDOR_NOCOMPILE=1 rake vendor erb )
34
+ end
35
+ end
data/rubysketch.gemspec CHANGED
@@ -28,12 +28,12 @@ Gem::Specification.new do |s|
28
28
  s.platform = Gem::Platform::RUBY
29
29
  s.required_ruby_version = '>= 2.7.0'
30
30
 
31
- s.add_runtime_dependency 'xot', '~> 0.1.32'
32
- s.add_runtime_dependency 'rucy', '~> 0.1.32'
33
- s.add_runtime_dependency 'beeps', '~> 0.1.32'
34
- s.add_runtime_dependency 'rays', '~> 0.1.32'
35
- s.add_runtime_dependency 'reflexion', '~> 0.1.32'
36
- s.add_runtime_dependency 'processing', '~> 0.5.2'
31
+ s.add_runtime_dependency 'xot', '~> 0.1.34'
32
+ s.add_runtime_dependency 'rucy', '~> 0.1.34'
33
+ s.add_runtime_dependency 'beeps', '~> 0.1.35'
34
+ s.add_runtime_dependency 'rays', '~> 0.1.34'
35
+ s.add_runtime_dependency 'reflexion', '~> 0.1.35'
36
+ s.add_runtime_dependency 'processing', '~> 0.5.5'
37
37
 
38
38
  s.add_development_dependency 'rake'
39
39
  s.add_development_dependency 'test-unit'
data/src/RubySketch.h ADDED
@@ -0,0 +1,22 @@
1
+ // -*- mode: objc -*-
2
+ #import <Foundation/Foundation.h>
3
+ #import <CRBValue.h>
4
+
5
+
6
+ @interface RubySketch : NSObject
7
+
8
+ typedef void (^RescueBlock) (CRBValue* exception);
9
+
10
+ + (void) setup;
11
+
12
+ + (BOOL) start;
13
+ + (BOOL) startWithRescue: (RescueBlock) rescue;
14
+
15
+ + (BOOL) start: (NSString*) path;
16
+ + (BOOL) start: (NSString*) path rescue: (RescueBlock) rescue;
17
+
18
+ + (void) setActiveReflexViewController: (id) reflexViewController;
19
+
20
+ + (void) resetActiveReflexViewController;
21
+
22
+ @end
data/src/RubySketch.mm ADDED
@@ -0,0 +1,92 @@
1
+ // -*- mode: objc -*-
2
+ #import <CRuby.h>
3
+ #import "RubySketch.h"
4
+ #include "../reflex/src/ios/view_controller.h"
5
+
6
+
7
+ extern "C"
8
+ {
9
+ void Init_beeps_native ();
10
+ void Init_rays_native ();
11
+ void Init_reflex_native ();
12
+ }
13
+
14
+
15
+ static ReflexViewController* active_reflex_view_controller = nil;
16
+
17
+ static ReflexViewController*
18
+ ReflexViewController_create()
19
+ {
20
+ return active_reflex_view_controller;
21
+ }
22
+
23
+ static void
24
+ ReflexViewController_show (UIViewController*, ReflexViewController*)
25
+ {
26
+ }
27
+
28
+
29
+ @implementation RubySketch
30
+
31
+ + (void) setup
32
+ {
33
+ static BOOL done = NO;
34
+ if (done) return;
35
+ done = YES;
36
+
37
+ [CRuby addExtension:@"beeps/native" init:^{Init_beeps_native();}];
38
+ [CRuby addExtension:@"rays/native" init:^{Init_rays_native();}];
39
+ [CRuby addExtension:@"reflex/native" init:^{Init_reflex_native();}];
40
+
41
+ for (NSString *ext in @[
42
+ @"Xot",
43
+ @"Rucy",
44
+ @"Beeps",
45
+ @"Rays",
46
+ @"Reflex",
47
+ @"Processing",
48
+ @"RubySketch"
49
+ ]) [CRuby addLibrary:ext bundle:[NSBundle bundleForClass:RubySketch.class]];
50
+
51
+ ReflexViewController_set_create_fun(ReflexViewController_create);
52
+ ReflexViewController_set_show_fun(ReflexViewController_show);
53
+ }
54
+
55
+ + (BOOL) start
56
+ {
57
+ return [self start:@"main.rb" rescue:nil];
58
+ }
59
+
60
+ + (BOOL) startWithRescue: (RescueBlock) rescue
61
+ {
62
+ return [self start:@"main.rb" rescue:rescue];
63
+ }
64
+
65
+ + (BOOL) start: (NSString*) path
66
+ {
67
+ return [self start:path rescue:nil];
68
+ }
69
+
70
+ + (BOOL) start: (NSString*) path rescue: (RescueBlock) rescue
71
+ {
72
+ CRBValue* ret = [CRuby evaluate:[NSString stringWithFormat:@
73
+ "raise 'already started' unless require 'rubysketch'\n"
74
+ "load '%@'\n"
75
+ "RubySketch::WINDOW.__send__ :end_draw\n"
76
+ "RubySketch::WINDOW.show",
77
+ path
78
+ ]];
79
+ return ret && ret.toBOOL;
80
+ }
81
+
82
+ + (void) setActiveReflexViewController: (id) reflexViewController
83
+ {
84
+ active_reflex_view_controller = reflexViewController;
85
+ }
86
+
87
+ + (void) resetActiveReflexViewController
88
+ {
89
+ active_reflex_view_controller = nil;
90
+ }
91
+
92
+ @end
data/test/helper.rb CHANGED
@@ -1,12 +1,13 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
 
4
- %w[../xot ../rucy ../rays ../reflex ../processing .]
4
+ %w[../xot ../rucy ../beeps ../rays ../reflex ../processing .]
5
5
  .map {|s| File.expand_path "../#{s}/lib", __dir__}
6
6
  .each {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
7
7
 
8
- require 'test/unit'
9
8
  require 'xot/test'
10
9
  require 'rubysketch/all'
11
10
 
11
+ require 'test/unit'
12
+
12
13
  include Xot::Test