Module: Goby::Equippable
Overview
Provides methods for equipping & unequipping an Item.
Instance Method Summary collapse
-
#alter_stats(entity, equipping) ⇒ Object
Alters the stats of the entity.
-
#equip(entity) ⇒ Object
Equips onto the entity and changes the entity’s attributes accordingly.
-
#stat_change ⇒ Object
The function that returns the type of the item.
-
#type ⇒ Object
The function that returns the change in stats for when the item is equipped.
-
#unequip(entity) ⇒ Object
Unequips from the entity and changes the entity’s attributes accordingly.
-
#use(user, entity) ⇒ Object
The function that executes when one uses the equippable.
Instance Method Details
#alter_stats(entity, equipping) ⇒ Object
ensure stats cannot go below zero (but does it matter..?).
Alters the stats of the entity
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/goby/item/equippable.rb', line 25 def alter_stats(entity, equipping) stats_to_change = entity.stats.dup affected_stats = [:attack, :defense, :agility, :max_hp] operator = equipping ? :+ : :- affected_stats.each do |stat| stats_to_change[stat]= stats_to_change[stat].send(operator, stat_change[stat]) if stat_change[stat] end entity.set_stats(stats_to_change) #do not kill entity by unequipping if entity.stats[:hp] < 1 entity.set_stats(hp: 1) end end |
#equip(entity) ⇒ Object
Equips onto the entity and changes the entity’s attributes accordingly.
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/goby/item/equippable.rb', line 45 def equip(entity) prev_item = entity.outfit[type] entity.outfit[type] = self alter_stats(entity, true) if prev_item prev_item.alter_stats(entity, false) entity.add_item(prev_item) end print "#{entity.name} equips #{@name}!\n\n" end |
#stat_change ⇒ Object
The function that returns the type of the item. Subclasses must override this function.
9 10 11 |
# File 'lib/goby/item/equippable.rb', line 9 def stat_change raise(NotImplementedError, 'An Equippable Item must implement a stat_change Hash') end |
#type ⇒ Object
The function that returns the change in stats for when the item is equipped. Subclasses must override this function.
16 17 18 |
# File 'lib/goby/item/equippable.rb', line 16 def type raise(NotImplementedError, 'An Equippable Item must have a type') end |
#unequip(entity) ⇒ Object
Unequips from the entity and changes the entity’s attributes accordingly.
62 63 64 65 66 67 |
# File 'lib/goby/item/equippable.rb', line 62 def unequip(entity) entity.outfit.delete(type) alter_stats(entity, false) print "#{entity.name} unequips #{@name}!\n\n" end |
#use(user, entity) ⇒ Object
The function that executes when one uses the equippable.
73 74 75 |
# File 'lib/goby/item/equippable.rb', line 73 def use(user, entity) print "Type 'equip #{@name}' to equip this item.\n\n" end |