Class: Sabrina::Plugins::Spritesheet
- Inherits:
-
Sabrina::Plugin
- Object
- Sabrina::Plugin
- Sabrina::Plugins::Spritesheet
- Includes:
- ChildrenManager
- Defined in:
- lib/sabrina/plugins/spritesheet.rb
Overview
This Sabrina::Plugin provides an abstraction for manipulating all Sprites and Palettes associated with a Monster.
Constant Summary collapse
- ENHANCES =
Monster
- PLUGIN_NAME =
'Spritesheet'
- SHORT_NAME =
'spritesheet'
- FEATURES =
Set.new [:reread, :write, :save, :load]
Instance Attribute Summary collapse
-
#index ⇒ Integer
The real index of the monster.
-
#palettes ⇒ Array
readonly
The palettes used by the sprites.
-
#rom ⇒ Rom
The current working ROM file.
-
#sprites ⇒ Array
readonly
The sprites.
Instance Method Summary collapse
- #children ⇒ Array
-
#initialize(monster) ⇒ Spritesheet
constructor
Generates a new Spritesheet object.
-
#justify ⇒ Object
Justify sprites to the target ROM count as per ROM Config, assuming 64x64 frame size.
-
#load(file = @monster.filename, dir = @monster.work_dir, *_args) ⇒ Array
Load data from a file.
-
#save(file = @monster.filename, dir = @monster.work_dir, *_args) ⇒ Array
Save data to a file.
Methods included from ChildrenManager
Methods inherited from Sabrina::Plugin
#feature?, feature_all, feature_this, features, inherited, plugin_name, #reread, short_name, target, to_s, #to_s, #write
Constructor Details
#initialize(monster) ⇒ Spritesheet
Generates a new Spritesheet object.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/sabrina/plugins/spritesheet.rb', line 51 def initialize(monster) @monster = monster @rom = monster.rom @index = monster.index @palettes = [ Palette.from_table(@monster.rom, :palette_table, @monster.index), Palette.from_table(@monster.rom, :shinypal_table, @monster.index) ] @sprites = [ Sprite.from_table(@monster.rom, :front_table, @monster.index), Sprite.from_table(@monster.rom, :back_table, @monster.index) ] end |
Instance Attribute Details
#index ⇒ Integer
The real index of the monster.
36 |
# File 'lib/sabrina/plugins/spritesheet.rb', line 36 attr_children :rom, :index |
#palettes ⇒ Array (readonly)
The palettes used by the sprites.
41 42 43 |
# File 'lib/sabrina/plugins/spritesheet.rb', line 41 def palettes @palettes end |
#rom ⇒ Rom
The current working ROM file.
36 |
# File 'lib/sabrina/plugins/spritesheet.rb', line 36 attr_children :rom, :index |
#sprites ⇒ Array (readonly)
The sprites.
46 47 48 |
# File 'lib/sabrina/plugins/spritesheet.rb', line 46 def sprites @sprites end |
Instance Method Details
#children ⇒ Array
69 70 71 |
# File 'lib/sabrina/plugins/spritesheet.rb', line 69 def children @sprites + @palettes end |
#justify ⇒ Object
Justify sprites to the target ROM count as per ROM Config, assuming 64x64 frame size.
75 76 77 |
# File 'lib/sabrina/plugins/spritesheet.rb', line 75 def justify @sprites.map(&:justify) end |
#load(file = @monster.filename, dir = @monster.work_dir, *_args) ⇒ Array
Load data from a file.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/sabrina/plugins/spritesheet.rb', line 82 def load(file = @monster.filename, dir = @monster.work_dir, *_args) path = get_path(file, dir) a = split_canvas(ChunkyPNG::Canvas.from_file(path)) h = { rom: @rom, index: @index } normal_rgb = a[0].to_rgb_stream + a[2].to_rgb_stream shiny_rgb = a[1].to_rgb_stream + a[3].to_rgb_stream @palettes = Palette.create_synced_palettes( normal_rgb, shiny_rgb, h.merge(table: :palette_table), h.merge(table: :shinypal_table) ) frames = @rom.special_frames.fetch(@index, @rom.frames) front = crop_canvas(a[0], frames.first * 64) back = crop_canvas(a[2], frames.last * 64) @sprites = [ Sprite.from_canvas( front, @palettes.first, h.merge(table: :front_table) ), Sprite.from_canvas( back, @palettes.first, h.merge(table: :back_table) ) ] justify children end |
#save(file = @monster.filename, dir = @monster.work_dir, *_args) ⇒ Array
Save data to a file.
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/sabrina/plugins/spritesheet.rb', line 121 def save(file = @monster.filename, dir = @monster.work_dir, *_args) a = [] @sprites.product(@palettes).each do |x| a << x.first.to_canvas(x.last) end path = get_path(file, dir, mkdir: true) combine_canvas(a).save(path) end |