Module: Petli::Pet::Food

Extended by:
Tatty::DB::Attributes
Included in:
Petli::Pet
Defined in:
lib/petli/pet/food.rb

Constant Summary collapse

POOP_LOCATIONS =
[[1,1], [1,4], [1,7], [11,1], [11,7], [20,1], [20,4], [20,7]]

Instance Method Summary collapse

Methods included from Tatty::DB::Attributes

db_attr

Instance Method Details

#cleanObject



37
38
39
# File 'lib/petli/pet/food.rb', line 37

def clean
  self.poops = []
end

#feed(food: :bread) ⇒ Object



24
25
26
27
28
29
# File 'lib/petli/pet/food.rb', line 24

def feed(food: :bread)
  return self.embarass if ((food == :medicine && self.sick <= 0) || (self.health == 10 && food != :medicine))
  self.last_meal = Time.now unless food == :medicine
  react("eat_#{food}")
  self.feed!(food: food)
end

#feed!(food: :bread) ⇒ Object



31
32
33
34
35
# File 'lib/petli/pet/food.rb', line 31

def feed!(food: :bread)
  self.health = [10, self.health+2].min unless food == :medicine
  self.happiness = [10, self.happiness+2].min if food == :candy
  self.sick = [0, self.sick - 1].max if food == :medicine
end

#update_hungerObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/petli/pet/food.rb', line 13

def update_hunger
  for_hours_since(last_meal) do |i, hr_ago|
    self.last_meal = Time.now
    next if rand <= 0.3
    self.health = [1, self.health-1].max
    self.happiness = [1, self.happiness-1].max
    self.poops << hr_ago if rand <= 0.8 && self.poops.count < POOP_LOCATIONS.count
    self.sick = self.poops.filter{|poop| hours_since(poop) > 1 }.count
  end
end