Class: RubySketch::Sprite
- Inherits:
-
Object
- Object
- RubySketch::Sprite
- Includes:
- Xot::Inspectable
- Defined in:
- lib/rubysketch/sprite.rb
Overview
Sprite object.
Instance Method Summary collapse
-
#angle ⇒ Numeric
Returns the rotation angle of sprite.
-
#angle=(angle) ⇒ Numeric
Sets the rotation angle of sprite.
-
#center ⇒ Vector
Returns the center position of the sprite.
-
#center=(arg) ⇒ Vector
Sets the center position of the sprite.
-
#clickCount ⇒ Numeric
Returns the mouse button click count on the sprite.
-
#contact(&block) ⇒ nil
Defines contact block.
-
#contact?(&block) ⇒ nil
Defines contact? block.
-
#density ⇒ Numeric
(also: #dens)
Returns the density of the sprite.
-
#density=(n) ⇒ Numeric
(also: #dens=)
Sets the density of the sprite.
-
#draw(&block) ⇒ nil
Defines draw block.
-
#dynamic=(bool) ⇒ Boolean
Sets whether the sprite is movable by the physics engine.
-
#dynamic? ⇒ Boolean
Returns whether the sprite is movable by the physics engine.
-
#friction ⇒ Numeric
(also: #fric)
Returns the friction of the sprite.
-
#friction=(n) ⇒ Numeric
(also: #fric=)
Sets the friction of the sprite.
-
#from_screen(vec) ⇒ Vector
Converts a vector from the screen coordinate.
-
#height ⇒ Numeric
(also: #h)
Returns the height of the sprite.
-
#height=(h) ⇒ Numeric
(also: #h=)
Sets the height of the sprite.
-
#image ⇒ Image
Returns the image of the sprite.
-
#image=(img) ⇒ Image
Sets the sprite image.
-
#initialize(x = 0, y = 0, w = nil, h = nil, image: nil, offset: nil, context: nil) ⇒ Sprite
constructor
Initialize sprite object.
-
#mouseButton ⇒ LEFT, ...
Returns the mouse button clicked on the sprite.
-
#mouseClicked(&block) ⇒ nil
Defines mouseClicked block.
-
#mouseDragged(&block) ⇒ nil
Defines mouseDragged block.
-
#mouseMoved(&block) ⇒ nil
Defines mouseMoved block.
-
#mousePressed(&block) ⇒ nil
Defines mousePressed block.
-
#mouseReleased(&block) ⇒ nil
Defines mouseReleased block.
-
#mouseX ⇒ Numeric
Returns the x-position of the mouse in the sprite coordinates.
-
#mouseY ⇒ Numeric
Returns the y-position of the mouse in the sprite coordinates.
-
#offset ⇒ Vector
Returns the offset of the sprite image.
-
#offset=(arg) ⇒ Vector
Sets the offset of the sprite image.
-
#ox ⇒ Numeric
Returns the x-axis offset of the sprite image.
-
#ox=(n) ⇒ Numeric
Sets the x-axis offset of the sprite image.
-
#oy ⇒ Numeric
Returns the y-axis offset of the sprite image.
-
#oy=(n) ⇒ Numeric
Sets the y-axis offset of the sprite image.
-
#pivot ⇒ Array<Numeric>
Returns the rotation center of sprite.
-
#pivot=(array) ⇒ Array<Numeric>
Sets the rotation center of sprite.
-
#pmouseX ⇒ Numeric
Returns the previous x-position of the mouse in the sprite coordinates.
-
#pmouseY ⇒ Numeric
Returns the previous y-position of the mouse in the sprite coordinates.
-
#position ⇒ Vector
(also: #pos)
Returns the position of the sprite.
-
#position=(arg) ⇒ Vector
(also: #pos=)
Sets the position of the sprite.
-
#restitution ⇒ Numeric
(also: #rest)
Returns the restitution of the sprite.
-
#restitution=(n) ⇒ Numeric
(also: #rest=)
Sets the restitution of the sprite.
-
#size ⇒ Vector
Returns the size of the sprite.
-
#size=(arg) ⇒ Vector
Returns the size of the sprite.
-
#to_screen(vec) ⇒ Vector
Converts a vector to the screen coordinate.
-
#touchEnded(&block) ⇒ nil
Defines touchEnded block.
-
#touches ⇒ Array<Touch>
Returns the touch objects touched on the sprite.
-
#touchMoved(&block) ⇒ nil
Defines touchMoved block.
-
#touchStarted(&block) ⇒ nil
Defines touchStarted block.
-
#update(&block) ⇒ nil
Defines update block.
-
#velocity ⇒ Vector
(also: #vel)
Returns the velocity of the sprite.
-
#velocity=(arg) ⇒ Vector
(also: #vel=)
Sets the velocity of the sprite.
-
#vx ⇒ Numeric
Returns the x-axis velocity of the sprite.
-
#vx=(n) ⇒ Numeric
Sets the x-axis velocity of the sprite.
-
#vy ⇒ Numeric
Returns the y-axis velocity of the sprite.
-
#vy=(n) ⇒ Numeric
Sets the y-axis velocity of the sprite.
-
#width ⇒ Numeric
(also: #w)
Returns the width of the sprite.
-
#width=(w) ⇒ Numeric
(also: #w=)
Sets the width of the sprite.
-
#x ⇒ Numeric
Returns the x-coordinate position of the sprite.
-
#x=(n) ⇒ Numeric
Set the x-coordinate position of the sprite.
-
#y ⇒ Numeric
Returns the y-coordinate position of the sprite.
-
#y=(n) ⇒ Numeric
Set the y-coordinate position of the sprite.
-
#z ⇒ Numeric
Returns the z-coordinate position of the sprite.
-
#z=(n) ⇒ Numeric
Set the z-coordinate position of the sprite.
Constructor Details
#new(image: img) ⇒ Sprite #new(x, y, image: img) ⇒ Sprite #new(x, y, w, h) ⇒ Sprite #new(x, y, w, h, image: img, offset: off) ⇒ Sprite
Initialize sprite object.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rubysketch/sprite.rb', line 38 def initialize( x = 0, y = 0, w = nil, h = nil, image: nil, offset: nil, context: nil) w ||= (image&.width || 0) h ||= (image&.height || 0) raise 'invalid size' unless w >= 0 && h >= 0 raise 'invalid image' if image && !image.getInternal__.is_a?(Rays::Image) @context__ = context || Context.context__ @view__ = SpriteView.new( self, x: x, y: y, w: w, h: h, static: true, density: 1, friction: 0, restitution: 0, back: :white) self.image = image if image self.offset = offset if offset end |
Instance Method Details
#angle ⇒ Numeric
Returns the rotation angle of sprite.
226 227 228 229 |
# File 'lib/rubysketch/sprite.rb', line 226 def angle() a, c = @view__.angle, @context__ c ? c.fromDegrees__(a) : a * Processing::GraphicsContext::DEG2RAD__ end |
#angle=(angle) ⇒ Numeric
Sets the rotation angle of sprite.
237 238 239 240 241 242 |
# File 'lib/rubysketch/sprite.rb', line 237 def angle=(angle) c = @context__ @view__.angle = c ? c.toAngle__(angle) : angle * Processing::GraphicsContext::RAD2DEG__ angle end |
#center ⇒ Vector
Returns the center position of the sprite.
144 145 146 |
# File 'lib/rubysketch/sprite.rb', line 144 def center() Vector.new(x + w / 2, y + h / 2, z) end |
#center=(vec) ⇒ Vector #center=(ary) ⇒ Vector
Sets the center position of the sprite.
158 159 160 161 162 |
# File 'lib/rubysketch/sprite.rb', line 158 def center=(arg) x, y = *(arg.is_a?(Vector) ? arg.getInternal__.to_a : arg) self.pos = [x - w / 2, y - h / 2, z] self.center end |
#clickCount ⇒ Numeric
Returns the mouse button click count on the sprite.
559 560 561 |
# File 'lib/rubysketch/sprite.rb', line 559 def clickCount() @view__.clickCount end |
#contact(&block) ⇒ nil
Defines contact block.
722 723 724 |
# File 'lib/rubysketch/sprite.rb', line 722 def contact(&block) @view__.contact = block end |
#contact?(&block) ⇒ nil
Defines contact? block.
735 736 737 |
# File 'lib/rubysketch/sprite.rb', line 735 def contact?(&block) @view__.will_contact = block end |
#density ⇒ Numeric Also known as: dens
Returns the density of the sprite.
435 436 437 |
# File 'lib/rubysketch/sprite.rb', line 435 def density() @view__.density end |
#density=(n) ⇒ Numeric Also known as: dens=
Sets the density of the sprite.
445 446 447 448 |
# File 'lib/rubysketch/sprite.rb', line 445 def density=(n) @view__.density = n n end |
#draw(&block) ⇒ nil
Defines draw block.
596 597 598 599 |
# File 'lib/rubysketch/sprite.rb', line 596 def draw(&block) @drawBlock__ = block nil end |
#dynamic=(bool) ⇒ Boolean
Sets whether the sprite is movable by the physics engine.
426 427 428 429 |
# File 'lib/rubysketch/sprite.rb', line 426 def dynamic=(bool) @view__.dynamic = bool bool end |
#dynamic? ⇒ Boolean
Returns whether the sprite is movable by the physics engine.
416 417 418 |
# File 'lib/rubysketch/sprite.rb', line 416 def dynamic?() @view__.dynamic? end |
#friction ⇒ Numeric Also known as: fric
Returns the friction of the sprite.
454 455 456 |
# File 'lib/rubysketch/sprite.rb', line 454 def friction() @view__.friction end |
#friction=(n) ⇒ Numeric Also known as: fric=
Sets the friction of the sprite.
464 465 466 467 |
# File 'lib/rubysketch/sprite.rb', line 464 def friction=(n) @view__.friction = n n end |
#from_screen(vec) ⇒ Vector
Converts a vector from the screen coordinate
501 502 503 |
# File 'lib/rubysketch/sprite.rb', line 501 def from_screen(vec) @view__.from_parent(vec.getInternal__).toVector end |
#height ⇒ Numeric Also known as: h
Returns the height of the sprite.
203 204 205 |
# File 'lib/rubysketch/sprite.rb', line 203 def height() @view__.height end |
#height=(h) ⇒ Numeric Also known as: h=
Sets the height of the sprite.
213 214 215 |
# File 'lib/rubysketch/sprite.rb', line 213 def height=(h) @view__.height = h end |
#image ⇒ Image
Returns the image of the sprite.
332 333 334 |
# File 'lib/rubysketch/sprite.rb', line 332 def image() @image__ end |
#image=(img) ⇒ Image
Sets the sprite image.
342 343 344 |
# File 'lib/rubysketch/sprite.rb', line 342 def image=(img) @image__ = img end |
#mouseButton ⇒ LEFT, ...
Returns the mouse button clicked on the sprite.
551 552 553 |
# File 'lib/rubysketch/sprite.rb', line 551 def mouseButton() @view__.mouseButton end |
#mouseClicked(&block) ⇒ nil
Defines mouseClicked block.
666 667 668 669 |
# File 'lib/rubysketch/sprite.rb', line 666 def mouseClicked(&block) @view__.mouseClicked = block nil end |
#mouseDragged(&block) ⇒ nil
Defines mouseDragged block.
652 653 654 655 |
# File 'lib/rubysketch/sprite.rb', line 652 def mouseDragged(&block) @view__.mouseDragged = block nil end |
#mouseMoved(&block) ⇒ nil
Defines mouseMoved block.
638 639 640 641 |
# File 'lib/rubysketch/sprite.rb', line 638 def mouseMoved(&block) @view__.mouseMoved = block nil end |
#mousePressed(&block) ⇒ nil
Defines mousePressed block.
610 611 612 613 |
# File 'lib/rubysketch/sprite.rb', line 610 def mousePressed(&block) @view__.mousePressed = block nil end |
#mouseReleased(&block) ⇒ nil
Defines mouseReleased block.
624 625 626 627 |
# File 'lib/rubysketch/sprite.rb', line 624 def mouseReleased(&block) @view__.mouseReleased = block nil end |
#mouseX ⇒ Numeric
Returns the x-position of the mouse in the sprite coordinates.
519 520 521 |
# File 'lib/rubysketch/sprite.rb', line 519 def mouseX() @view__.mouseX end |
#mouseY ⇒ Numeric
Returns the y-position of the mouse in the sprite coordinates.
527 528 529 |
# File 'lib/rubysketch/sprite.rb', line 527 def mouseY() @view__.mouseY end |
#offset ⇒ Vector
Returns the offset of the sprite image.
350 351 352 |
# File 'lib/rubysketch/sprite.rb', line 350 def offset() @offset__ end |
#offset=(vec) ⇒ Vector #velocity=(ary) ⇒ Vector
Sets the offset of the sprite image.
364 365 366 367 368 369 370 371 372 373 |
# File 'lib/rubysketch/sprite.rb', line 364 def offset=(arg) @offset__ = case arg when Vector then arg when Array then Vector.new(arg[0] || 0, arg[1] || 0) when nil then nil else raise ArgumentError end @offset__ end |
#ox ⇒ Numeric
Returns the x-axis offset of the sprite image.
379 380 381 |
# File 'lib/rubysketch/sprite.rb', line 379 def ox() @offset__&.x || 0 end |
#ox=(n) ⇒ Numeric
Sets the x-axis offset of the sprite image.
389 390 391 392 |
# File 'lib/rubysketch/sprite.rb', line 389 def ox=(n) self.offset = [n, oy] n end |
#oy ⇒ Numeric
Returns the y-axis offset of the sprite image.
398 399 400 |
# File 'lib/rubysketch/sprite.rb', line 398 def oy() @offset__&.y || 0 end |
#oy=(n) ⇒ Numeric
Sets the y-axis offset of the sprite image.
408 409 410 |
# File 'lib/rubysketch/sprite.rb', line 408 def oy=(n) self.offset = [ox, n] end |
#pivot ⇒ Array<Numeric>
Returns the rotation center of sprite.
248 249 250 |
# File 'lib/rubysketch/sprite.rb', line 248 def pivot() @view__.pivot.to_a[0, 2] end |
#pivot=(array) ⇒ Array<Numeric>
Sets the rotation center of sprite.
- 0.0, 0.0
-
is the left-top, [1.0, 1.0] is the right-bottom, and [0.5, 0.5] is the center.
259 260 261 262 |
# File 'lib/rubysketch/sprite.rb', line 259 def pivot=(array) @view__.pivot = array pivot end |
#pmouseX ⇒ Numeric
Returns the previous x-position of the mouse in the sprite coordinates.
535 536 537 |
# File 'lib/rubysketch/sprite.rb', line 535 def pmouseX() @view__.pmouseX end |
#pmouseY ⇒ Numeric
Returns the previous y-position of the mouse in the sprite coordinates.
543 544 545 |
# File 'lib/rubysketch/sprite.rb', line 543 def pmouseY() @view__.pmouseY end |
#position ⇒ Vector Also known as: pos
Returns the position of the sprite.
61 62 63 |
# File 'lib/rubysketch/sprite.rb', line 61 def position() @view__.position.toVector end |
#position=(vec) ⇒ Vector #position=(ary) ⇒ Vector Also known as: pos=
Sets the position of the sprite.
75 76 77 78 |
# File 'lib/rubysketch/sprite.rb', line 75 def position=(arg) @view__.position = arg.is_a?(Vector) ? arg.getInternal__ : arg arg end |
#restitution ⇒ Numeric Also known as: rest
Returns the restitution of the sprite.
473 474 475 |
# File 'lib/rubysketch/sprite.rb', line 473 def restitution() @view__.restitution end |
#restitution=(n) ⇒ Numeric Also known as: rest=
Sets the restitution of the sprite.
483 484 485 486 |
# File 'lib/rubysketch/sprite.rb', line 483 def restitution=(n) @view__.restitution = n n end |
#size ⇒ Vector
Returns the size of the sprite.
168 169 170 |
# File 'lib/rubysketch/sprite.rb', line 168 def size() @view__.size.toVector end |
#size=(arg) ⇒ Vector
Returns the size of the sprite.
176 177 178 179 |
# File 'lib/rubysketch/sprite.rb', line 176 def size=(arg) @view__.size = arg.is_a?(Vector) ? arg.getInternal__ : arg arg end |
#to_screen(vec) ⇒ Vector
Converts a vector to the screen coordinate
511 512 513 |
# File 'lib/rubysketch/sprite.rb', line 511 def to_screen(vec) @view__.to_parent(vec.getInternal__).toVector end |
#touchEnded(&block) ⇒ nil
Defines touchEnded block.
694 695 696 697 |
# File 'lib/rubysketch/sprite.rb', line 694 def touchEnded(&block) @view__.touchEnded = block nil end |
#touches ⇒ Array<Touch>
Returns the touch objects touched on the sprite.
567 568 569 |
# File 'lib/rubysketch/sprite.rb', line 567 def touches() @view__.touches end |
#touchMoved(&block) ⇒ nil
Defines touchMoved block.
708 709 710 711 |
# File 'lib/rubysketch/sprite.rb', line 708 def touchMoved(&block) @view__.touchMoved = block nil end |
#touchStarted(&block) ⇒ nil
Defines touchStarted block.
680 681 682 683 |
# File 'lib/rubysketch/sprite.rb', line 680 def touchStarted(&block) @view__.touchStarted = block nil end |
#update(&block) ⇒ nil
Defines update block.
580 581 582 583 |
# File 'lib/rubysketch/sprite.rb', line 580 def update(&block) @view__.update = block nil end |
#velocity ⇒ Vector Also known as: vel
Returns the velocity of the sprite.
268 269 270 |
# File 'lib/rubysketch/sprite.rb', line 268 def velocity() @view__.velocity.toVector end |
#velocity=(vec) ⇒ Vector #velocity=(ary) ⇒ Vector Also known as: vel=
Sets the velocity of the sprite.
282 283 284 285 |
# File 'lib/rubysketch/sprite.rb', line 282 def velocity=(arg) @view__.velocity = arg.is_a?(Vector) ? arg.getInternal__ : arg arg end |
#vx ⇒ Numeric
Returns the x-axis velocity of the sprite.
291 292 293 |
# File 'lib/rubysketch/sprite.rb', line 291 def vx() @view__.velocity.x end |
#vx=(n) ⇒ Numeric
Sets the x-axis velocity of the sprite.
301 302 303 304 |
# File 'lib/rubysketch/sprite.rb', line 301 def vx=(n) @view__.velocity = @view__.velocity.tap {|v| v.x = n} n end |
#vy ⇒ Numeric
Returns the y-axis velocity of the sprite.
310 311 312 |
# File 'lib/rubysketch/sprite.rb', line 310 def vy() @view__.velocity.y end |
#vy=(n) ⇒ Numeric
Sets the y-axis velocity of the sprite.
320 321 322 323 |
# File 'lib/rubysketch/sprite.rb', line 320 def vy=(n) @view__.velocity = @view__.velocity.tap {|v| v.y = n} n end |
#width ⇒ Numeric Also known as: w
Returns the width of the sprite.
185 186 187 |
# File 'lib/rubysketch/sprite.rb', line 185 def width() @view__.width end |
#width=(w) ⇒ Numeric Also known as: w=
Sets the width of the sprite.
195 196 197 |
# File 'lib/rubysketch/sprite.rb', line 195 def width=(w) @view__.width = w end |
#x ⇒ Numeric
Returns the x-coordinate position of the sprite.
84 85 86 |
# File 'lib/rubysketch/sprite.rb', line 84 def x() @view__.x end |
#x=(n) ⇒ Numeric
Set the x-coordinate position of the sprite.
94 95 96 97 |
# File 'lib/rubysketch/sprite.rb', line 94 def x=(n) @view__.x = n n end |
#y ⇒ Numeric
Returns the y-coordinate position of the sprite.
103 104 105 |
# File 'lib/rubysketch/sprite.rb', line 103 def y() @view__.y end |
#y=(n) ⇒ Numeric
Set the y-coordinate position of the sprite.
113 114 115 116 |
# File 'lib/rubysketch/sprite.rb', line 113 def y=(n) @view__.y = n n end |
#z ⇒ Numeric
Returns the z-coordinate position of the sprite.
122 123 124 |
# File 'lib/rubysketch/sprite.rb', line 122 def z() @view__.z end |
#z=(n) ⇒ Numeric
Set the z-coordinate position of the sprite.
132 133 134 135 |
# File 'lib/rubysketch/sprite.rb', line 132 def z=(n) @view__.z = n n end |