class Sabrina::Monster

An abstract class for dealing with further abstractions of data related to a specific, numbered monster.

See {Sabrina::Plugins} for extensions that might enhance this class with added functionality.

Attributes

dex_number[R]

The effective dex number of the monster, depending on any blanks in the ROM file.

filename[R]

The filename for saving data to a file, sans the path and extension. Refer to {#work_dir=} for setting the path, and child #save methods should take care of adding the right extension.

@return [String]

work_dir[R]

The directory for reading and saving file data. Defaults to the current ROM’s file name affixed with _files.

@return [String] @see work_dir=

Private Class Methods

new(rom, index, dir = nil) click to toggle source

Generates a new instance of Monster.

@param [Rom] rom the ROM to be associated with the monster. @param [Integer] index either the dex number of the monster,

or the real (ROM) index prefixed with "!". See {parse_index}
for details.

@param [String] dir the working directory. @return [Monster]

# File lib/sabrina/monster.rb, line 86
def initialize(rom, index, dir = nil)
  @plugins = {}

  @rom = rom
  @index = self.class.parse_index(index, @rom)
  @dex_number = index

  @filename = format('%03d', @index)
  @work_dir = dir || @rom.filename.dup << '_files/'

  load_plugins
end
parse_index(index, rom) click to toggle source

Calculates the real index by parsing the provided index either as a dex number (when integer) or as a string. A number string prefixed with an exclamation mark +“!”+ will be interpreted as the real index.

@param [Integer, String] index either the dex number, or the

real index prepended with "!".

@param [Rom] rom the rom file to pull the dex data from. @return [Integer] the real index of the monster in the ROM file,

after accounting for blank spaces between dex numbers 251 and 252.
# File lib/sabrina/monster.rb, line 55
def parse_index(index, rom)
  in_index = index.to_s
  if /[^0-9\!]/ =~ in_index
    fail "\'#{in_index}\' does not look like a valid index."
  end

  out_index =
    if in_index['!']
      in_index.rpartition('!').last.to_i
    else
      i = in_index.to_i
      i < rom.dex_blank_start ? i : i + rom.dex_blank_length
    end

  unless out_index < rom.dex_length
    fail "Real index #{out_index} out of bounds;" \
      " #{rom.id} has #{rom.dex_length} monsters."
  end

  out_index
end

Private Instance Methods

children() click to toggle source

@return [Array] @see ChildrenManager

# File lib/sabrina/monster.rb, line 101
def children
  @plugins.values
end
close() click to toggle source

Closes the ROM file.

@return [0]

# File lib/sabrina/monster.rb, line 135
def close
  @rom.close
  0
end
dex_number=(n) click to toggle source

Sets the dex number of the monster, dependent on any blanks in the ROM.

@return [Integer] @see parse_index

# File lib/sabrina/monster.rb, line 120
def dex_number=(n)
  @dex_number = n
  self.index = self.class.parse_index(n, @rom)
end
dir=(path)
Alias for: work_dir=
name() click to toggle source

Reads the monster name from the ROM.

@return [String]

# File lib/sabrina/monster.rb, line 128
def name
  @rom.monster_name(@index)
end
to_s() click to toggle source

Prints a blurb consisting of the monster’s dex number and name.

@return [String]

# File lib/sabrina/monster.rb, line 143
def to_s
  "#{@index}. #{name}"
end
work_dir=(path) click to toggle source

Sets the path for saving data to a file.

@param [String] path @return [String]

# File lib/sabrina/monster.rb, line 109
def work_dir=(path)
  path << '/' unless path.end_with?('/')
  @work_dir = path
end
Also aliased as: dir=