Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Character
- attr_accessor :strength, :level, :agility
- def initialize(level, strength, agility)
- @level, @strength, @agility = level, strength, agility
- @stat_boosts = {}
- end
- def attack_power
- stat_boost :attack_power
- end
- def stat_boost(stat)
- @stat_boosts[stat] || 0
- end
- def add_stat_boost(stat, amount)
- @stat_boosts[stat] = amount
- end
- def remove_stat_boost(stat)
- @stat_boosts.delete(stat)
- end
- end
- class Rogue < Character
- def attack_power
- strength + agility + (level * 2) - 20 + super
- end
- end
- class Druid < Character
- VALID_FORMS = [:bear, :cat, :moonkin]
- def attack_power
- super + case form
- when :bear
- (strength * 2) + (level * 3) - 20
- when :cat
- (strength * 2) + agility + (level * 2) - 20
- when :moonkin
- (strength * 2) + (level * 1.5) - 20
- end
- end
- def form=(form)
- @form = VALID_FORMS.find {|f| f == (form) } || raise("no such form")
- end
- attr_reader :form
- end
- progue = Rogue.new(120, 700, 70)
- progue.add_stat_boost :attack_power, 200
- puts progue.attack_power
- # => 1190
- progue.remove_stat_boost :attack_power
- puts progue.attack_power
- # => 990
- durid = Druid.new(200, 600, 70)
- durid.form = :cat
- puts durid.attack_power
- # => 1650
Add Comment
Please, Sign In to add comment