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



40
41
42
# File 'lib/petli/pet/food.rb', line 40

def clean
  self.poops = []
end

#feed(food: :bread) ⇒ Object



19
20
21
22
23
24
# File 'lib/petli/pet/food.rb', line 19

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



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

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

#hunger(hr_ago = hours_ago(1)) ⇒ Object



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

def hunger(hr_ago=hours_ago(1))
  self.health = [1, self.health-1].max
  self.happiness = [1, self.happiness-1].max
  self.poops = self.poops + [hr_ago] if rand <= 0.4 && self.poops.count < POOP_LOCATIONS.count
  self.sick = [self.poops.filter{|poop| hours_since(poop) > 1 }.count, 3].min
  self.last_meal = Time.now
end

#update_hungerObject



13
14
15
16
17
# File 'lib/petli/pet/food.rb', line 13

def update_hunger
  for_hours_since(last_meal) do |i, hr_ago|
    hunger(hr_ago) if rand >= 0.3
  end
end