Class: Okami::Sprite

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

Constant Summary collapse

@@modes =
{
  loop:          {direction: :forward,  update_method: :update_loop},
  backward_loop: {direction: :backward, update_method: :update_backward_loop},
  reverse_loop:  {direction: :backward, update_method: :update_backward_loop},
  forward:       {direction: :forward,  update_method: :update_forward},
  backward:      {direction: :backward, update_method: :update_backward},
  reverse:       {direction: :backward, update_method: :update_backward},
  ping_pong:     {direction: :_forward, update_method: :update_ping_pong}
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Sprite

Returns a new instance of Sprite.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/okami/sprite.rb', line 15

def initialize options
  raise_possible_errors options
  
  self.images         = options[:images]
  self.fps            = options[:fps]
  self.frame_index    = options[:frame_index] if options[:frame_index]
  self.mode = options[:mode] || :loop
  
  set_initial_frame_index
  update_current_image
  
  @@default_dt ||= $window.update_interval / 1000.0    
end

Instance Attribute Details

#frame_indexObject

Returns the value of attribute frame_index.



64
65
66
# File 'lib/okami/sprite.rb', line 64

def frame_index
  @frame_index
end

#imageObject (readonly)

Returns the value of attribute image.



2
3
4
# File 'lib/okami/sprite.rb', line 2

def image
  @image
end

#imagesObject

Returns the value of attribute images.



2
3
4
# File 'lib/okami/sprite.rb', line 2

def images
  @images
end

#last_frame_indexObject (readonly)

Returns the value of attribute last_frame_index.



2
3
4
# File 'lib/okami/sprite.rb', line 2

def last_frame_index
  @last_frame_index
end

#modeObject

Returns the value of attribute mode.



55
56
57
# File 'lib/okami/sprite.rb', line 55

def mode
  @mode
end

Class Method Details

.modesObject



13
# File 'lib/okami/sprite.rb', line 13

def self.modes; @@modes end

Instance Method Details

#draw(*args) ⇒ Object



29
# File 'lib/okami/sprite.rb', line 29

def draw       *args;            @image.draw       *args            end

#draw_rot(*args) ⇒ Object



30
# File 'lib/okami/sprite.rb', line 30

def draw_rot   *args;            @image.draw_rot   *args            end

#draw_using(image_attributes) ⇒ Object



31
# File 'lib/okami/sprite.rb', line 31

def draw_using image_attributes; @image.draw_using image_attributes end

#finished?Boolean Also known as: animation_finished?

Returns:

  • (Boolean)


47
48
49
50
51
52
# File 'lib/okami/sprite.rb', line 47

def finished?
  case @mode
  when :forward;  @frame_index == @last_frame_index
  when :backward; @frame_index == 0
  end
end

#fpsObject



33
# File 'lib/okami/sprite.rb', line 33

def fps;      @delta_frame_index * 60.0         end

#fps=(fps) ⇒ Object



34
# File 'lib/okami/sprite.rb', line 34

def fps=fps;  @delta_frame_index = fps / 60.0   end

#random_frame_indexObject



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

def random_frame_index
  rand * @images.length
end

#update(dt = @@default_dt) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/okami/sprite.rb', line 36

def update dt=@@default_dt
  case @direction
  when :forward  then @frame_index += @delta_frame_index * dt * 60.0
  when :backward then @frame_index -= @delta_frame_index * dt * 60.0
  end
  
  @update_method.call
  update_current_image
end