rubysketch 0.5.4 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 82a93ef9dac4ae855bfa85f9a076ed05c807875db70be9c5e72419137499434e
4
- data.tar.gz: 2f432e94c80ad231f50ca11df96ecd6a13c389551f75c426e161701e25d85a23
3
+ metadata.gz: 11852910038d162e39601eb01056ee3b4161c4ac0576ac93aef87e25eed557ee
4
+ data.tar.gz: 6d978cf96d2e8d32df00477e77f03c645840bb70985f74e5d254ff5f47f5cd4c
5
5
  SHA512:
6
- metadata.gz: cdf7238a4d07fe88b5d1fe3d68ea6f62d9536b142afda5dad1f4c15538bae2047d4e188bbc88e0777d641650ad9c5f5864720345ad5ec950e2ff18e83f552db9
7
- data.tar.gz: 310a71a0b3388876bd0c0910a09bb9aa12e5686e5885ec8e4b2a32cb4af629ed18a010731a77b4a8ea1a9a4be41de504098767d0d133e3344554d5078508ad29
6
+ metadata.gz: '0593125c1c609a1f1c38eb45631f558fcb525db85792335524a37718d306c770d12f12012d3ddf5752080318070eb139d8ece676fb436f5a10abd88af02b2c8b'
7
+ data.tar.gz: 2d78202eb29f9541886efc54ec44142caf1cbd192ff58e51a26af5d3a0b8cb242c80be05171013f215b698a01cd70b9878fb10a2364abb4259402c48f6ea8475
data/ChangeLog.md CHANGED
@@ -1,6 +1,22 @@
1
1
  # rubysketch ChangeLog
2
2
 
3
3
 
4
+ ## [v0.5.5] - 2023-05-08
5
+
6
+ - Add Sprite#draw()
7
+ - Add Sprite#angle accessor
8
+ - Add Sprite#pivot accessor
9
+ - Add Sprite#ox and Sprite#oy
10
+ - Add mousePressed, mouseReleased, mouseMoved, mouseDragged, mouseClicked, touchStarted, touchEnded, and touchMoved to Sprite class
11
+ - Add inspect() to classes
12
+ - Alias draw methods
13
+ - Sprite has density 1 by default
14
+ - Sprite is static by deault
15
+ - Add sprite.rb and physics.rb as an example
16
+ - Delete Sound class
17
+ - Remove wall collision by default
18
+
19
+
4
20
  ## [v0.5.4] - 2023-04-30
5
21
 
