Class: Sabrina::Monster

Inherits:
Object
  • Object
show all
Includes:
ChildrenManager
Defined in:
lib/sabrina/monster.rb

Overview

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

See Plugins for extensions that might enhance this class with added functionality.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ChildrenManager

included

Constructor Details

#initialize(rom, index, dir = nil) ⇒ Monster

Generates a new instance of Monster.

Parameters:

  • rom (Rom)

    the ROM to be associated with the monster.

  • index (Integer)

    either the dex number of the monster, or the real (ROM) index prefixed with “!”. See parse_index for details.

  • dir (String) (defaults to: nil)

    the working directory.



86
87
88
89
90
91
92
93
94
95
96
97
# 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

Instance Attribute Details

#dex_numberObject

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



42
43
44
# File 'lib/sabrina/monster.rb', line 42

def dex_number
  @dex_number
end

#filenameString (readonly)

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.

Returns:

  • (String)


31
32
33
# File 'lib/sabrina/monster.rb', line 31

def filename
  @filename
end

#indexInteger

The real index of the monster.

Returns:

  • (Integer)


24
# File 'lib/sabrina/monster.rb', line 24

attr_children :rom, :index

#romRom

The current working ROM file.

Returns:



24
# File 'lib/sabrina/monster.rb', line 24

attr_children :rom, :index

#work_dirString

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

Returns:

  • (String)

See Also:



38
39
40
# File 'lib/sabrina/monster.rb', line 38

def work_dir
  @work_dir
end

Class Method Details

.parse_index(index, rom) ⇒ Integer

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.

Parameters:

  • index (Integer, String)

    either the dex number, or the real index prepended with “!”.

  • rom (Rom)

    the rom file to pull the dex data from.

Returns:

  • (Integer)

    the real index of the monster in the ROM file, after accounting for blank spaces between dex numbers 251 and 252.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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

Instance Method Details

#childrenArray

Returns:

  • (Array)

See Also:



101
102
103
# File 'lib/sabrina/monster.rb', line 101

def children
  @plugins.values
end

#close0

Closes the ROM file.

Returns:

  • (0)


135
136
137
138
# File 'lib/sabrina/monster.rb', line 135

def close
  @rom.close
  0
end

#nameString

Reads the monster name from the ROM.

Returns:

  • (String)


128
129
130
# File 'lib/sabrina/monster.rb', line 128

def name
  @rom.monster_name(@index)
end

#to_sString

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

Returns:

  • (String)


143
144
145
# File 'lib/sabrina/monster.rb', line 143

def to_s
  "#{@index}. #{name}"
end