class Petli::Animator

Constants

ANIMATIONS

Attributes

action[R]
mood[W]

Public Class Methods

new(hatching:, mood:, action: :walk) click to toggle source
# File lib/petli/animator.rb, line 21
def initialize(hatching:, mood:, action: :walk)
  @frame = 0
  @food = :bread
  @mood = mood
  @action = action
  if hatching
    @mood_stack = [:mezmerized, :mezmerized, :mezmerized, :mezmerized]
    @action_stack = [:egg, :egg_crack, :stand, :stand]
  else
    @mood_stack = []
    @action_stack = []
  end
end

Public Instance Methods

action=(act) click to toggle source
# File lib/petli/animator.rb, line 80
def action=(act)
  @action = act
  @frame = 0
end
busy?() click to toggle source
# File lib/petli/animator.rb, line 76
def busy?
  @busy
end
celebrate() click to toggle source
# File lib/petli/animator.rb, line 64
def celebrate
  @action_stack = [:stand, :stand, :stand, :stand]
  @mood_stack = [:mezmerized, :mezmerized, :mezmerized, :mezmerized]
  @frame = 0
end
eat(food: :bread, &block) click to toggle source
# File lib/petli/animator.rb, line 55
def eat(food: :bread, &block)
  @busy = true
  @frame = 0
  @food = food
  @action_stack = [:item, :eat, :stand, :stand, :stand, :stand]
  @mood_stack = [:mezmerized, :eating, :eating, :mezmerized, :mezmerized]
  @on_complete = block
end
embarass() click to toggle source
# File lib/petli/animator.rb, line 70
def embarass
  @action_stack = [:stand, :stand, :stand, :stand]
  @mood_stack = [:embarassed, :embarassed, :embarassed, :embarassed]
  @frame = 0
end
mood() click to toggle source
# File lib/petli/animator.rb, line 89
def mood
  @mood_stack.count > 0 ? @mood_stack[0] : @mood
end
step() click to toggle source
# File lib/petli/animator.rb, line 35
def step
  self.add_frame
  leftarm, rightarm = getit(:arms, 2)
  lefteye, righteye = getit(:eyes, 2)
  mouth = getit(:mouth)
  lefthead, righthead = getit(:head, 2)
  [leftarm,lefthead,lefteye,mouth,righteye,righthead,rightarm]

  food_peices = self.food.split('')

  ANIMATIONS[self.action][self.frame]
    .sub('a', leftarm.to_s).sub('a', rightarm.to_s)
    .sub('h', lefthead).sub('h', righthead)
    .sub('e', lefteye).sub('e', righteye)
    .sub('m', mouth)
    .reverse
    .sub('f', food_peices[2]).sub('f', food_peices[1]).sub('f', food_peices[0])
    .reverse
end

Private Instance Methods

add_frame() click to toggle source
# File lib/petli/animator.rb, line 95
def add_frame
  frame_count = ANIMATIONS[self.action].count
  @frame += 1
  if self.frame == frame_count
    @frame = 0
    @action_stack.shift if @action_stack.count > 0
    @mood_stack.shift if @mood_stack.count > 0
    if @action_stack.count == 0 && @mood_stack.count == 0
      @busy = false
      @on_complete.call unless @on_complete.nil?
      @on_complete = nil
    end
  end
end
data() click to toggle source
# File lib/petli/animator.rb, line 129
def data
  @data ||= JSON.parse(
    File.read(File.expand_path('../../data/character.json', __dir__)),
    {:symbolize_names => true}
  )
end
food() click to toggle source
# File lib/petli/animator.rb, line 110
def food
  self.data[@food]
end
frame() click to toggle source
# File lib/petli/animator.rb, line 114
def frame # to control framerate
  (@frame/3).ceil
end
getit(part, vals=1) click to toggle source
# File lib/petli/animator.rb, line 118
def getit(part, vals=1)
  mood_part = self.data[part][self.mood] || self.data[part][:default]
  part_frame = self.frame % mood_part.count
  result = mood_part[part_frame]
  if vals == 2 && result.is_a?(String)
    [result, result]
  else
    result
  end
end