Class: Goby::Food

Inherits:
Item
  • Object
show all
Defined in:
lib/goby/item/food.rb

Overview

Recovers HP when used.

Constant Summary

Constants inherited from Item

Item::DEFAULT_USE_TEXT

Instance Attribute Summary collapse

Attributes inherited from Item

#consumable, #disposable, #name, #price

Instance Method Summary collapse

Methods inherited from Item

#==, #to_s

Constructor Details

#initialize(name: "Food", price: 0, consumable: true, disposable: true, recovers: 0) ⇒ Food

Returns a new instance of Food.

Parameters:

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

    the name.

  • price (Integer) (defaults to: 0)

    the cost in a shop.

  • consumable (Boolean) (defaults to: true)

    upon use, the item is lost when true.

  • disposable (Boolean) (defaults to: true)

    allowed to sell or drop item when true.

  • recovers (Integer) (defaults to: 0)

    the amount of HP recovered when used.



13
14
15
16
# File 'lib/goby/item/food.rb', line 13

def initialize(name: "Food", price: 0, consumable: true, disposable: true, recovers: 0)
  super(name: name, price: price, consumable: consumable, disposable: disposable)
  @recovers = recovers
end

Instance Attribute Details

#recoversObject (readonly)

The amount of HP that the food recovers.



45
46
47
# File 'lib/goby/item/food.rb', line 45

def recovers
  @recovers
end

Instance Method Details

#use(user, entity) ⇒ Object

Heals the entity.

Parameters:

  • user (Entity)

    the one using the food.

  • entity (Entity)

    the one on whom the food is used.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/goby/item/food.rb', line 22

def use(user, entity)
  if entity.stats[:hp] + recovers > entity.stats[:max_hp]
    this_recover = entity.stats[:max_hp] - entity.stats[:hp]
    heal_entity(entity, entity.stats[:max_hp])
  else
    current_hp = entity.stats[:hp]
    this_recover = @recovers
    heal_entity(entity, current_hp + this_recover)
  end

  # Helpful output.
  print "#{user.name} uses #{name}"
  if (user == entity)
    print " and "
  else
    print " on #{entity.name}!\n#{entity.name} "
  end
  print "recovers #{this_recover} HP!\n\n"
  print "#{entity.name}'s HP: #{entity.stats[:hp]}/#{entity.stats[:max_hp]}\n\n"

end