Class: DXRubyRP5::Sprite

Inherits:
Object
  • Object
show all
Defined in:
lib/dxruby_rp5/sprite.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = 0, y = 0, image = nil) ⇒ Sprite

Returns a new instance of Sprite.



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dxruby_rp5/sprite.rb', line 93

def initialize(x = 0, y = 0, image = nil)
  @x = x
  @y = y
  @image = image

  @collision_enable = true
  @collision_sync = true
  @visible = true
  @vanished = false

  calc_center
end

Instance Attribute Details

#alphaObject

Returns the value of attribute alpha.



18
19
20
# File 'lib/dxruby_rp5/sprite.rb', line 18

def alpha
  @alpha
end

#angleObject

Returns the value of attribute angle.



13
14
15
# File 'lib/dxruby_rp5/sprite.rb', line 13

def angle
  @angle
end

#blendObject

Returns the value of attribute blend.



19
20
21
# File 'lib/dxruby_rp5/sprite.rb', line 19

def blend
  @blend
end

#center_xObject

Returns the value of attribute center_x.



16
17
18
# File 'lib/dxruby_rp5/sprite.rb', line 16

def center_x
  @center_x
end

#center_yObject

Returns the value of attribute center_y.



17
18
19
# File 'lib/dxruby_rp5/sprite.rb', line 17

def center_y
  @center_y
end

#collisionObject

Returns the value of attribute collision.



22
23
24
# File 'lib/dxruby_rp5/sprite.rb', line 22

def collision
  @collision
end

#collision_enableObject

Returns the value of attribute collision_enable.



23
24
25
# File 'lib/dxruby_rp5/sprite.rb', line 23

def collision_enable
  @collision_enable
end

#collision_syncObject

Returns the value of attribute collision_sync.



24
25
26
# File 'lib/dxruby_rp5/sprite.rb', line 24

def collision_sync
  @collision_sync
end

#imageObject

Returns the value of attribute image.



11
12
13
# File 'lib/dxruby_rp5/sprite.rb', line 11

def image
  @image
end

#scale_xObject

Returns the value of attribute scale_x.



14
15
16
# File 'lib/dxruby_rp5/sprite.rb', line 14

def scale_x
  @scale_x
end

#scale_yObject

Returns the value of attribute scale_y.



15
16
17
# File 'lib/dxruby_rp5/sprite.rb', line 15

def scale_y
  @scale_y
end

#shaderObject

Returns the value of attribute shader.



20
21
22
# File 'lib/dxruby_rp5/sprite.rb', line 20

def shader
  @shader
end

#targetObject

Returns the value of attribute target.



21
22
23
# File 'lib/dxruby_rp5/sprite.rb', line 21

def target
  @target
end

#visibleObject

Returns the value of attribute visible.



25
26
27
# File 'lib/dxruby_rp5/sprite.rb', line 25

def visible
  @visible
end

#xObject

Returns the value of attribute x.



9
10
11
# File 'lib/dxruby_rp5/sprite.rb', line 9

def x
  @x
end

#yObject

Returns the value of attribute y.



10
11
12
# File 'lib/dxruby_rp5/sprite.rb', line 10

def y
  @y
end

#zObject

Returns the value of attribute z.



12
13
14
# File 'lib/dxruby_rp5/sprite.rb', line 12

def z
  @z
end

Class Method Details

.check(o_sprites, d_sprites, shot = :shot, hit = :hit) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dxruby_rp5/sprite.rb', line 28

def check(o_sprites, d_sprites, shot = :shot, hit = :hit)
  res = false
  o_sprites = [o_sprites].flatten.select { |s| s.is_a?(self) }
  d_sprites = [d_sprites].flatten.select { |s| s.is_a?(self) }
  discards = []
  o_sprites.each do |o_sprite|
    if discards.include?(o_sprite)
      next
    end
    d_sprites.each do |d_sprite|
      if discards.include?(o_sprite)
        break
      end
      if discards.include?(d_sprite)
        next
      end
      if o_sprite.object_id == d_sprite.object_id
        next
      end
      if o_sprite === d_sprite
        res = true
        discard = false
        if o_sprite.respond_to?(shot) && shot
          if o_sprite.send(shot, d_sprite) == :discard
            discard = true
          end
        end
        if d_sprite.respond_to?(hit) && hit
          if d_sprite.send(hit, o_sprite) == :discard
            discard = true
          end
        end
        if discard
          discards << o_sprite
          discards << d_sprite
        end
      end
    end
  end
  return res
end

.clean(sprites) ⇒ Object



86
87
88
89
90
# File 'lib/dxruby_rp5/sprite.rb', line 86

def clean(sprites)
  sprites = [sprites].flatten.select { |s| s.is_a?(self) }.compact
  return sprites.reject(&:vanished?)
#        return [sprites].flatten.compact.reject(&:vanished?)
end

.draw(sprites) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/dxruby_rp5/sprite.rb', line 78

def draw(sprites)
  sprites.flatten.each do |s|
    if s.respond_to?(:draw)
      s.draw
    end
  end
end

.update(sprites) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/dxruby_rp5/sprite.rb', line 70

def update(sprites)
  sprites.flatten.each do |s|
    if s.respond_to?(:update)
      s.update
    end
  end
end

Instance Method Details

#===(other) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/dxruby_rp5/sprite.rb', line 136

def ===(other)
  if !@collision_enable || vanished? ||
      !other.collision_enable || other.vanished? ||
      !other.image && !other.collision
    return false
  end
  if @collision_enable && @collision
    x = @collision[0] + @x
    y = @collision[1] + @y
    width = @collision[2] - @collision[0]
    height = @collision[3] - @collision[1]
  else
    x, y, width, height =
      @x, @y, @image.width, @image.height
  end
  if other.collision_enable && other.collision
    other_x = other.x + other.collision[0]
    other_y = other.y + + other.collision[1]
    other_width = other.collision[2] - other.collision[0]
    other_height = other.collision[3] - other.collision[1]
  else
    other_x, other_y, other_width, other_height =
      other.x, other.y, other.image.width, other.image.height
  end
  return other_x + other_width > x && other_x < x + width &&
    other_y + other_height > y && other_y < y + height
end

#check(sprites) ⇒ Object



164
165
166
# File 'lib/dxruby_rp5/sprite.rb', line 164

def check(sprites)
  return [sprites].flatten.select { |s| self === s }
end

#drawObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/dxruby_rp5/sprite.rb', line 111

def draw
  if !@visible || vanished?
    return
  end
  #TODO:

  options = {}
  if alpha
    options[:alpha] = alpha
  end
  if angle
    options[:angle] = angle
  end
  if scale_x
    options[:scale_x] = scale_x
  end
  if scale_y
    options[:scale_y] = scale_y
  end
  if z
    options[:z] = z
  end
  Window.draw_ex(x, y, image, options)
end

#vanishObject



168
169
170
# File 'lib/dxruby_rp5/sprite.rb', line 168

def vanish
  @vanished = true
end

#vanished?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/dxruby_rp5/sprite.rb', line 172

def vanished?
  return @vanished
end