Class: Goby::Monster
- 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
-
#total_treasures ⇒ Object
readonly
Returns the value of attribute total_treasures.
-
#treasures ⇒ Object
readonly
Returns the value of attribute treasures.
Attributes inherited from Entity
#escaped, #gold, #inventory, #name, #outfit
Instance Method Summary collapse
-
#clone ⇒ Monster
Provides a deep copy of the monster.
-
#die ⇒ Object
What to do if the Monster dies in a Battle.
-
#handle_victory(fighter) ⇒ Object
What to do if a Monster wins a Battle.
-
#initialize(name: "Monster", stats: {}, inventory: [], gold: 0, battle_commands: [], outfit: {}, treasures: []) ⇒ Monster
constructor
A new instance of Monster.
-
#sample_gold ⇒ Object
The amount gold given to a victorious Entity after losing a battle.
-
#sample_treasures ⇒ Item
Chooses a treasure based on the sample distribution.
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.
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_treasures ⇒ Object (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 |
#treasures ⇒ Object (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
#clone ⇒ Monster
Provides a deep copy of the monster. This is necessary since the monster can use up its items in battle.
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 |
#die ⇒ Object
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_gold ⇒ Object
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_treasures ⇒ Item
Chooses a treasure based on the sample distribution.
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 |