Module: Petli::Pet::Animation

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

Instance Method Summary collapse

Methods included from Tatty::DB::Attributes

db_attr

Instance Method Details

#animationObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/petli/pet/animation.rb', line 60

def animation
  if self.dead?
    avail_animations[:death]
  elsif !@doing.nil?
    @doing
  elsif self.lights_out
    if sleeping?
      avail_animations[:sleep][:sleep]
    else
      avail_animations[:sleep][:blink]
    end
  elsif self.sick > 0
    atlas[:walk_sick]
  else
    atlas["walk_#{self.mood}"]
  end
end

#atlasObject



78
79
80
# File 'lib/petli/pet/animation.rb', line 78

def atlas
  avail_animations[phase][phase_select]
end

#avail_animationsObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/petli/pet/animation.rb', line 82

def avail_animations
  @avail_animations ||= {
    hatch: Tatty::Anim.from_atlas(Petli.data_path('hatch.txtanim')),
    infant: Dir[Petli.data_path('infant',"*.txtanim")].map {|p| Tatty::Atlas.new(p) },
    teen: Dir[Petli.data_path('teen',"*.txtanim")].map {|p| Tatty::Atlas.new(p) },
    adult: Dir[Petli.data_path('adult',"*.txtanim")].map {|p| Tatty::Atlas.new(p) },
    death: Tatty::Anim.from_atlas(Petli.data_path('death.txtanim')),
    sleep: Tatty::Atlas.new(Petli.data_path('sleep.txtanim')),
  }
end

#busy?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/petli/pet/animation.rb', line 28

def busy?
  !@doing.nil?
end

#celebrateObject



32
33
34
# File 'lib/petli/pet/animation.rb', line 32

def celebrate
  react(:celebrate)
end

#embarassObject



36
37
38
# File 'lib/petli/pet/animation.rb', line 36

def embarass
  react(:embarass)
end

#hatchObject



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

def hatch
  @doing = avail_animations[:hatch]
end

#light_switchObject



44
45
46
47
# File 'lib/petli/pet/animation.rb', line 44

def light_switch
  self.lights_out = !lights_out
  self.happiness = [1, self.happiness-1].max if self.lights_out && !sleeping?
end

#react(action) ⇒ Object



54
55
56
57
58
# File 'lib/petli/pet/animation.rb', line 54

def react(action)
  anim = atlas[action]
  anim.reset
  @doing = anim
end

#resetObject



24
25
26
# File 'lib/petli/pet/animation.rb', line 24

def reset
  @doing = nil
end

#setup_animationObject



9
10
11
# File 'lib/petli/pet/animation.rb', line 9

def setup_animation
  hatch if (Time.now - self.birth) < 10
end

#sleeping?Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/petli/pet/animation.rb', line 49

def sleeping?
  hour = Time.now.hour
  hour >= 16 || hour <= 8
end

#update_animationObject



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

def update_animation
  reset if animation.step && !animation.loop
  if phase == :infant && hours_since(birth) > 1
    self.phase = :teen
    self.phase_select = rand(0...avail_animations[phase].count)
  elsif phase == :teen && days_since(birth) > 1
    self.phase = :adult
    self.phase_select = rand(0...avail_animations[phase].count)
  end
end