Class: Ruby2D::Sprite

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby2d/sprite.rb,
ext/ruby2d/ruby2d-opal.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, path, z = 0) ⇒ Sprite

Returns a new instance of Sprite.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby2d/sprite.rb', line 9

def initialize(x, y, path, z=0)

  unless RUBY_ENGINE == 'opal'
    unless File.exists? path
      raise Error, "Cannot find sprite image file `#{path}`"
    end
  end

  @x, @y, @path = x, y, path
  @clip_x, @clip_y, @clip_w, @clip_h = 0, 0, 0, 0
  @default = nil
  @animations = {}
  @current_animation = nil
  @current_frame = 0
  @current_frame_time = 0
  @z = z

  ext_init(path)
  if Module.const_defined? :DSL
    Application.add(self)
  end
end

Instance Attribute Details

#clip_hObject

Returns the value of attribute clip_h.



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

def clip_h
  @clip_h
end

#clip_wObject

Returns the value of attribute clip_w.



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

def clip_w
  @clip_w
end

#clip_xObject

Returns the value of attribute clip_x.



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

def clip_x
  @clip_x
end

#clip_yObject

Returns the value of attribute clip_y.



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

def clip_y
  @clip_y
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#xObject

Returns the value of attribute x.



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

def x
  @x
end

#yObject

Returns the value of attribute y.



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

def y
  @y
end

#zObject (readonly)

Returns the value of attribute z.



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

def z
  @z
end

Instance Method Details

#add(animations) ⇒ Object



37
38
39
# File 'lib/ruby2d/sprite.rb', line 37

def add(animations)
  @animations.merge!(animations)
end

#animate(animation) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/ruby2d/sprite.rb', line 41

def animate(animation)
  if @current_animation != animation
    @current_frame      = 0
    @current_frame_time = 0
    @current_animation  = animation
  end
  animate_frames(@animations[animation])
end

#ext_init(path) ⇒ Object



172
173
174
# File 'ext/ruby2d/ruby2d-opal.rb', line 172

def ext_init(path)
  `#{self}.data = S2D.CreateSprite(path);`
end

#ext_renderObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'ext/ruby2d/ruby2d-opal.rb', line 176

def ext_render
  `
  #{self}.data.x = #{self}.x;
  #{self}.data.y = #{self}.y;

  S2D.ClipSprite(
    #{self}.data,
    #{self}.clip_x,
    #{self}.clip_y,
    #{self}.clip_w,
    #{self}.clip_h
  );

  S2D.DrawSprite(#{self}.data);
  `
end

#heightObject



72
73
74
# File 'lib/ruby2d/sprite.rb', line 72

def height
  @current_animation ? @animations[@current_animation][@current_frame][3] : @default[3]
end

#removeObject

TODO: Sprite already has an ‘add` method, have to reconsile def add

if Module.const_defined? :DSL
  Application.add(self)
end

end



62
63
64
65
66
# File 'lib/ruby2d/sprite.rb', line 62

def remove
  if Module.const_defined? :DSL
    Application.remove(self)
  end
end

#resetObject



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

def reset
  clip(@default[0], @default[1], @default[2], @default[3])
  @current_animation = nil
end

#start(x, y, w, h) ⇒ Object



32
33
34
35
# File 'lib/ruby2d/sprite.rb', line 32

def start(x, y, w, h)
  @default = [x, y, w, h]
  clip(x, y, w, h)
end

#widthObject



68
69
70
# File 'lib/ruby2d/sprite.rb', line 68

def width
  @current_animation ? @animations[@current_animation][@current_frame][2] : @default[2]
end