class DXOpal::Sprite
Attributes
Set alpha (0~255, default: nil)
Set angle (0~360, default: 0)
Set blend type (Any of :alpha(default), :add) (:none, :add2 and :sub are not implemented yet; Pull request is welcome)
Set rotation center (default: center of `image`)
Set rotation center (default: center of `image`)
Set horizontal/vertical scale (default: 1.0)
Set horizontal/vertical scale (default: 1.0)
Public Class Methods
# 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
(internal) Matter.Engine instance
# File lib/dxopal/sprite/physics.rb, line 40 def self._matter_engine @matter_engine ||= `Matter.Engine.create()` end
(internal) Matter.Runner instance
# File lib/dxopal/sprite/physics.rb, line 45 def self._matter_runner @matter_runner ||= `Matter.Runner.create()` end
(internal)
# File lib/dxopal/sprite/physics.rb, line 50 def self._matter_sprites @matter_bodies ||= {} end
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 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
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
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
# 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
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 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
# File lib/dxopal/sprite.rb, line 74 def image; @image; end
# 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
# File lib/dxopal/sprite.rb, line 86 def vanish; @vanished = true; end
# File lib/dxopal/sprite.rb, line 87 def vanished?; @vanished; end
# File lib/dxopal/sprite.rb, line 64 def x=(newx) @x = newx _move_matter_body if @_matter_body end
# File lib/dxopal/sprite.rb, line 69 def y=(newy) @y = newy _move_matter_body if @_matter_body end