class DXOpal::Sprite

Attributes

alpha[RW]

Set alpha (0~255, default: nil)

angle[RW]

Set angle (0~360, default: 0)

blend[RW]

Set blend type (Any of :alpha(default), :add) (:none, :add2 and :sub are not implemented yet; Pull request is welcome)

center_x[RW]

Set rotation center (default: center of `image`)

center_y[RW]

Set rotation center (default: center of `image`)

scale_x[RW]

Set horizontal/vertical scale (default: 1.0)

scale_y[RW]

Set horizontal/vertical scale (default: 1.0)

visible[RW]
x[R]
y[R]
z[RW]

Public Class Methods

_add_matter_body(body, type, sprite, info) click to toggle source
# File lib/dxopal/sprite/physics.rb, line 54
def self._add_matter_body(body, type, sprite, info)
  _matter_sprites[`body.id`] = [type, sprite, info]
  `Matter.World.addBody(#{Sprite._matter_engine}.world, body)`
end
_matter_engine() click to toggle source

(internal) Matter.Engine instance

# File lib/dxopal/sprite/physics.rb, line 40
def self._matter_engine
  @matter_engine ||= `Matter.Engine.create()`
end
_matter_runner() click to toggle source

(internal) Matter.Runner instance

# File lib/dxopal/sprite/physics.rb, line 45
def self._matter_runner
  @matter_runner ||= `Matter.Runner.create()`
end
_matter_sprites() click to toggle source

(internal)

# File lib/dxopal/sprite/physics.rb, line 50
def self._matter_sprites
  @matter_bodies ||= {}
end
clean(sprites) click to toggle source

Remove vanished sprites (and nils) from the array, destructively

# File lib/dxopal/sprite.rb, line 20
def self.clean(sprites)
  sprites.reject!{|sprite|
    sprite.nil? || sprite.vanished?
  }
end
draw(sprites) click to toggle source

Draw each of the given sprites (unless it is vanished)

# File lib/dxopal/sprite.rb, line 27
def self.draw(sprites)
  sprites.flatten.sort_by(&:z).each do |sprite|
    next if sprite.respond_to?(:vanished?) && sprite.vanished?
    sprite.draw
  end
end
matter_enabled?() click to toggle source

Return true if `physical_body=` is ever called

# File lib/dxopal/sprite/physics.rb, line 60
def self.matter_enabled?
  # Note: we cannot use `!!` here because @matter_engine may be a JS object,
  # which does not have Ruby's `!@` method
  @matter_engine ? true : false
end
matter_tick(time) click to toggle source

Call Matter.Runner.tick

  • time: time given by requestAnimationFrame

# File lib/dxopal/sprite/physics.rb, line 68
def self.matter_tick(time)
  %x{
    Matter.Runner.tick(#{Sprite._matter_runner}, #{Sprite._matter_engine}, time);
    Matter.Composite.allBodies(#{Sprite._matter_engine}.world).forEach((body) => {
      var [type, sprite, info] = #{Sprite._matter_sprites[`body.id`]};
      switch(type) {
      case "rectangle":
        var [width, height] = info;
        sprite['$_move_to_matter_body'](body.position.x, body.position.y);
        sprite['$angle='](body.angle / Math.PI * 180);
        break;
      default:
        `#{raise "unknown type: #{type}"}`
      }
    });
  }
end
new(x=0, y=0, image=nil) click to toggle source
# File lib/dxopal/sprite.rb, line 34
def initialize(x=0, y=0, image=nil)
  @x, @y, @image = x, y, image
  @z = 0
  @angle = 0
  @scale_x = @scale_y = 1.0
  if image
    @center_x = image.width / 2
    @center_y = image.height / 2
  end

  @visible = true
  @vanished = false
  _init_collision_info(@image)
end
update(sprites) click to toggle source

Call update on each sprite (unless it is vanished or do not have update)

# File lib/dxopal/sprite.rb, line 11
def self.update(sprites)
  sprites.each do |sprite|
    next if !sprite.respond_to?(:update)
    next if sprite.respond_to?(:vanished?) && sprite.vanished?
    sprite.update
  end
end

Public Instance Methods

draw() click to toggle source

Draw this sprite to Window

# File lib/dxopal/sprite.rb, line 90
def draw
  raise "image not set to Sprite" if @image.nil?
  return if !@visible

  Window.draw_ex(@x, @y, @image,
                 scale_x: @scale_x, scale_y: @scale_y,
                 alpha: @alpha, blend: @blend,
                 angle: @angle, center_x: @center_x, center_y: @center_y)
end
image() click to toggle source
# File lib/dxopal/sprite.rb, line 74
def image; @image; end
image=(img) click to toggle source
# File lib/dxopal/sprite.rb, line 75
def image=(img)
  @image = img
  if @collision.nil?
    self.collision = [0, 0, img.width-1, img.height-1]
  end
  if @center_x.nil?
    @center_x = img.width / 2
    @center_y = img.height / 2
  end
end
vanish() click to toggle source
# File lib/dxopal/sprite.rb, line 86
def vanish; @vanished = true; end
vanished?() click to toggle source
# File lib/dxopal/sprite.rb, line 87
def vanished?; @vanished; end
x=(newx) click to toggle source
# File lib/dxopal/sprite.rb, line 64
def x=(newx)
  @x = newx
  _move_matter_body if @_matter_body
end
y=(newy) click to toggle source
# File lib/dxopal/sprite.rb, line 69
def y=(newy)
  @y = newy
  _move_matter_body if @_matter_body
end