Class: Nimo::SpriteRepresentation

Inherits:
ObjectRepresentation show all
Defined in:
lib/nimo/representations/sprite_representation.rb

Instance Attribute Summary collapse

Attributes inherited from ObjectRepresentation

#game_object, #game_window

Instance Method Summary collapse

Methods inherited from ObjectRepresentation

#act_upon, #always, #listen_to, #update, #with_observer

Methods included from InputListener

#any_key, #button_down, #process_inputs, #when_key

Methods included from EventListener

#listen_to, #notify

Constructor Details

#initialize(game_window, game_object) ⇒ SpriteRepresentation

Returns a new instance of SpriteRepresentation.



7
8
9
10
11
12
13
# File 'lib/nimo/representations/sprite_representation.rb', line 7

def initialize(game_window, game_object)
  super(game_window, game_object)
  @animations = {}
  @current_animation = nil
			@next_animation = nil
  unflip
end

Instance Attribute Details

#flipObject

Returns the value of attribute flip.



5
6
7
# File 'lib/nimo/representations/sprite_representation.rb', line 5

def flip
  @flip
end

Instance Method Details

#change_to(animation_name) ⇒ Object



28
29
30
# File 'lib/nimo/representations/sprite_representation.rb', line 28

def change_to(animation_name)
	@next_animation = animation_name
end

#drawObject



32
33
34
35
36
37
38
# File 'lib/nimo/representations/sprite_representation.rb', line 32

def draw
  if @current_animation.name != @next_animation && @animations.has_key?(@next_animation)
     @current_animation = @animations[@next_animation]
     @current_animation.reset_animation
  end
  @drawing.call(@sprite_tiles[@current_animation.frame_index])
end

#load(resources, params) ⇒ Object



15
16
17
18
# File 'lib/nimo/representations/sprite_representation.rb', line 15

def load(resources, params)
			raise "Must provide :image param for sprite loading" unless params.has_key?(:image)
  @sprite_tiles = resources.image(params[:image])
end

#unflipObject



24
25
26
# File 'lib/nimo/representations/sprite_representation.rb', line 24

def unflip
  @drawing = lambda { |sprite| sprite.draw(@game_object.x, @game_object.y, 0) }
end

#with_animation(name, frame_indexes, options = {}) ⇒ Object

Adds a new animation to the sprite. The first animation will be set as the current. A list of options can be specified to customise the behavior:

  • :timeout (default is 0.1): time to wait between frames

  • :loop (default is true): true if the animation should start from the beginning, after the last element,

    otherwise it will draw the last frame
    


45
46
47
48
49
# File 'lib/nimo/representations/sprite_representation.rb', line 45

def with_animation(name, frame_indexes, options = {})
  @animations[name] = Animation.new(name, frame_indexes, options)
  @current_animation = @animations[name] if @current_animation.nil?
  self
end