class Goby::Monster
An Entity
controlled by the CPU. Used for battle against Players.
Attributes
Public Class Methods
@param [String] name the name. @param [Hash] stats hash of stats @param [[C(Item
, Integer
)]] inventory an array of pairs of items and their respective amounts. @param [Integer] gold the max amount of gold that can be rewarded to the opponent. @param [[BattleCommand]] battle_commands the commands that can be used in battle. @param [Hash] outfit the coolection of equippable items currently worn. @param [[C(Item
, Integer
)]] treasures an array of treasures and the likelihood of receiving each.
Goby::Entity::new
# File lib/goby/entity/monster.rb, line 17 def initialize(name: "Monster", stats: {}, inventory: [], gold: 0, battle_commands: [], outfit: {}, treasures: []) super(name: name, stats: stats, inventory: inventory, gold: gold, outfit: outfit) @treasures = treasures # Find the total number of treasures in the distribution. @total_treasures = 0 @treasures.each do |pair| @total_treasures += pair.second end add_battle_commands(battle_commands) end
Public Instance Methods
Provides a deep copy of the monster. This is necessary since the monster can use up its items in battle.
@return [Monster] deep copy of the monster.
# File lib/goby/entity/monster.rb, line 35 def clone # Create a shallow copy for most of the variables. monster = super # Reset the copy's inventory. monster.inventory = [] # Create a deep copy of the inventory. @inventory.each do |pair| monster.inventory << C[pair.first.clone, pair.second] end return monster end
Chooses a treasure based on the sample distribution.
@return [Item] the reward for the victor of the battle (or nil - no treasure).
# File lib/goby/entity/monster.rb, line 72 def sample_treasures # Return nil for no treasures. return if total_treasures.zero? # Choose uniformly from the total given above. index = Random.rand(total_treasures) # Choose the treasure based on the distribution. total = 0 treasures.each do |pair| total += pair.second return pair.first if index < total end end