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(pixels_per_meter: 0) ⇒ SpriteWorld

Create a new physics world



942
943
944
# File 'lib/rubysketch/sprite.rb', line 942

def initialize(pixels_per_meter: 0)
  @view, @debug = View.new(pixels_per_meter: pixels_per_meter), false
end

Instance Method Details

#addSprite(*sprites) ⇒ Sprite

Adds sprites to the physics engine.

Parameters:

  • sprites (Sprite)

    sprite objects

Returns:

  • (Sprite)

    first added sprite



1005
1006
1007
1008
# File 'lib/rubysketch/sprite.rb', line 1005

def addSprite(*sprites)
  sprites.each {@view.add _1.getInternal__}
  sprites.first
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



994
995
996
997
# File 'lib/rubysketch/sprite.rb', line 994

def createSprite(*args, context: nil, **kwargs)
  context ||= Context.context__
  addSprite Sprite.new(*args, context: context, **kwargs)
end

#debug=(state) ⇒ Object



1046
1047
1048
# File 'lib/rubysketch/sprite.rb', line 1046

def debug=(state)
  @view.debug = state
end

#debug?Boolean

Returns:

  • (Boolean)


1050
# File 'lib/rubysketch/sprite.rb', line 1050

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



1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
# File 'lib/rubysketch/sprite.rb', line 1035

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

#removeSprite(*sprites) ⇒ Sprite

Removes sprites from the physics engine.

Parameters:

  • sprites (Sprite)

    sprite objects

Returns:

  • (Sprite)

    first removed sprite



1016
1017
1018
1019
# File 'lib/rubysketch/sprite.rb', line 1016

def removeSprite(*sprites)
  sprites.each {@view.remove _1.getInternal__}
  sprites.first
end