Class: AdventureRL::Animation

Inherits:
Image show all
Defined in:
lib/AdventureRL/Animation.rb

Constant Summary collapse

DEFAULT_SETTINGS =
Settings.new(
  files:     ['DEFAULT_ANIMATION_FILE.png'],
  intervals: [0.5]  # Image switch intervals in seconds.
)

Constants inherited from Image

Image::IMAGE_OPTION_KEYS

Constants included from Helpers::Error

Helpers::Error::PADDING, Helpers::Error::STACK_TRACE_PADDING, Helpers::Error::STACK_TRACE_SIZE

Constants inherited from Mask

Mask::MASKS

Constants inherited from Point

Point::POINTS

Instance Method Summary collapse

Methods inherited from Image

#draw

Methods included from Helpers::Error

directory_exists?, error, error_no_directory, error_no_file, file_exists?

Methods inherited from Mask

#assign_to, #assigned_to?, #collides_with?, #collides_with_hash?, #collides_with_mask?, #collides_with_point?, #get_assigned, #get_center, #get_corner, #get_layer, #get_mask, #get_origin, #get_real_center, #get_real_corner, #get_real_side, #get_real_sides, #get_side, #get_sides, #get_size, #has_layer?, #has_mask?, #set_layer, #set_size

Methods inherited from Point

#assign_to, #assigned_to?, #collides_with?, #collides_with_hash?, #collides_with_mask?, #collides_with_point?, #get_assigned, #get_layer, #get_point, #get_position, #get_real_point, #get_real_position, #has_layer?, #has_point?, #keys, #move_by, #set_layer, #set_position, #values, #x, #y

Constructor Details

#initialize(settings = {}) ⇒ Animation

Returns a new instance of Animation.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/AdventureRL/Animation.rb', line 8

def initialize settings = {}
  @settings = DEFAULT_SETTINGS.merge settings
  image_settings = @settings.get
  image_settings[:dont_create_image] = true
  super image_settings
  @images              = get_images_from [@settings.get(:files)].flatten
  @animation_intervals = [@settings.get(:intervals)].flatten
  @timing_handler      = TimingHandler.new
  @timeout_id          = :next_image_timeout
  @current_image_index = -1
  next_image
end

Instance Method Details

#next_imageObject



30
31
32
33
34
35
# File 'lib/AdventureRL/Animation.rb', line 30

def next_image
  @current_image_index += 1
  @current_image_index  = 0  if (@current_image_index >= @images.size)
  @image = @images[@current_image_index]
  set_timeout
end

#set_timeoutObject



37
38
39
40
41
42
43
44
# File 'lib/AdventureRL/Animation.rb', line 37

def set_timeout
  current_interval = get_current_interval
  @timing_handler.set_timeout(
    id:      @timeout_id,
    seconds: get_current_interval,
    method:  method(:next_image)
  )  if (current_interval)
end

#updateObject

Call this (or #update_animation) every frame, to ensure that the animation is playing.



26
27
28
# File 'lib/AdventureRL/Animation.rb', line 26

def update
  update_animation
end

#update_animationObject



21
22
23
# File 'lib/AdventureRL/Animation.rb', line 21

def update_animation
  @timing_handler.update
end