Class: Goby::Monster

Inherits:
Entity show all
Includes:
Fighter
Defined in:
lib/goby/entity/monster.rb

Overview

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

Constant Summary

Constants inherited from Entity

Entity::NOT_EQUIPPED_ERROR, Entity::NO_SUCH_ITEM_ERROR

Instance Attribute Summary collapse

Attributes inherited from Entity

#escaped, #gold, #inventory, #name, #outfit

Instance Method Summary collapse

Methods included from Fighter

#add_battle_command, #add_battle_commands, #battle, #battle_commands, #choose_attack, #choose_item_and_on_whom, #has_battle_command, #print_battle_commands, #print_status, #remove_battle_command, #sample_agilities

Methods inherited from Entity

#==, #add_gold, #add_item, #add_loot, #clear_inventory, #equip_item, #has_item, #print_inventory, #print_status, #remove_gold, #remove_item, #set_gold, #set_stats, #stats, #unequip_item, #use_item

Constructor Details

#initialize(name: "Monster", stats: {}, inventory: [], gold: 0, battle_commands: [], outfit: {}, treasures: []) ⇒ Monster

Returns a new instance of Monster.

Parameters:

  • name (String) (defaults to: "Monster")

    the name.

  • stats (Hash) (defaults to: {})

    hash of stats

  • inventory ([C(Item, Integer)]) (defaults to: [])

    an array of pairs of items and their respective amounts.

  • gold (Integer) (defaults to: 0)

    the max amount of gold that can be rewarded to the opponent.

  • battle_commands ([BattleCommand]) (defaults to: [])

    the commands that can be used in battle.

  • outfit (Hash) (defaults to: {})

    the coolection of equippable items currently worn.

  • treasures ([C(Item, Integer)]) (defaults to: [])

    an array of treasures and the likelihood of receiving each.



17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

Instance Attribute Details

#total_treasuresObject (readonly)

Returns the value of attribute total_treasures.



87
88
89
# File 'lib/goby/entity/monster.rb', line 87

def total_treasures
  @total_treasures
end

#treasuresObject (readonly)

Returns the value of attribute treasures.



87
88
89
# File 'lib/goby/entity/monster.rb', line 87

def treasures
  @treasures
end

Instance Method Details

#cloneMonster

Provides a deep copy of the monster. This is necessary since the monster can use up its items in battle.

Returns:

  • (Monster)

    deep copy of the monster.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

#dieObject

What to do if the Monster dies in a Battle.



51
52
53
# File 'lib/goby/entity/monster.rb', line 51

def die
  # Do nothing special.
end

#handle_victory(fighter) ⇒ Object

What to do if a Monster wins a Battle.



56
57
58
59
# File 'lib/goby/entity/monster.rb', line 56

def handle_victory(fighter)
  # Take some of the Player's gold.
  fighter.sample_gold
end

#sample_goldObject

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

@return the amount of gold to award the victorious Entity



64
65
66
67
# File 'lib/goby/entity/monster.rb', line 64

def sample_gold
  # Sample a random amount of gold.
  Random.rand(0..@gold)
end

#sample_treasuresItem

Chooses a treasure based on the sample distribution.

Returns:

  • (Item)

    the reward for the victor of the battle (or nil - no treasure).



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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