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) ⇒ Sprite

Returns a new instance of Sprite.



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

def initialize(x, y, path)
  
  # unless File.exists? path
  #   raise Error, "Cannot find image file `#{path}`"
  # end
  
  @type_id = 4
  @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
  
  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

Instance Method Details

#add(animations) ⇒ Object



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

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

#animate(animation) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/ruby2d/sprite.rb', line 38

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

#init(path) ⇒ Object



141
142
143
# File 'ext/ruby2d/ruby2d-opal.rb', line 141

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

#removeObject

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

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

end



59
60
61
62
63
# File 'lib/ruby2d/sprite.rb', line 59

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

#resetObject



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

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

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



29
30
31
32
# File 'lib/ruby2d/sprite.rb', line 29

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