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.



1052
1053
1054
# File 'lib/rubysketch/sprite.rb', line 1052

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

Instance Method Details

#addSprite(array = nil, sprite) ⇒ Sprite

Adds sprite to the physics engine.

Parameters:

  • array (Array) (defaults to: nil)

    user array to store the sprite

  • sprite (Sprite)

    sprite object

Returns:

  • (Sprite)

    the added sprite



1117
1118
1119
1120
1121
# File 'lib/rubysketch/sprite.rb', line 1117

def addSprite(array = nil, sprite)
  @view.add sprite.getInternal__
  array&.push sprite
  sprite
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



1104
1105
1106
1107
1108
# File 'lib/rubysketch/sprite.rb', line 1104

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

#debug=(state) ⇒ Object



1251
1252
1253
# File 'lib/rubysketch/sprite.rb', line 1251

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

#debug?Boolean

Returns:

  • (Boolean)


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

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



1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
# File 'lib/rubysketch/sprite.rb', line 1150

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

#offsetVector

Returns the offset of the sprite world.

Returns:

  • (Vector)

    offset of the sprite world



1165
1166
1167
1168
# File 'lib/rubysketch/sprite.rb', line 1165

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 sprite world.

Overloads:

  • #offset=(vec) ⇒ Vector

    Parameters:

  • #offset=(ary) ⇒ Vector

    Parameters:

    • ary (Array<Numeric>)

      an array of offsetX and offsetY

Returns:

  • (Vector)

    offset of the sprite world



1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
# File 'lib/rubysketch/sprite.rb', line 1180

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 sprite world.

Returns:

  • (Numeric)

    offset.x



1197
1198
1199
# File 'lib/rubysketch/sprite.rb', line 1197

def ox()
  offset.x
end

#ox=(x) ⇒ Numeric

Sets the x-axis offset of the sprite world.

Parameters:

  • x (Numeric)

    x-axis offset

Returns:

  • (Numeric)

    offset.x



1207
1208
1209
1210
1211
1212
# File 'lib/rubysketch/sprite.rb', line 1207

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

#oyNumeric

Returns the y-axis offset of the sprite world.

Returns:

  • (Numeric)

    offset.y



1218
1219
1220
# File 'lib/rubysketch/sprite.rb', line 1218

def oy()
  offset.y
end

#oy=(x) ⇒ Numeric

Sets the y-axis offset of the sprite world.

Parameters:

  • x (Numeric)

    y-axis offset

Returns:

  • (Numeric)

    offset.y



1228
1229
1230
1231
1232
1233
# File 'lib/rubysketch/sprite.rb', line 1228

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

#removeSprite(array = nil, sprite) ⇒ Sprite

Removes sprite from the physics engine.

Parameters:

  • array (Array) (defaults to: nil)

    user array to remove the sprite

  • sprite (Sprite)

    sprite object

Returns:

  • (Sprite)

    the removed sprite



1130
1131
1132
1133
1134
# File 'lib/rubysketch/sprite.rb', line 1130

def removeSprite(array = nil, sprite)
  @view.remove sprite.getInternal__
  array&.delete sprite
  sprite
end

#zoomNumeric

Returns the zoom value of the sprite world.

Returns:

  • (Numeric)

    zoom



1239
1240
1241
# File 'lib/rubysketch/sprite.rb', line 1239

def zoom()
  @view.zoom
end

#zoom=(zoom) ⇒ Numeric

Sets the zoom value of the sprite world.

Returns:

  • (Numeric)

    zoom



1247
1248
1249
# File 'lib/rubysketch/sprite.rb', line 1247

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