class Patterns::Monster
The monster class. It delegates its calls to the monster type class. This is an example of the Type Object pattern. This is also an example of the concept of Object Composition over inheritance. The monster HAS a type attribute instead of inheriting one. This allows us to decouple the monster from its type and stops inheritance from getting out of control in a system with many similar objects.
Attributes
The MonsterType object of the Monster
Public Class Methods
Initializes the monster class
-
monster_type
- The type of the monster that has a corresponding json file
Examples
=> orc = Monster.new('orc')
# File app/models/type_object/monster.rb, line 21 def initialize(monster_type) @monster_type = MonsterType.new(monster_type) end
Public Instance Methods
Delegates getting monster attack to the #monster_type
Examples
=> orc.attack
# File app/models/type_object/monster.rb, line 48 def attack @monster_type.attack end
Performs a deep clone of the Monster Object by utilizing the MonsterType object
Examples
=> orc2 = orc.clone
# File app/models/type_object/monster.rb, line 30 def deep_clone Monster.new(@monster_type) end
Delegates getting monster health to the #monster_type
Examples
=> orc.health
# File app/models/type_object/monster.rb, line 39 def health @monster_type.health end
Gets this monster's prototype name if it has one and the string 'none' if it does not
Examples
=> orc_wizard.prototype_name
# File app/models/type_object/monster.rb, line 84 def prototype_name @monster_type.prototype_name end
Delegates getting monster range to the #monster_type
Examples
=> orc.range
# File app/models/type_object/monster.rb, line 57 def range @monster_type.range end
Delegates getting monster resistances to the #monster_type
Examples
=> orc.resistances
# File app/models/type_object/monster.rb, line 66 def resistances @monster_type.resistances end
Delegates getting monster weaknesses to the #monster_type
Examples
=> orc.weaknesses
# File app/models/type_object/monster.rb, line 75 def weaknesses @monster_type.weaknesses end