Class: DXRubySDL::Sprite

Inherits:
Object
  • Object
show all
Defined in:
lib/dxruby_sdl/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.



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dxruby_sdl/sprite.rb', line 84

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.



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

def alpha
  @alpha
end

#angleObject

Returns the value of attribute angle.



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

def angle
  @angle
end

#blendObject

Returns the value of attribute blend.



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

def blend
  @blend
end

#center_xObject

Returns the value of attribute center_x.



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

def center_x
  @center_x
end

#center_yObject

Returns the value of attribute center_y.



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

def center_y
  @center_y
end

#collisionObject

Returns the value of attribute collision.



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

def collision
  @collision
end

#collision_enableObject

Returns the value of attribute collision_enable.



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

def collision_enable
  @collision_enable
end

#collision_syncObject

Returns the value of attribute collision_sync.



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

def collision_sync
  @collision_sync
end

#imageObject

Returns the value of attribute image.



7
8
9
# File 'lib/dxruby_sdl/sprite.rb', line 7

def image
  @image
end

#scale_xObject

Returns the value of attribute scale_x.



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

def scale_x
  @scale_x
end

#scale_yObject

Returns the value of attribute scale_y.



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

def scale_y
  @scale_y
end

#shaderObject

Returns the value of attribute shader.



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

def shader
  @shader
end

#targetObject

Returns the value of attribute target.



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

def target
  @target
end

#visibleObject

Returns the value of attribute visible.



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

def visible
  @visible
end

#xObject

Returns the value of attribute x.



5
6
7
# File 'lib/dxruby_sdl/sprite.rb', line 5

def x
  @x
end

#yObject

Returns the value of attribute y.



6
7
8
# File 'lib/dxruby_sdl/sprite.rb', line 6

def y
  @y
end

#zObject

Returns the value of attribute z.



8
9
10
# File 'lib/dxruby_sdl/sprite.rb', line 8

def z
  @z
end

Class Method Details

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



24
25
26
27
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
# File 'lib/dxruby_sdl/sprite.rb', line 24

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



79
80
81
# File 'lib/dxruby_sdl/sprite.rb', line 79

def clean(sprites)
  return [sprites].flatten.compact.reject(&:vanished?)
end

.draw(sprites) ⇒ Object



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

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

.update(sprites) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/dxruby_sdl/sprite.rb', line 63

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

Instance Method Details

#===(other) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/dxruby_sdl/sprite.rb', line 130

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



154
155
156
# File 'lib/dxruby_sdl/sprite.rb', line 154

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

#drawObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/dxruby_sdl/sprite.rb', line 102

def draw
  if !@visible || vanished?
    return
  end
  [:target, :blend, :shader].each do |method|
    if send(method)
      raise NotImplementedError, "Sprite#draw with #{method}"
    end
  end
  options = {}
  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 center_x
    options[:center_x] = center_x
  end
  if center_y
    options[:center_y] = center_y
  end
  Window.draw_ex(x, y, image, options)
end

#vanishObject



158
159
160
# File 'lib/dxruby_sdl/sprite.rb', line 158

def vanish
  @vanished = true
end

#vanished?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/dxruby_sdl/sprite.rb', line 162

def vanished?
  return @vanished
end