Class: RubySketch::SpriteWorld

Inherits:
Object
  • Object
show all
Defined in:
lib/rubysketch/sprite.rb

Overview

A class Manages sprites.

Instance Method Summary collapse

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.

Parameters:

  • sprite (Sprite)

    sprite object

  • to (Array) (defaults to: nil)

    user array to store the sprite

Returns:

  • (Sprite)

    the added sprite

Raises:

  • (ArgumentError)


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

#bottomNumeric

Returns the bottom position of the world.

Returns:

  • (Numeric)

    bottom



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.

Parameters:

  • n (Numeric)

    world bottom position

Returns:

  • (Numeric)

    world bottom position



1482
1483
1484
# File 'lib/rubysketch/sprite.rb', line 1482

def bottom=(n)
  @view.bottom = n
end

#centerVector

Returns the center position of the world.

Returns:

  • (Vector)

    center position



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.

Overloads:

  • #center=(vec) ⇒ Vector

    Parameters:

    • vec (Vector)

      center position

  • #center=(ary) ⇒ Vector

    Parameters:

    • ary (Array<Numeric>)

      an array of centerX and centerY

Returns:

  • (Vector)

    center position



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.

Overloads:

  • #createSprite(x, y, w, h) ⇒ Sprite

    pos(x, y), size: [w, h]

    Parameters:

    • x (Numeric)

      x of the sprite position

    • y (Numeric)

      y of the sprite position

    • w (Numeric)

      width of the sprite

    • h (Numeric)

      height of the sprite

  • #createSprite(image: img) ⇒ Sprite

    pos: [0, 0], size: [image.width, image.height]

    Parameters:

    • img (Image)

      sprite image

  • #createSprite(x, y, image: img) ⇒ Sprite

    pos: [x, y], size: [image.width, image.height]

    Parameters:

    • x (Numeric)

      x of the sprite position

    • y (Numeric)

      y of the sprite position

    • img (Image)

      sprite image

  • #createSprite(x, y, image: img, offset: off) ⇒ Sprite

    pos: [x, y], size: [image.width, image.height], offset: [offset.x, offset.x]

    Parameters:

    • x (Numeric)

      x of the sprite position

    • y (Numeric)

      y of the sprite position

    • img (Image)

      sprite image

    • off (Vector)

      offset of the sprite image

  • #createSprite(x, y, image: img, shape: shp) ⇒ Sprite

    pos: [x, y], size: [image.width, image.height]

    Parameters:

    • x (Numeric)

      x of the sprite position

    • y (Numeric)

      y of the sprite position

    • img (Image)

      sprite image

  • #createSprite(x, y, image: img, offset: off, shape: shp) ⇒ Sprite

    pos: [x, y], size: [image.width, image.height], offset: [offset.x, offset.x]

    Parameters:

    • x (Numeric)

      x of the sprite position

    • y (Numeric)

      y of the sprite position

    • img (Image)

      sprite image

    • off (Vector)

      offset of the sprite image

    • shp (Shape)

      shape of the sprite for physics calculations

  • #createSprite(x, y, shape: shp) ⇒ Sprite

    pos: [x, y], size: [shape.width, shape.height]

    Parameters:

    • x (Numeric)

      x of the sprite position

    • y (Numeric)

      y of the sprite position

    • shp (Shape)

      shape of the sprite for physics calculations

Returns:

  • (Sprite)

    the new sprite object



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

