class Goby::Monster

An Entity controlled by the CPU. Used for battle against Players.

Attributes

total_treasures[R]
treasures[R]

Public Class Methods

new(name: "Monster", stats: {}, inventory: [], gold: 0, battle_commands: [], outfit: {}, treasures: []) click to toggle source

@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.

Calls superclass method 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

clone() click to toggle source

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.

Calls superclass method
# 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
die() click to toggle source

What to do if the Monster dies in a Battle.

# File lib/goby/entity/monster.rb, line 51
def die
  # Do nothing special.
end
handle_victory(fighter) click to toggle source

What to do if a Monster wins a Battle.

# File lib/goby/entity/monster.rb, line 56
def handle_victory(fighter)
  # Take some of the Player's gold.
  fighter.sample_gold
end
sample_gold() click to toggle source

The amount gold given to a victorious Entity after losing a battle

@return the amount of gold to award the victorious Entity

# File lib/goby/entity/monster.rb, line 64
def sample_gold
  # Sample a random amount of gold.
  Random.rand(0..@gold)
end
sample_treasures() click to toggle source

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