Class: DXOpal::Sprite
- Inherits:
-
Object
- Object
- DXOpal::Sprite
- Extended by:
- CollisionCheck::ClassMethods
- Includes:
- CollisionCheck, Physics
- Defined in:
- opal/dxopal/sprite.rb
Instance Attribute Summary collapse
-
#angle ⇒ Object
Set angle (0~360, default: 0).
-
#center_x ⇒ Object
Set rotation center (default: center of `image`).
-
#center_y ⇒ Object
Set rotation center (default: center of `image`).
-
#scale_x ⇒ Object
Set horizontal/vertical scale (default: 1.0).
-
#scale_y ⇒ Object
Set horizontal/vertical scale (default: 1.0).
-
#visible ⇒ Object
Returns the value of attribute visible.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#z ⇒ Object
Returns the value of attribute z.
Class Method Summary collapse
-
.clean(sprites) ⇒ Object
Remove vanished sprites (and nils) from the array, destructively.
-
.draw(sprites) ⇒ Object
Draw each of the given sprites (unless it is vanished).
-
.update(sprites) ⇒ Object
Call #update on each sprite (unless it is vanished or do not have #update).
Instance Method Summary collapse
-
#draw ⇒ Object
Draw this sprite to Window.
- #image ⇒ Object
- #image=(img) ⇒ Object
-
#initialize(x = 0, y = 0, image = nil) ⇒ Sprite
constructor
A new instance of Sprite.
- #vanish ⇒ Object
- #vanished? ⇒ Boolean
Constructor Details
#initialize(x = 0, y = 0, image = nil) ⇒ Sprite
Returns a new instance of Sprite
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'opal/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 |
Instance Attribute Details
#angle ⇒ Object
Set angle (0~360, default: 0)
51 52 53 |
# File 'opal/dxopal/sprite.rb', line 51 def angle @angle end |
#center_x ⇒ Object
Set rotation center (default: center of `image`)
55 56 57 |
# File 'opal/dxopal/sprite.rb', line 55 def center_x @center_x end |
#center_y ⇒ Object
Set rotation center (default: center of `image`)
55 56 57 |
# File 'opal/dxopal/sprite.rb', line 55 def center_y @center_y end |
#scale_x ⇒ Object
Set horizontal/vertical scale (default: 1.0)
53 54 55 |
# File 'opal/dxopal/sprite.rb', line 53 def scale_x @scale_x end |
#scale_y ⇒ Object
Set horizontal/vertical scale (default: 1.0)
53 54 55 |
# File 'opal/dxopal/sprite.rb', line 53 def scale_y @scale_y end |
#visible ⇒ Object
Returns the value of attribute visible
48 49 50 |
# File 'opal/dxopal/sprite.rb', line 48 def visible @visible end |
#x ⇒ Object
Returns the value of attribute x
57 58 59 |
# File 'opal/dxopal/sprite.rb', line 57 def x @x end |
#y ⇒ Object
Returns the value of attribute y
57 58 59 |
# File 'opal/dxopal/sprite.rb', line 57 def y @y end |
#z ⇒ Object
Returns the value of attribute z
48 49 50 |
# File 'opal/dxopal/sprite.rb', line 48 def z @z end |
Class Method Details
.clean(sprites) ⇒ Object
Remove vanished sprites (and nils) from the array, destructively
20 21 22 23 24 |
# File 'opal/dxopal/sprite.rb', line 20 def self.clean(sprites) sprites.reject!{|sprite| sprite.nil? || sprite.vanished? } end |
.draw(sprites) ⇒ Object
Draw each of the given sprites (unless it is vanished)
27 28 29 30 31 32 |
# File 'opal/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 |
.update(sprites) ⇒ Object
Call #update on each sprite (unless it is vanished or do not have #update)
11 12 13 14 15 16 17 |
# File 'opal/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 |
Instance Method Details
#draw ⇒ Object
Draw this sprite to Window
85 86 87 88 89 90 91 92 |
# File 'opal/dxopal/sprite.rb', line 85 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, angle: @angle, center_x: @center_x, center_y: @center_y) end |
#image ⇒ Object
69 |
# File 'opal/dxopal/sprite.rb', line 69 def image; @image; end |
#image=(img) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'opal/dxopal/sprite.rb', line 70 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 ⇒ Object
81 |
# File 'opal/dxopal/sprite.rb', line 81 def vanish; @vanished = true; end |
#vanished? ⇒ Boolean
82 |
# File 'opal/dxopal/sprite.rb', line 82 def vanished?; @vanished; end |