Returns:

  • (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.

Overloads:

  • #gravity(vec) ⇒ nil

    Parameters:

    • vec (Vector)

      gracity vector

  • #gravity(ary) ⇒ nil

    Parameters:

    • ary (Array<Numeric>)

      gravityX, gravityY

  • #gravity(x, y) ⇒ nil

    Parameters:

    • x (Numeric)

      x of gravity vector

    • y (Numeric)

      y of gracity vector

Returns:

  • (nil)

    nil



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

#heightNumeric Also known as: h

Returns the height of the world.

Returns:

  • (Numeric)

    height



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.

Parameters:

  • h (Numeric)

    height

Returns:

  • (Numeric)

    height



1559
1560
1561
# File 'lib/rubysketch/sprite.rb', line 1559

def height=(h)
  @view.height = h
end

#leftNumeric

Returns the left position of the world.

Returns:

  • (Numeric)

    left position



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.

Parameters:

  • n (Numeric)

    world left position

Returns:

  • (Numeric)

    world left position



1428
1429
1430
# File 'lib/rubysketch/sprite.rb', line 1428

def left=(n)
  @view.left = n
end

#offsetVector

Returns the offset of the world.

Returns:

  • (Vector)

    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.

Overloads:

  • #offset=(vec) ⇒ Vector

    Parameters:

  • #offset=(ary) ⇒ Vector

    Parameters:

    • ary (Array<Numeric>)

      an array of offsetX and offsetY

Returns:

  • (Vector)

    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

#oxNumeric

Returns the x-axis offset of the world.

Returns:

  • (Numeric)

    offset.x



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.

Parameters:

  • x (Numeric)

    x-axis offset

Returns:

  • (Numeric)

    offset.x



1614
1615
1616
1617
1618
# File 'lib/rubysketch/sprite.rb', line 1614

def ox=(x)
  o           = offset
  o.x         = x
  self.offset = o
end

#oyNumeric

Returns the y-axis offset of the world.

Returns:

  • (Numeric)

    offset.y



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.

Parameters:

  • y (Numeric)

    y-axis offset

Returns:

  • (Numeric)

    offset.y



1634
1635
1636
1637
1638
# File 'lib/rubysketch/sprite.rb', line 1634

def oy=(y)
  o           = offset
  o.y         = y
  self.offset = o
end

#positionVector Also known as: pos

Returns the position of the world.

Returns:



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.

Overloads:

  • #position=(vec) ⇒ Vector

    Parameters:

    • vec (Vector)

      position vector

  • #position=(ary) ⇒ Vector

    Parameters:

    • ary (Array<Numeric>)

      an array of positionX and positionY

Returns:



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.

Parameters:

  • sprite (Sprite)

    sprite object

  • from (Array) (defaults to: nil)

    user array to remove the sprite

Returns:

  • (Sprite)

    the removed sprite

Raises:

  • (ArgumentError)


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

#rightNumeric

Returns the right position of the world.

Returns:

  • (Numeric)

    right position



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.

Parameters:

  • n (Numeric)

    world right position

Returns:

  • (Numeric)

    world right position



1464
1465
1466
# File 'lib/rubysketch/sprite.rb', line 1464

def right=(n)
  @view.right = n
end

#sizeVector

Returns the size of the world.

Returns:



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.

Returns:



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

#topNumeric

Returns the top position of the world.

Returns:

  • (Numeric)

    top position



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.

Parameters:

  • n (Numeric)

    world top position

Returns:

  • (Numeric)

    world top position



1446
1447
1448
# File 'lib/rubysketch/sprite.rb', line 1446

def top=(n)
  @view.top = n
end

#widthNumeric Also known as: w

Returns the width of the world.

Returns:

  • (Numeric)

    width



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.

Parameters:

  • w (Numeric)

    width

Returns:

  • (Numeric)

    width



1541
1542
1543
# File 'lib/rubysketch/sprite.rb', line 1541

def width=(w)
  @view.width = w
end

#xNumeric

Returns the x-coordinate position of the world.

Returns:

  • (Numeric)

    world position x



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.

Parameters:

  • n (Numeric)

    world position x

Returns:

  • (Numeric)

    world position x



1371
1372
1373
# File 'lib/rubysketch/sprite.rb', line 1371

def x=(n)
  @view.x = n
end

#yNumeric

Returns the y-coordinate position of the world.

Returns:

  • (Numeric)

    world position y



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.

Parameters:

  • n (Numeric)

    world position y

Returns:

  • (Numeric)

    world position y



1389
1390
1391
# File 'lib/rubysketch/sprite.rb', line 1389

def y=(n)
  @view.y = n
end

#zNumeric

Returns the z-coordinate position of the world.

Returns:

  • (Numeric)

    world position z



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.

Parameters:

  • n (Numeric)

    world position z

Returns:

  • (Numeric)

    world position z



1407
1408
1409
# File 'lib/rubysketch/sprite.rb', line 1407

def z=(n)
  @view.z = n
end

#zoomNumeric

Returns the zoom value of the world.

Returns:

  • (Numeric)

    zoom



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.

Returns:

  • (Numeric)

    zoom



1652
1653
1654
# File 'lib/rubysketch/sprite.rb', line 1652

def zoom=(zoom)
  @view.zoom = zoom
end