Module: Goby::Equippable

Included in:
Helmet, Legs, Shield, Torso, Weapon
Defined in:
lib/goby/item/equippable.rb

Overview

Provides methods for equipping & unequipping an Item.

Instance Method Summary collapse

Instance Method Details

#alter_stats(entity, equipping) ⇒ Object

TODO:

ensure stats cannot go below zero (but does it matter..?).

Alters the stats of the entity

Parameters:

  • entity (Entity)

    the entity equipping/unequipping the item.

  • equipping (Boolean)

    flag for when the item is being equipped or unequipped.



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.

Parameters:

  • entity (Entity)

    the entity who is equipping the equippable.



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_changeObject

The function that returns the type of the item. Subclasses must override this function.

Raises:

  • (NotImplementedError)


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

#typeObject

The function that returns the change in stats for when the item is equipped. Subclasses must override this function.

Raises:

  • (NotImplementedError)


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.

Parameters:

  • entity (Entity)

    the entity who is unequipping the equippable.



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.

Parameters:

  • user (Entity)

    the one using the item.

  • entity (Entity)

    the one on whom the item is used.



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