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.



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/dxruby_sdl/sprite.rb', line 99

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



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

def clean(sprites)
  sprites.size.times do |i|
    s = sprites[i]
    if s.kind_of?(Array)
      clean(s)
    else
      if s.respond_to?(:vanished?)
        sprites[i] = nil if s.vanished?
      end
    end
  end
  sprites.compact!
  nil
end

.draw(sprites) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/dxruby_sdl/sprite.rb', line 73

def draw(sprites)
  [sprites].flatten.each do |s|
    if !s.respond_to?(:vanished?) or !s.vanished?
      if s.respond_to?(:draw)
        s.draw
      end
    end
  end
end

.update(sprites) ⇒ Object



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

def update(sprites)
  [sprites].flatten.each do |s|
    if !s.respond_to?(:vanished?) or !s.vanished?
      if s.respond_to?(:update)
        s.update
      end
    end
  end
end

Instance Method Details

#===(other) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/dxruby_sdl/sprite.rb', line 149

def ===(other)
  if !@collision_enable || vanished? ||
      !other.collision_enable || other.vanished? ||
      !other.image && !other.collision
    return false
  end
  if @collision_enable && @collision
    x1, y1, x2, y2 =
      @collision[0] + @x, @collision[1] + @y, @collision[2] + @x, @collision[3] + @y
  else
    x1, y1, x2, y2 =
      @x, @y, @image.width + @x - 1, @image.height + @y - 1
  end
  if other.collision_enable && other.collision
    other_x1, other_y1, other_x2, other_y2 =
      other.collision[0] + other.x, other.collision[1] + other.y,
      other.collision[2] + other.x, other.collision[3] + other.y
  else
    other_x1, other_y1, other_x2, other_y2 =
      other.x, other.y, other.image.width + other.x - 1, other.image.height + other.y - 1
  end
  return other_x2 >= x1 && other_x1 <= x2 &&
    other_y2 >= y1 && other_y1 <= y2
end

#check(sprites) ⇒ Object



174
175
176
# File 'lib/dxruby_sdl/sprite.rb', line 174

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

#drawObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/dxruby_sdl/sprite.rb', line 117

def draw
  if !@visible || vanished?
    return
  end
  [: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
  if target
    target.draw_ex(x, y, image, options)
  else
    Window.draw_ex(x, y, image, options)
  end
end

#vanishObject



178
179
180
# File 'lib/dxruby_sdl/sprite.rb', line 178

def vanish
  @vanished = true
end

#vanished?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/dxruby_sdl/sprite.rb', line 182

def vanished?
  return @vanished
end