6
22
  - Add Sprite#image=() and Sprite#offset=()
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.4
1
+ 0.5.5
@@ -0,0 +1,24 @@
1
+ %w[xot rucy beeps rays reflex processing rubysketch]
2
+ .map {|s| File.expand_path "../../#{s}/lib", __dir__}
3
+ .each {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
4
+
5
+ require 'rubysketch'
6
+ using RubySketch
7
+
8
+ noStroke
9
+ gravity 0, 1000
10
+
11
+ sprites = []
12
+ ground = createSprite 0, height - 10, width, 10
13
+
14
+ draw do
15
+ background 100
16
+ sprite *sprites, ground
17
+ end
18
+
19
+ mousePressed do
20
+ sp = createSprite mouseX, mouseY, 20, 20
21
+ sp.dynamic = true
22
+ sp.restitution = 0.5
23
+ sprites << sp
24
+ end
@@ -0,0 +1,47 @@
1
+ %w[xot rucy beeps rays reflex processing rubysketch]
2
+ .map {|s| File.expand_path "../../#{s}/lib", __dir__}
3
+ .each {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
4
+
5
+ require 'rubysketch'
6
+ using RubySketch
7
+
8
+ sp = createSprite 100, 100, 50, 50
9
+ sp.angle += Math::PI * 0.2
10
+
11
+ red = 0
12
+
13
+ sp.update do
14
+ red = (red + 1) % 255
15
+ end
16
+
17
+ sp.draw do |&draw|
18
+ fill red, 200, 200
19
+ draw.call
20
+ fill 0
21
+ text :hello, 10, 20
22
+ end
23
+
24
+ sp.mousePressed do
25
+ p [:pressed, sp.mouseX, sp.mouseY, sp.mouseButton]
26
+ end
27
+
28
+ sp.mouseReleased do
29
+ p [:released, sp.mouseX, sp.mouseY, sp.mouseButton]
30
+ end
31
+
32
+ sp.mouseMoved do
33
+ p [:moved, sp.mouseX, sp.mouseY, sp.pmouseX, sp.pmouseY]
34
+ end
35
+
36
+ sp.mouseDragged do
37
+ p [:dragged, sp.mouseX, sp.mouseY, sp.pmouseX, sp.pmouseY]
38
+ end
39
+
40
+ sp.mouseClicked do
41
+ p [:clicked, sp.mouseX, sp.mouseY, sp.mouseButton]
42
+ end
43
+
44
+ draw do
45
+ background 0
46
+ sprite sp
47
+ end
@@ -6,8 +6,6 @@ require 'rubysketch/window'
6
6
  require 'rubysketch/helper'
7
7
 
8
8
  require 'rubysketch/sprite'
9
- require 'rubysketch/sound'
10
- require 'rubysketch/graphics_context'
11
9
  require 'rubysketch/context'
12
10
 
13
11
 
@@ -3,9 +3,6 @@ module RubySketch
3
3
 
4
4
  class Context < Processing::Context
5
5
 
6
- include GraphicsContext
7
-
8
- Sound = RubySketch::Sound
9
6
  Sprite = RubySketch::Sprite
10
7
 
11
8
  # @private
@@ -43,13 +40,15 @@ module RubySketch
43
40
  # @param [Vector] off offset of sprite image
44
41
  #
45
42
  def createSprite(*args, **kwargs)
46
- addSprite Sprite.new(*args, **kwargs)
43
+ addSprite Sprite.new(*args, **kwargs, context: self)
47
44
  end
48
45
 
49
46
  # Adds the sprite to the physics engine.
50
47
  #
51
48
  # @param [Sprite] sprite sprite object
52
49
  #
50
+ # @return [Sprite] added sprite
51
+ #
53
52
  def addSprite(sprite)
54
53
  @layer__.add sprite.getInternal__ if sprite
55
54
  sprite
@@ -59,14 +58,57 @@ module RubySketch
59
58
  #
60
59
  # @param [Sprite] sprite sprite object
61
60
  #
61
+ # @return [Sprite] removed sprite
62
+ #
62
63
  def removeSprite(sprite)
63
64
  @layer__.remove sprite.getInternal__ if sprite
64
65
  sprite
65
66
  end
66
67
 
68
+ # Draws one or more sprites.
69
+ #
70
+ # @param [Array<Sprite>] sprites
71
+ #
72
+ # @return [nil] nil
73
+ #
74
+ def sprite(*sprites)
75
+ sprites.flatten! if sprites.first&.is_a? Array
76
+ sprites.each do |sp|
77
+ view, draw = sp.getInternal__, sp.instance_variable_get(:@drawBlock__)
78
+ f, degrees, pivot = view.frame, view.angle, view.pivot
79
+ if draw
80
+ push do
81
+ translate f.x + pivot.x * f.w, f.y + pivot.y * f.h
82
+ rotate fromDegrees__ degrees
83
+ translate -pivot.x * f.w, -pivot.y * f.h
84
+ draw.call {drawSprite__ sp, 0, 0, f.w, f.h}
85
+ end
86
+ elsif degrees == 0
87
+ drawSprite__ sp, f.x, f.y, f.w, f.h
88
+ else
89
+ pushMatrix do
90
+ translate f.x + pivot.x * f.w, f.y + pivot.y * f.h
91
+ rotate fromDegrees__ degrees
92
+ translate -pivot.x * f.w, -pivot.y * f.h
93
+ drawSprite__ sp, 0, 0, f.w, f.h
94
+ end
95
+ end
96
+ end
97
+ nil
98
+ end
99
+
100
+ alias drawSprite sprite
101
+
67
102
  # @private
68
- def loadSound(path)
69
- Sound.load path
103
+ def drawSprite__(sp, x, y, w, h)
104
+ img, off = sp.image, sp.offset
105
+ if img && off
106
+ copy img, off.x, off.y, w, h, x, y, w, h
107
+ elsif img
108
+ image img, x, y
109
+ else
110
+ rect x, y, w, h
111
+ end
70
112
  end
71
113
 
72
114
  # Sets gravity for the physics engine.
@@ -97,6 +139,11 @@ module RubySketch
97
139
  # @private
98
140
  class SpriteLayer < Reflex::View
99
141
 
142
+ def initialize(*a, **k, &b)
143
+ super
144
+ remove wall
145
+ end
146
+
100
147
  def on_draw(e)
101
148
  e.block
102
149
  end
@@ -5,6 +5,8 @@ module RubySketch
5
5
  #
6
6
  class Sprite
7
7
 
8
+ include Xot::Inspectable
9
+
8
10
  # Initialize sprite object.
9
11
  #
10
12
  # @overload new(image: img)
@@ -33,13 +35,20 @@ module RubySketch
33
35
  # @param [Image] img sprite image
34
36
  # @param [Vector] off offset of sprite image
35
37
  #
36
- def initialize(x = 0, y = 0, w = nil, h = nil, image: nil, offset: nil)
38
+ def initialize(
39
+ x = 0, y = 0, w = nil, h = nil, image: nil, offset: nil,
40
+ context: nil)
41
+
37
42
  w ||= (image&.width || 0)
38
43
  h ||= (image&.height || 0)
39
44
  raise 'invalid size' unless w >= 0 && h >= 0
40
45
  raise 'invalid image' if image && !image.getInternal__.is_a?(Rays::Image)
41
46
 
42
- @view__ = SpriteView.new(self, x: x, y: y, w: w, h: h, back: :white)
47
+ @context__ = context || Context.context__
48
+ @view__ = SpriteView.new(
49
+ self, x: x, y: y, w: w, h: h,
50
+ static: true, density: 1, friction: 0, restitution: 0,
51
+ back: :white)
43
52
 
44
53
  self.image = image if image
45
54
  self.offset = offset if offset
@@ -59,7 +68,7 @@ module RubySketch
59
68
  # @param [Vector] vec position vector
60
69
  #
61
70
  # @overload position=(ary)
62
- # @param [Array<Numeric>] ary positionX, positionY
71
+ # @param [Array<Numeric>] ary an array of positionX and positionY
63
72
  #
64
73
  # @return [Vector] position
65
74
  #
@@ -136,6 +145,48 @@ module RubySketch
136
145
  alias w width
137
146
  alias h height
138
147
 
148
+ # Returns the rotation angle of sprite.
149
+ #
150
+ # @return [Numeric] radians or degrees depending on angleMode()
151
+ #
152
+ def angle()
153
+ a, c = @view__.angle, @context__
154
+ c ? c.fromDegrees__(a) : a * Processing::GraphicsContext::DEG2RAD__
155
+ end
156
+
157
+ # Sets the rotation angle of sprite.
158
+ #
159
+ # @param [Numeric] angle radians or degrees depending on angleMode()
160
+ #
161
+ # @return [Numeric] angle
162
+ #
163
+ def angle=(angle)
164
+ c = @context__
165
+ @view__.angle =
166
+ c ? c.toAngle__(angle) : angle * Processing::GraphicsContext::RAD2DEG__
167
+ angle
168
+ end
169
+
170
+ # Returns the rotation center of sprite.
171
+ #
172
+ # @return [Array<Numeric>] [pivotX, pivotY]
173
+ #
174
+ def pivot()
175
+ @view__.pivot.to_a[0, 2]
176
+ end
177
+
178
+ # Sets the rotation center of sprite.
179
+ # [0.0, 0.0] is the left-top, [1.0, 1.0] is the right-bottom, and [0.5, 0.5] is the center.
180
+ #
181
+ # @param [Array<Numeric>] ary an array of pivotX and pivotY
182
+ #
183
+ # @return [Array<Numeric>] [pivotX, pivotY]
184
+ #
185
+ def pivot=(array)
186
+ @view__.pivot = array
187
+ pivot
188
+ end
189
+
139
190
  # Returns the velocity of the sprite.
140
191
  #
141
192
  # @return [Vector] velocity
@@ -150,7 +201,7 @@ module RubySketch
150
201
  # @param [Vector] vec velocity vector
151
202
  #
152
203
  # @overload velocity=(ary)
153
- # @param [Array<Numeric>] ary velocityX, velocityY
204
+ # @param [Array<Numeric>] ary an array of velocityX and velocityY
154
205
  #
155
206
  # @return [Vector] velocity
156
207
  #
@@ -226,14 +277,62 @@ module RubySketch
226
277
  @offset__
227
278
  end
228
279
 
280
+ # Sets the offset of the sprite image.
281
+ #
282
+ # @overload offset=(vec)
283
+ # @param [Vector] vec offset
284
+ #
285
+ # @overload velocity=(ary)
286
+ # @param [Array<Numeric>] ary an array of offsetX and offsetY
287
+ #
288
+ # @return [Vector] offset of the sprite image
289
+ #
229
290
  def offset=(arg)
230
291
  @offset__ =
231
292
  case arg
232
293
  when Vector then arg
233
- when Array then Vector.new(*arg[0, 2])
294
+ when Array then Vector.new(arg[0] || 0, arg[1] || 0)
234
295
  when nil then nil
235
296
  else raise ArgumentError
236
297
  end
298
+ @offset__
299
+ end
300
+
301
+ # Returns the x-axis offset of the sprite image.
302
+ #
303
+ # @return [Numeric] offset.x
304
+ #
305
+ def ox()
306
+ @offset__&.x || 0
307
+ end
308
+
309
+ # Sets the x-axis offset of the sprite image.
310
+ #
311
+ # @param [Numeric] n x-axis offset
312
+ #
313
+ # @return [Numeric] offset.x
314
+ #
315
+ def ox=(n)
316
+ self.offset = [n, oy]
317
+ n
318
+ end
319
+
320
+ # Returns the y-axis offset of the sprite image.
321
+ #
322
+ # @return [Numeric] offset.y
323
+ #
324
+ def oy()
325
+ @offset__&.y || 0
326
+ end
327
+
328
+ # Sets the y-axis offset of the sprite image.
329
+ #
330
+ # @param [Numeric] n y-axis offset
331
+ #
332
+ # @return [Numeric] offset.y
333
+ #
334
+ def oy=(n)
335
+ self.offset = [ox, n]
237
336
  end
238
337
 
239
338
  # Returns whether the sprite is movable by the physics engine.
@@ -319,6 +418,54 @@ module RubySketch
319
418
  alias rest restitution
320
419
  alias rest= restitution=
321
420
 
421
+ # Returns the x-position of the mouse in the sprite coordinates.
422
+ #
423
+ # @return [Numeric] x position
424
+ #
425
+ def mouseX()
426
+ @view__.mouseX
427
+ end
428
+
429
+ # Returns the y-position of the mouse in the sprite coordinates.
430
+ #
431
+ # @return [Numeric] y position
432
+ #
433
+ def mouseY()
434
+ @view__.mouseY
435
+ end
436
+
437
+ # Returns the previous x-position of the mouse in the sprite coordinates.
438
+ #
439
+ # @return [Numeric] x position
440
+ #
441
+ def pmouseX()
442
+ @view__.pmouseX
443
+ end
444
+
445
+ # Returns the previous y-position of the mouse in the sprite coordinates.
446
+ #
447
+ # @return [Numeric] y position
448
+ #
449
+ def pmouseY()
450
+ @view__.pmouseY
451
+ end
452
+
453
+ # Returns the mouse button clicked on the sprite.
454
+ #
455
+ # @return [LEFT, RIGHT, CENTER] mouse button
456
+ #
457
+ def mouseButton()
458
+ @view__.mouseButton
459
+ end
460
+
461
+ # Returns the touch objects touched on the sprite.
462
+ #
463
+ # @return [Array<Touch>] touches
464
+ #
465
+ def touches()
466
+ @view__.touches
467
+ end
468
+
322
469
  # Defines update block.
323
470
  #
324
471
  # @example vx is updated every frame
@@ -332,6 +479,134 @@ module RubySketch
332
479
  @view__.update = block
333
480
  end
334
481
 
482
+ # Defines draw block.
483
+ #
484
+ # @example Draw on your own before and after default drawing
485
+ # sprite.draw do |&defaultDrawSprite|
486
+ # rect 0, 0, 10, 10
487
+ # defaultDrawSprite.call
488
+ # text :hello, 10, 20
489
+ # end
490
+ #
491
+ # @return [nil] nil
492
+ #
493
+ def draw(&block)
494
+ @drawBlock__ = block
495
+ nil
496
+ end
497
+
498
+ # Defines mousePressed block.
499
+ #
500
+ # @example Print mouse states on mouse press
501
+ # sprite.mousePressed do
502
+ # p [sprite.mouseX, sprite.mouseY, sprite.mouseButton]
503
+ # end
504
+ #
505
+ # @return [nil] nil
506
+ #
507
+ def mousePressed(&block)
508
+ @view__.mousePressed = block
509
+ nil
510
+ end
511
+
512
+ # Defines mouseReleased block.
513
+ #
514
+ # @example Print mouse states on mouse release
515
+ # sprite.mouseReleased do
516
+ # p [sprite.mouseX, sprite.mouseY, sprite.mouseButton]
517
+ # end
518
+ #
519
+ # @return [nil] nil
520
+ #
521
+ def mouseReleased(&block)
522
+ @view__.mouseReleased = block
523
+ nil
524
+ end
525
+
526
+ # Defines mouseMoved block.
527
+ #
528
+ # @example Print mouse states on mouse move
529
+ # sprite.mouseMoved do
530
+ # p [sprite.mouseX, sprite.mouseY, sprite.pmouseX, sprite.pmouseY]
531
+ # end
532
+ #
533
+ # @return [nil] nil
534
+ #
535
+ def mouseMoved(&block)
536
+ @view__.mouseMoved = block
537
+ nil
538
+ end
539
+
540
+ # Defines mouseDragged block.
541
+ #
542
+ # @example Print mouse states on mouse drag
543
+ # sprite.mouseDragged do
544
+ # p [sprite.mouseX, sprite.mouseY, sprite.pmouseX, sprite.pmouseY]
545
+ # end
546
+ #
547
+ # @return [nil] nil
548
+ #
549
+ def mouseDragged(&block)
550
+ @view__.mouseDragged = block
551
+ nil
552
+ end
553
+
554
+ # Defines mouseClicked block.
555
+ #
556
+ # @example Print mouse states on mouse click
557
+ # sprite.mouseClicked do
558
+ # p [sprite.mouseX, sprite.mouseY, sprite.mouseButton]
559
+ # end
560
+ #
561
+ # @return [nil] nil
562
+ #
563
+ def mouseClicked(&block)
564
+ @view__.mouseClicked = block
565
+ nil
566
+ end
567
+
568
+ # Defines touchStarted block.
569
+ #
570
+ # @example Print touches on touch start
571
+ # sprite.touchStarted do
572
+ # p sprite.touches
573
+ # end
574
+ #
575
+ # @return [nil] nil
576
+ #
577
+ def touchStarted(&block)
578
+ @view__.touchStarted = block
579
+ nil
580
+ end
581
+
582
+ # Defines touchEnded block.
583
+ #
584
+ # @example Print touches on touch end
585
+ # sprite.touchEnded do
586
+ # p sprite.touches
587
+ # end
588
+ #
589
+ # @return [nil] nil
590
+ #
591
+ def touchEnded(&block)
592
+ @view__.touchEnded = block
593
+ nil
594
+ end
595
+
596
+ # Defines touchMoved block.
597
+ #
598
+ # @example Print touches on touch move
599
+ # sprite.touchMoved do
600
+ # p sprite.touches
601
+ # end
602
+ #
603
+ # @return [nil] nil
604
+ #
605
+ def touchMoved(&block)
606
+ @view__.touchMoved = block
607
+ nil
608
+ end
609
+
335
610
  # Defines contact block.
336
611
  #
337
612
  # @example Score increases when the player sprite touches a coin
@@ -347,7 +622,7 @@ module RubySketch
347
622
 
348
623
  # Defines contact? block.
349
624
  #
350
- # @example only collide with an enemy
625
+ # @example Only collide with an enemy
351
626
  # playerSprite.contact? do |o|
352
627
  # o.enemy?
353
628
  # end
@@ -358,18 +633,6 @@ module RubySketch
358
633
  @view__.will_contact = block
359
634
  end
360
635
 
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
-
373
636
  # @private
374
637
  def getInternal__()
375
638
  @view__
@@ -381,28 +644,106 @@ module RubySketch
381
644
  # @private
382
645
  class SpriteView < Reflex::View
383
646
 
384
- attr_accessor :update, :contact, :will_contact
385
- attr_reader :sprite
647
+ attr_accessor :update,
648
+ :mousePressed, :mouseReleased, :mouseMoved, :mouseDragged, :mouseClicked,
649
+ :touchStarted, :touchEnded, :touchMoved,
650
+ :contact, :will_contact
651
+
652
+ attr_reader :sprite, :touches
386
653
 
387
654
  def initialize(sprite, *a, **k, &b)
388
655
  @sprite = sprite
389
656
  super(*a, **k, &b)
657
+
658
+ @pointerPos =
659
+ @pointerPrevPos = Rays::Point.new 0
660
+ @pointersPressed = []
661
+ @pointersReleased = []
662
+ @touches = []
663
+ end
664
+
665
+ def mouseX()
666
+ @pointerPos.x
667
+ end
668
+
669
+ def mouseY()
670
+ @pointerPos.y
671
+ end
672
+
673
+ def pmouseX()
674
+ @pointerPrevPos.x
675
+ end
676
+
677
+ def pmouseY()
678
+ @pointerPrevPos.y
679
+ end
680
+
681
+ def mouseButton()
682
+ ((@pointersPressed + @pointersReleased) & [LEFT, RIGHT, CENTER]).last
390
683
  end
391
684
 
392
685
  def on_update(e)
393
- @update.call if @update
686
+ @update&.call
687
+ end
688
+
689
+ def on_pointer_down(e)
690
+ updatePointerStates e, true
691
+ @pointerDownStartPos = @pointerPos.dup
692
+ (@touchStarted || @mousePressed)&.call
693
+ end
694
+
695
+ def on_pointer_up(e)
696
+ updatePointerStates e, false
697
+ (@touchEnded || @mouseReleased)&.call
698
+ if startPos = @pointerDownStartPos
699
+ @mouseClicked&.call if (@pointerPos - startPos).length < 3
700
+ @pointerDownStartPos = nil
701
+ end
702
+ @pointersReleased.clear
703
+ end
704
+
705
+ def on_pointer_move(e)
706
+ updatePointerStates e
707
+ (@touchMoved || (e.drag? ? @mouseDragged : @mouseMoved))&.call
708
+ end
709
+
710
+ def on_pointer_cancel(e)
711
+ on_pointer_up e
394
712
  end
395
713
 
396
714
  def on_contact(e)
397
715
  v = e.view
398
- @contact.call v.sprite, e.action if @contact && v.is_a?(SpriteView)
716
+ @contact.call v.sprite, e.action if @contact && v.respond_to?(:sprite)
399
717
  end
400
718
 
401
719
  def will_contact?(v)
402
- return true if !@will_contact || !v.is_a?(SpriteView)
720
+ return true unless @will_contact && v.respond_to?(:sprite)
403
721
  @will_contact.call v.sprite
404
722
  end
405
723
 
724
+ private
725
+
726
+ MOUSE_BUTTON_MAP = {
727
+ mouse_left: Processing::GraphicsContext::LEFT,
728
+ mouse_right: Processing::GraphicsContext::RIGHT,
729
+ mouse_middle: Processing::GraphicsContext::CENTER
730
+ }
731
+
732
+ def updatePointerStates(event, pressed = nil)
733
+ @pointerPrevPos = @pointerPos
734
+ @pointerPos = event.pos.dup
735
+ @touches = event.pointers.map {|p| Touch.new(p.id, *p.pos.to_a)}
736
+ if pressed != nil
737
+ event.types
738
+ .tap {|types| types.delete :mouse}
739
+ .map {|type| MOUSE_BUTTON_MAP[type] || type}
740
+ .each do |type|
741
+ (pressed ? @pointersPressed : @pointersReleased).push type
742
+ @pointersPressed.delete type unless pressed
743
+ end
744
+ end
745
+ end
746
+
406
747
  end# SpriteView
407
748
 
408
749
 
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.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'
31
+ s.add_runtime_dependency 'xot', '~> 0.1.35'
32
+ s.add_runtime_dependency 'rucy', '~> 0.1.35'
33
+ s.add_runtime_dependency 'beeps', '~> 0.1.36'
34
+ s.add_runtime_dependency 'rays', '~> 0.1.35'
35
+ s.add_runtime_dependency 'reflexion', '~> 0.1.36'
36
+ s.add_runtime_dependency 'processing', '~> 0.5.6'
37
37
 
38
38
  s.add_development_dependency 'rake'
39
39
  s.add_development_dependency 'test-unit'
data/test/helper.rb CHANGED
@@ -1,6 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
-
3
-
4
1
  %w[../xot ../rucy ../beeps ../rays ../reflex ../processing .]
5
2
  .map {|s| File.expand_path "../#{s}/lib", __dir__}
6
3
  .each {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
data/test/test_sprite.rb CHANGED
@@ -1,6 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
-
3
-
4
1
  require_relative 'helper'
5
2
 
6
3
 
@@ -79,6 +76,22 @@ class TestSprite < Test::Unit::TestCase
79
76
  assert_equal 2, sprite(0, 0, 1, 2).height
80
77
  end
81
78
 
79
+ def test_angle()
80
+ s = sprite
81
+ assert_equal 0, s.angle
82
+
83
+ s.angle = Math::PI
84
+ assert_in_epsilon Math::PI, s.angle
85
+ end
86
+
87
+ def test_pivot()
88
+ s = sprite
89
+ assert_each_in_epsilon [0, 0], s.pivot
90
+
91
+ s.pivot = [0.1, 0.2]
92
+ assert_each_in_epsilon [0.1, 0.2], s.pivot
93
+ end
94
+
82
95
  def test_velocity()
83
96
  s = sprite
84
97
  assert_equal vec(0, 0), s.vel
@@ -122,18 +135,44 @@ class TestSprite < Test::Unit::TestCase
122
135
  s = sprite
123
136
  assert_equal nil, s.offset
124
137
 
125
- s.offset = [1, 2]
138
+ s.offset = [1, 2]
126
139
  assert_equal vec(1, 2), s.offset
127
140
 
128
- s.offset = vec(3, 4)
141
+ s.offset = vec(3, 4)
129
142
  assert_equal vec(3, 4), s.offset
130
143
 
131
- s.offset = nil
144
+ s.offset = [5]
145
+ assert_equal vec(5, 0), s.offset
146
+
147
+ s.offset = []
148
+ assert_equal vec(0, 0), s.offset
149
+
150
+ s.offset = nil
132
151
  assert_equal nil, s.offset
133
152
 
134
153
  assert_raise(ArgumentError) {s.offset = 1}
135
154
  end
136
155
 
156
+ def test_oxoy()
157
+ s = sprite
158
+ assert_equal [0, 0], [s.ox, s.oy]
159
+ assert_equal nil, s.offset
160
+
161
+ s.ox = 1
162
+ assert_equal [1, 0], [s.ox, s.oy]
163
+ assert_equal vec(1, 0), s.offset
164
+
165
+ s = sprite
166
+
167
+ s.oy = 2
168
+ assert_equal [0, 2], [s.ox, s.oy]
169
+ assert_equal vec(0, 2), s.offset
170
+
171
+ s.offset = nil
172
+ assert_equal [0, 0], [s.ox, s.oy]
173
+ assert_equal nil, s.offset
174
+ end
175
+
137
176
  def test_dynamic?()
138
177
  s = sprite
139
178
  assert_equal false, s.dynamic?
@@ -147,10 +186,10 @@ class TestSprite < Test::Unit::TestCase
147
186
 
148
187
  def test_density()
149
188
  s = sprite
150
- assert_equal 0, s.dens
151
-
152
- s.dens = 1
153
189
  assert_equal 1, s.dens
190
+
191
+ s.dens = 2
192
+ assert_equal 2, s.dens
154
193
  end
155
194
 
156
195
  def test_friction()
@@ -192,4 +231,8 @@ class TestSprite < Test::Unit::TestCase
192
231
  assert_not_nil v.will_contact
193
232
  end
194
233
 
234
+ def test_inspect()
235
+ assert_match %r|#<RubySketch::Sprite:0x\w{16}>|, sprite.inspect
236
+ end
237
+
195
238
  end# TestSprite
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysketch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.5.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - xordog
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-30 00:00:00.000000000 Z
11
+ date: 2023-05-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: xot
@@ -16,84 +16,84 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.1.34
19
+ version: 0.1.35
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.1.34
26
+ version: 0.1.35
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rucy
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.1.34
33
+ version: 0.1.35
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.1.34
40
+ version: 0.1.35
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: beeps
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 0.1.35
47
+ version: 0.1.36
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 0.1.35
54
+ version: 0.1.36
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rays
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 0.1.34
61
+ version: 0.1.35
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 0.1.34
68
+ version: 0.1.35
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: reflexion
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 0.1.35
75
+ version: 0.1.36
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 0.1.35
82
+ version: 0.1.36
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: processing
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: 0.5.5
89
+ version: 0.5.6
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: 0.5.5
96
+ version: 0.5.6
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rake
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -156,13 +156,13 @@ files:
156
156
  - RubySketch.podspec
157
157
  - VERSION
158
158
  - examples/hello.rb
159
+ - examples/physics.rb
160
+ - examples/sprite.rb
159
161
  - lib/rubysketch.rb
160
162
  - lib/rubysketch/all.rb
161
163
  - lib/rubysketch/context.rb
162
164
  - lib/rubysketch/extension.rb
163
- - lib/rubysketch/graphics_context.rb
164
165
  - lib/rubysketch/helper.rb
165
- - lib/rubysketch/sound.rb
166
166
  - lib/rubysketch/sprite.rb
167
167
  - lib/rubysketch/window.rb
168
168
  - pod.rake
@@ -170,7 +170,6 @@ files:
170
170
  - src/RubySketch.h
171
171
  - src/RubySketch.mm
172
172
  - test/helper.rb
173
- - test/test_sound.rb
174
173
  - test/test_sprite.rb
175
174
  homepage: https://github.com/xord/rubysketch
176
175
  licenses: []
@@ -196,5 +195,4 @@ specification_version: 4
196
195
  summary: A game engine based on the Processing API.
197
196
  test_files:
198
197
  - test/helper.rb
199
- - test/test_sound.rb
200
198
  - test/test_sprite.rb
@@ -1,30 +0,0 @@
1
- module RubySketch
2
-
3
-
4
- module GraphicsContext
5
-
6
- # Draws one or more sprites.
7
- #
8
- # @param [Array<Sprite>] sprites
9
- #
10
- def sprite(*sprites)
11
- sprites.flatten! if sprites.first&.is_a? Array
12
- sprites.each do |sp|
13
- v = sp.getInternal__
14
- f, angle = v.frame, v.angle
15
- if angle == 0
16
- sp.on_draw__ f.x, f.y, f.w, f.h
17
- else
18
- pushMatrix do
19
- translate f.x, f.y
20
- rotate radians(angle)
21
- sp.on_draw__ 0, 0, f.w, f.h
22
- end
23
- end
24
- end
25
- end
26
-
27
- end# GraphicsContext
28
-
29
-
30
- end# RubySketch
@@ -1,86 +0,0 @@
1
- module RubySketch
2
-
3
-
4
- # @private
5
- class Sound
6
-
7
- # @private
8
- def initialize(sound)
9
- @sound = sound
10
- end
11
-
12
- # Play sound.
13
- #
14
- def play()
15
- old = @player
16
- @player = @sound.play
17
- old&.stop
18
- true
19
- end
20
-
21
- # Pause sound.
22
- #
23
- def pause()
24
- return false unless @player
25
- @player.pause
26
- true
27
- end
28
-
29
- # Stop sound.
30
- #
31
- def stop()
32
- return false unless @player
33
- @player.stop
34
- @player = nil
35
- true
36
- end
37
-
38
- # Returns whether the sound is playing or not.
39
- #
40
- # @return [Boolean] playing or not
41
- #
42
- def playing?()
43
- @player ? @player.playing? : false
44
- end
45
-
46
- # Returns whether the sound is paused or not.
47
- #
48
- # @return [Boolean] paused or not
49
- #
50
- def paused?()
51
- @player ? @player.paused? : false
52
- end
53
-
54
- # Returns whether the sound is stopped or not.
55
- #
56
- # @return [Boolean] stopped or not
57
- #
58
- def stopped?()
59
- @player ? @player.stopped? : true
60
- end
61
-
62
- # Save the sound data to a file.
63
- #
64
- # @param [String] path file path
65
- #
66
- # @return [Sound] self
67
- #
68
- def save(path)
69
- @sound.save path
70
- end
71
-
72
- # Load a sound file.
73
- #
74
- # @param [String] path file path
75
- #
76
- # @return [Sound] sound object
77
- #
78
- def self.load(path)
79
- f = Beeps::FileIn.new path
80
- self.new Beeps::Sound.new(f, f.seconds, nchannels: f.nchannels)
81
- end
82
-
83
- end# Sound
84
-
85
-
86
- end# RubySketch
data/test/test_sound.rb DELETED
@@ -1,88 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
-
4
- require_relative 'helper'
5
-
6
-
7
- class TestSound < Test::Unit::TestCase
8
-
9
- RS = RubySketch
10
- B = Beeps
11
-
12
- PATH = 'test.wav'
13
-
14
- def sound()
15
- RS::Sound.load PATH
16
- end
17
-
18
- def setup()
19
- B::Sound.new(B::Oscillator.new >> B::Gain.new(gain: 0), 0.1).save PATH
20
- end
21
-
22
- def teardown()
23
- B::SoundPlayer.stop_all
24
- File.delete PATH if File.exist?(PATH)
25
- end
26
-
27
- def test_play()
28
- s = sound
29
- assert_equal [false, false, true], [s.playing?, s.paused?, s.stopped?]
30
- s.play
31
- assert_equal [true, false, false], [s.playing?, s.paused?, s.stopped?]
32
- end
33
-
34
- def test_pause()
35
- s = sound
36
- s.play
37
- assert_equal [true, false, false], [s.playing?, s.paused?, s.stopped?]
38
- s.pause
39
- assert_equal [false, true, false], [s.playing?, s.paused?, s.stopped?]
40
- end
41
-
42
- def test_stop()
43
- s = sound
44
- s.play
45
- assert_equal [true, false, false], [s.playing?, s.paused?, s.stopped?]
46
- s.stop
47
- assert_equal [false, false, true], [s.playing?, s.paused?, s.stopped?]
48
- end
49
-
50
- def test_play_end_then_stop()
51
- s = sound
52
- s.play
53
- assert_equal [true, false, false], [s.playing?, s.paused?, s.stopped?]
54
- sleep 0.2
55
- assert_equal [false, false, true], [s.playing?, s.paused?, s.stopped?]
56
- end
57
-
58
- def test_play_after_pause()
59
- s = sound
60
- s.play
61
- s.pause
62
- assert_equal [false, true, false], [s.playing?, s.paused?, s.stopped?]
63
- s.play
64
- assert_equal [true, false, false], [s.playing?, s.paused?, s.stopped?]
65
- end
66
-
67
- def test_stop_after_pause()
68
- s = sound
69
- s.play
70
- s.pause
71
- assert_equal [false, true, false], [s.playing?, s.paused?, s.stopped?]
72
- s.stop
73
- assert_equal [false, false, true], [s.playing?, s.paused?, s.stopped?]
74
- end
75
-
76
- def test_save_load()
77
- s = sound
78
-
79
- File.delete PATH
80
- assert_false File.exist? PATH
81
-
82
- s.save PATH
83
- assert_true File.exist? PATH
84
-
85
- assert_nothing_raised {RS::Sound.load(PATH).play}
86
- end
87
-
88
- end# TestSound