class Petli::Pet

Constants

MOOD_SCALE

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/petli/pet.rb, line 18
def initialize
  super()
  @animation = Animator.new(
    hatching: (Time.now - self.birth) < 10,
    mood: self.mood,
    action: self.died_at.nil? ? :walk : :death
  )
end

Public Instance Methods

busy?() click to toggle source
# File lib/petli/pet.rb, line 109
def busy?
  @animation.busy?
end
celebrate() click to toggle source
# File lib/petli/pet.rb, line 117
def celebrate
  @animation.celebrate
end
check_if_dead() click to toggle source
# File lib/petli/pet.rb, line 62
def check_if_dead
  not_happy = self.happiness <= 1
  not_healthy = self.health <= 1
  is_sick = self.sick > 2
  too_much_time = days_since(self.last_meal) >= 1

  if not_happy && not_healthy && (is_sick || too_much_time)
    self.died_at = Time.now
    @animation.action = :death
  end
end
clean() click to toggle source
# File lib/petli/pet.rb, line 105
def clean
  self.poops = []
end
dead?() click to toggle source
# File lib/petli/pet.rb, line 113
def dead?
  @animation.action == :death
end
display() click to toggle source
# File lib/petli/pet.rb, line 31
def display
  return @animation.step if self.dead?

  if hours_since(self.last_meal) > 1
    hours_past = hours_since(self.last_meal)
    self.last_meal = Time.now
    (0...hours_past).each do |i|
      self.health = [1, self.health-1].max
      self.happiness = [1, self.happiness-1].max
      self.poop(hours_past - i) if rand <= 0.8
    end
    self.sick = self.poops.filter{|poop| hours_since(poop.hatch) > 1 }.count
  end

  if hours_since(self.last_play) > 1
    hours_past = hours_since(self.last_play)
    self.last_play = Time.now
    (0...hours_past).each do
      self.happiness = [1, self.happiness-1].max
    end
  end

  self.happiness = [self.happiness, 5].min if self.health <= 3
  self.mood = MOOD_SCALE[((self.happiness.to_f/10.0)*(MOOD_SCALE.count - 1)).floor ]

  self.check_if_dead

  @animation.mood = self.sick > 0 ? :sick : self.mood
  @animation.step
end
embarass() click to toggle source
# File lib/petli/pet.rb, line 121
def embarass
  @animation.embarass
end
feed(food: :bread) click to toggle source
# File lib/petli/pet.rb, line 74
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
  @animation.eat(food: food) do
    self.feed!(food)
  end
end
feed!(food: :bread) click to toggle source
# File lib/petli/pet.rb, line 82
def feed!(food: :bread)
  self.health = [10, self.health+1].min unless food == :medicine
  self.happiness = [10, self.happiness+1].min if food == :candy
  self.sick = [0, self.sick - 1].max if food == :medicine
end
lifetime() click to toggle source
# File lib/petli/pet.rb, line 125
def lifetime
  if self.died_at.nil?
    days_since(self.birth).to_i
  else
    days_since(self.birth, self.died_at).to_i
  end
end
lose() click to toggle source
# File lib/petli/pet.rb, line 92
def lose
  # todo I dont really want it to become more unhappy if they lose
end
play(game: :dice) click to toggle source
# File lib/petli/pet.rb, line 96
def play(game: :dice)
  self.last_play = Time.now
  @animation.action = :stand
end
poop(hours_ago) click to toggle source
# File lib/petli/pet.rb, line 101
def poop(hours_ago)
  self.poops = self.poops + [Poop.new(hours_ago)] if self.poops.count < Poop::LOCATIONS.count
end
reset() click to toggle source
# File lib/petli/pet.rb, line 27
def reset
  @animation.action = :walk
end
win() click to toggle source
# File lib/petli/pet.rb, line 88
def win
  self.happiness = [10, self.happiness+1].min
end