Class: Airsprite::Sprite

Inherits:
Object
  • Object
show all
Defined in:
lib/airsprite/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sheet, name, path) ⇒ Sprite

Returns a new instance of Sprite.



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/airsprite/base.rb', line 170

def initialize(sheet, name, path)
  @name = name
  @animations = []

  if File.directory?(path)
    Dir["#{path}*/"].each do |dir|
      @animations += [SpriteAnimation.new(dir.split('/').last, dir)]
    end
  else
    @animations += [SpriteAnimation.new('idle', path)]
  end

  if @animations.map(&:frames).flatten.map(&:width).uniq.size > 1
    raise "Widths of frames for a sprite must be the same width"
  end

  if @animations.map(&:frames).flatten.map(&:height).uniq.size > 1
    raise "Widths of frames for a sprite must be the same height"
  end
end

Instance Attribute Details

#animationsObject

Returns the value of attribute animations.



168
169
170
# File 'lib/airsprite/base.rb', line 168

def animations
  @animations
end

#nameObject

Returns the value of attribute name.



167
168
169
# File 'lib/airsprite/base.rb', line 167

def name
  @name
end

#sheetObject

Returns the value of attribute sheet.



167
168
169
# File 'lib/airsprite/base.rb', line 167

def sheet
  @sheet
end

Instance Method Details

#to_sObject



191
192
193
194
195
196
197
198
199
200
# File 'lib/airsprite/base.rb', line 191

def to_s
  <<-EOS
  Sprite
  {
name "#{name}"

#{animations.map(&:to_s).join}
  }
  EOS
end