Class: RubySketch::SpriteWorld
- Inherits:
-
Object
- Object
- RubySketch::SpriteWorld
- Defined in:
- lib/rubysketch/sprite.rb
Overview
A class Manages sprites.
Instance Method Summary collapse
-
#addSprite(sprite, deprecatedSprite__ = nil, to: nil) ⇒ Sprite
Adds sprite to the physics engine.
-
#bottom ⇒ Numeric
Returns the bottom position of the world.
-
#bottom=(n) ⇒ Numeric
Set the bottom position of the world.
-
#center ⇒ Vector
Returns the center position of the world.
-
#center=(arg) ⇒ Vector
Sets the center position of the world.
-
#createSprite(*args, klass: nil, **kwargs) ⇒ Sprite
Creates a new sprite and add it to physics engine.
- #debug=(state) ⇒ Object
- #debug? ⇒ Boolean
-
#gravity(*args) ⇒ nil
Sets gravity for the physics engine.
-
#height ⇒ Numeric
(also: #h)
Returns the height of the world.
-
#height=(h) ⇒ Numeric
(also: #h=)
Sets the height of the world.
-
#initialize(pixelsPerMeter: 0) ⇒ SpriteWorld
constructor
Create a new physics world.
-
#left ⇒ Numeric
Returns the left position of the world.
-
#left=(n) ⇒ Numeric
Set the left position of the world.
-
#offset ⇒ Vector
Returns the offset of the world.
-
#offset=(arg) ⇒ Vector
Sets the offset of the world.
-
#ox ⇒ Numeric
Returns the x-axis offset of the world.
-
#ox=(x) ⇒ Numeric
Sets the x-axis offset of the world.
-
#oy ⇒ Numeric
Returns the y-axis offset of the world.
-
#oy=(y) ⇒ Numeric
Sets the y-axis offset of the world.
-
#position ⇒ Vector
(also: #pos)
Returns the position of the world.
-
#position=(arg) ⇒ Vector
(also: #pos=)
Sets the position of the world.
-
#removeSprite(sprite, deprecatedSprite__ = nil, from: nil) ⇒ Sprite
Removes sprite from the physics engine.
-
#right ⇒ Numeric
Returns the right position of the world.
-
#right=(n) ⇒ Numeric
Set the right position of the world.
-
#size ⇒ Vector
Returns the size of the world.
-
#size=(arg) ⇒ Vector
Sets the size of the world.
-
#top ⇒ Numeric
Returns the top position of the world.
-
#top=(n) ⇒ Numeric
Set the top position of the world.
-
#width ⇒ Numeric
(also: #w)
Returns the width of the world.
-
#width=(w) ⇒ Numeric
(also: #w=)
Sets the width of the world.
-
#x ⇒ Numeric
Returns the x-coordinate position of the world.
-
#x=(n) ⇒ Numeric
Set the x-coordinate position of the world.
-
#y ⇒ Numeric
Returns the y-coordinate position of the world.
-
#y=(n) ⇒ Numeric
Set the y-coordinate position of the world.
-
#z ⇒ Numeric
Returns the z-coordinate position of the world.
-
#z=(n) ⇒ Numeric
Set the z-coordinate position of the world.
-
#zoom ⇒ Numeric
Returns the zoom value of the world.
-
#zoom=(zoom) ⇒ Numeric
Sets the zoom value of the world.
Constructor Details
#initialize(pixelsPerMeter: 0) ⇒ SpriteWorld
Create a new physics world.
1211 1212 1213 1214 |
# File 'lib/rubysketch/sprite.rb', line 1211 def initialize(pixelsPerMeter: 0) @context, @sprites, @debug = nil, [], false @view = View.new pixelsPerMeter: pixelsPerMeter end |
Instance Method Details
#addSprite(sprite, deprecatedSprite__ = nil, to: nil) ⇒ Sprite
Adds sprite to the physics engine.
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 |
# File 'lib/rubysketch/sprite.rb', line 1276 def addSprite(sprite, deprecatedSprite__ = nil, to: nil) if sprite.is_a?(Array) Xot.warn "addSprite(array, sprite) is deprecated; use addSprite(sprite, to: array) instead", uniq: true sprite, to = deprecatedSprite__, sprite end raise ArgumentError if sprite.getWorld__ @view.add sprite.getInternal__ @sprites.push sprite to.push sprite if to sprite.setWorld__ self sprite end |
#bottom ⇒ Numeric
Returns the bottom position of the world.
1472 1473 1474 |
# File 'lib/rubysketch/sprite.rb', line 1472 def bottom() @view.bottom end |
#bottom=(n) ⇒ Numeric
Set the bottom position of the world.
1482 1483 1484 |
# File 'lib/rubysketch/sprite.rb', line 1482 def bottom=(n) @view.bottom = n end |
#center ⇒ Vector
Returns the center position of the world.
1490 1491 1492 |
# File 'lib/rubysketch/sprite.rb', line 1490 def center() Vector.new(x + w / 2, y + h / 2, z) end |
#center=(vec) ⇒ Vector #center=(ary) ⇒ Vector
Sets the center position of the world.
1504 1505 1506 1507 1508 |
# File 'lib/rubysketch/sprite.rb', line 1504 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 |
#createSprite(x, y, w, h) ⇒ Sprite #createSprite(image: img) ⇒ Sprite #createSprite(x, y, image: img) ⇒ Sprite #createSprite(x, y, image: img, offset: off) ⇒ Sprite #createSprite(x, y, image: img, shape: shp) ⇒ Sprite #createSprite(x, y, image: img, offset: off, shape: shp) ⇒ Sprite #createSprite(x, y, shape: shp) ⇒ Sprite
Creates a new sprite and add it to physics engine.
1264 1265 1266 1267 |
# File 'lib/rubysketch/sprite.rb', line 1264 def createSprite(*args, klass: nil, **kwargs) klass ||= RubySketch::Sprite addSprite klass.new(*args, **kwargs) end |
#debug=(state) ⇒ Object
1656 1657 1658 |
# File 'lib/rubysketch/sprite.rb', line 1656 def debug=(state) @view.debug = state end |
#debug? ⇒ Boolean
1660 |
# File 'lib/rubysketch/sprite.rb', line 1660 def debug? = @view.debug? |
#gravity(vec) ⇒ nil #gravity(ary) ⇒ nil #gravity(x, y) ⇒ nil
Sets gravity for the physics engine.
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 |
# File 'lib/rubysketch/sprite.rb', line 1323 def gravity(*args) x, y = case arg = args.first when Vector then arg.array when Array then arg else args end @view.gravity x, y nil end |
#height ⇒ Numeric Also known as: h
Returns the height of the world.
1549 1550 1551 |
# File 'lib/rubysketch/sprite.rb', line 1549 def height() @view.height end |
#height=(h) ⇒ Numeric Also known as: h=
Sets the height of the world.
1559 1560 1561 |
# File 'lib/rubysketch/sprite.rb', line 1559 def height=(h) @view.height = h end |
#left ⇒ Numeric
Returns the left position of the world.
1418 1419 1420 |
# File 'lib/rubysketch/sprite.rb', line 1418 def left() @view.left end |
#left=(n) ⇒ Numeric
Set the left position of the world.
1428 1429 1430 |
# File 'lib/rubysketch/sprite.rb', line 1428 def left=(n) @view.left = n end |
#offset ⇒ Vector
Returns the offset of the world.
1572 1573 1574 1575 |
# File 'lib/rubysketch/sprite.rb', line 1572 def offset() s, z = @view.scroll, zoom Vector.new(-s.x / z, -s.y / z, -s.z / z) end |
#offset=(vec) ⇒ Vector #offset=(ary) ⇒ Vector
Sets the offset of the world.
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 |
# File 'lib/rubysketch/sprite.rb', line 1587 def offset=(arg) zoom_ = zoom x, y, z = case arg when Vector then [arg.x, arg.y, arg.z] when Array then [arg[0] || 0, arg[1] || 0, arg[2] || 0] when nil then [0, 0, 0] else raise ArgumentError end @view.scroll_to(-x * zoom_, -y * zoom_, -z * zoom_) offset end |
#ox ⇒ Numeric
Returns the x-axis offset of the world.
1604 1605 1606 |
# File 'lib/rubysketch/sprite.rb', line 1604 def ox() offset.x end |
#ox=(x) ⇒ Numeric
Sets the x-axis offset of the world.
1614 1615 1616 1617 1618 |
# File 'lib/rubysketch/sprite.rb', line 1614 def ox=(x) o = offset o.x = x self.offset = o end |
#oy ⇒ Numeric
Returns the y-axis offset of the world.
1624 1625 1626 |
# File 'lib/rubysketch/sprite.rb', line 1624 def oy() offset.y end |
#oy=(y) ⇒ Numeric
Sets the y-axis offset of the world.
1634 1635 1636 1637 1638 |
# File 'lib/rubysketch/sprite.rb', line 1634 def oy=(y) o = offset o.y = y self.offset = o end |
#position ⇒ Vector Also known as: pos
Returns the position of the world.
1338 1339 1340 |
# File 'lib/rubysketch/sprite.rb', line 1338 def position() @view.position.toVector end |
#position=(vec) ⇒ Vector #position=(ary) ⇒ Vector Also known as: pos=
Sets the position of the world.
1352 1353 1354 1355 |
# File 'lib/rubysketch/sprite.rb', line 1352 def position=(arg) @view.position = arg.is_a?(Vector) ? arg.getInternal__ : arg arg end |
#removeSprite(sprite, deprecatedSprite__ = nil, from: nil) ⇒ Sprite
Removes sprite from the physics engine.
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 |
# File 'lib/rubysketch/sprite.rb', line 1296 def removeSprite(sprite, deprecatedSprite__ = nil, from: nil) if sprite.is_a?(Array) Xot.warn "removeSprite(array, sprite) is deprecated; use removeSprite(sprite, from: array) instead", uniq: true sprite, from = deprecatedSprite__, sprite end raise ArgumentError if sprite.getWorld__ != self @view.remove sprite.getInternal__ @sprites.delete sprite from.delete sprite if from sprite.setWorld__ nil sprite end |
#right ⇒ Numeric
Returns the right position of the world.
1454 1455 1456 |
# File 'lib/rubysketch/sprite.rb', line 1454 def right() @view.right end |
#right=(n) ⇒ Numeric
Set the right position of the world.
1464 1465 1466 |
# File 'lib/rubysketch/sprite.rb', line 1464 def right=(n) @view.right = n end |
#size ⇒ Vector
Returns the size of the world.
1514 1515 1516 |
# File 'lib/rubysketch/sprite.rb', line 1514 def size() @view.size.toVector end |
#size=(arg) ⇒ Vector
Sets the size of the world.
1522 1523 1524 1525 |
# File 'lib/rubysketch/sprite.rb', line 1522 def size=(arg) @view.size = arg.is_a?(Vector) ? arg.getInternal__ : arg arg end |
#top ⇒ Numeric
Returns the top position of the world.
1436 1437 1438 |
# File 'lib/rubysketch/sprite.rb', line 1436 def top() @view.top end |
#top=(n) ⇒ Numeric
Set the top position of the world.
1446 1447 1448 |
# File 'lib/rubysketch/sprite.rb', line 1446 def top=(n) @view.top = n end |
#width ⇒ Numeric Also known as: w
Returns the width of the world.
1531 1532 1533 |
# File 'lib/rubysketch/sprite.rb', line 1531 def width() @view.width end |
#width=(w) ⇒ Numeric Also known as: w=
Sets the width of the world.
1541 1542 1543 |
# File 'lib/rubysketch/sprite.rb', line 1541 def width=(w) @view.width = w end |
#x ⇒ Numeric
Returns the x-coordinate position of the world.
1361 1362 1363 |
# File 'lib/rubysketch/sprite.rb', line 1361 def x() @view.x end |
#x=(n) ⇒ Numeric
Set the x-coordinate position of the world.
1371 1372 1373 |
# File 'lib/rubysketch/sprite.rb', line 1371 def x=(n) @view.x = n end |
#y ⇒ Numeric
Returns the y-coordinate position of the world.
1379 1380 1381 |
# File 'lib/rubysketch/sprite.rb', line 1379 def y() @view.y end |
#y=(n) ⇒ Numeric
Set the y-coordinate position of the world.
1389 1390 1391 |
# File 'lib/rubysketch/sprite.rb', line 1389 def y=(n) @view.y = n end |
#z ⇒ Numeric
Returns the z-coordinate position of the world.
1397 1398 1399 |
# File 'lib/rubysketch/sprite.rb', line 1397 def z() @view.z end |
#z=(n) ⇒ Numeric
Set the z-coordinate position of the world.
1407 1408 1409 |
# File 'lib/rubysketch/sprite.rb', line 1407 def z=(n) @view.z = n end |
#zoom ⇒ Numeric
Returns the zoom value of the world.
1644 1645 1646 |
# File 'lib/rubysketch/sprite.rb', line 1644 def zoom() @view.zoom end |
#zoom=(zoom) ⇒ Numeric
Sets the zoom value of the world.
1652 1653 1654 |
# File 'lib/rubysketch/sprite.rb', line 1652 def zoom=(zoom) @view.zoom = zoom end |