Class: DXOpal::Sprite

Inherits:
Object
  • Object
show all
Extended by:
CollisionCheck::ClassMethods
Includes:
CollisionCheck, Physics
Defined in:
lib/dxopal/sprite.rb,
lib/dxopal/sprite/collision_area.rb,
lib/dxopal/sprite/collision_check.rb,
lib/dxopal/sprite/physics.rb

Defined Under Namespace

Modules: CollisionArea, CollisionCheck, Physics

Instance Attribute Summary collapse

Attributes included from Physics

#_matter_body

Attributes included from CollisionCheck

#_collision_area, #collision, #collision_enable, #collision_sync

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CollisionCheck::ClassMethods

check

Methods included from Physics

#_move_matter_body, #_move_to_matter_body, #physical_body=

Methods included from CollisionCheck

#===, #_collidable?, #_collides?, #_init_collision_info, #check, #hit, #shot

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 '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

Instance Attribute Details

#alphaObject

Set alpha (0~255, default: nil)



57
58
59
# File 'lib/dxopal/sprite.rb', line 57

def alpha
  @alpha
end

#angleObject

Set angle (0~360, default: 0)



51
52
53
# File 'lib/dxopal/sprite.rb', line 51

def angle
  @angle
end

#blendObject

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



60
61
62
# File 'lib/dxopal/sprite.rb', line 60

def blend
  @blend
end

#center_xObject

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



55
56
57
# File 'lib/dxopal/sprite.rb', line 55

def center_x
  @center_x
end

#center_yObject

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



55
56
57
# File 'lib/dxopal/sprite.rb', line 55

def center_y
  @center_y
end

#scale_xObject

Set horizontal/vertical scale (default: 1.0)



53
54
55
# File 'lib/dxopal/sprite.rb', line 53

def scale_x
  @scale_x
end

#scale_yObject

Set horizontal/vertical scale (default: 1.0)



53
54
55
# File 'lib/dxopal/sprite.rb', line 53

def scale_y
  @scale_y
end

#visibleObject

Returns the value of attribute visible.



48
49
50
# File 'lib/dxopal/sprite.rb', line 48

def visible
  @visible
end

#xObject

Returns the value of attribute x.



62
63
64
# File 'lib/dxopal/sprite.rb', line 62

def x
  @x
end

#yObject

Returns the value of attribute y.



62
63
64
# File 'lib/dxopal/sprite.rb', line 62

def y
  @y
end

#zObject

Returns the value of attribute z.



48
49
50
# File 'lib/dxopal/sprite.rb', line 48

def z
  @z
end

Class Method Details

._add_matter_body(body, type, sprite, info) ⇒ Object



54
55
56
57
# 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_engineObject

(internal) Matter.Engine instance



40
41
42
# File 'lib/dxopal/sprite/physics.rb', line 40

def self._matter_engine
  @matter_engine ||= `Matter.Engine.create()`
end

._matter_runnerObject

(internal) Matter.Runner instance



45
46
47
# File 'lib/dxopal/sprite/physics.rb', line 45

def self._matter_runner
  @matter_runner ||= `Matter.Runner.create()`
end

._matter_spritesObject

(internal)



50
51
52
# File 'lib/dxopal/sprite/physics.rb', line 50

def self._matter_sprites
  @matter_bodies ||= {}
end

.clean(sprites) ⇒ Object

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



20
21
22
23
24
# File 'lib/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 '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?Boolean

Return true if `physical_body=` is ever called

Returns:

  • (Boolean)


60
61
62
63
64
# 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) ⇒ Object

Call Matter.Runner.tick

  • time: time given by requestAnimationFrame



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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

.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 '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

Instance Method Details

#drawObject

Draw this sprite to Window



90
91
92
93
94
95
96
97
98
# 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

#imageObject



74
# File 'lib/dxopal/sprite.rb', line 74

def image; @image; end

#image=(img) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# 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

#vanishObject



86
# File 'lib/dxopal/sprite.rb', line 86

def vanish; @vanished = true; end

#vanished?Boolean

Returns:

  • (Boolean)


87
# File 'lib/dxopal/sprite.rb', line 87

def vanished?; @vanished; end