class Egg

Constants

DEFAULT_TOUCHES
KONAMI

Attributes

current_code[RW]
number_touches[RW]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/motion-egg/egg.rb, line 12
def initialize (opts = {})
  @options = {secret_code: KONAMI, number_touches: DEFAULT_TOUCHES, image_file: "toasty.png"}.merge(opts)
end

Public Instance Methods

activate() click to toggle source
# File lib/motion-egg/egg.rb, line 16
def activate
  #p "************* OMG THE CODEZ! *************"
  insert_egg
  show_egg
  NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: 'hide_egg:', userInfo: nil, repeats: false)
end
add_code(new_direction) click to toggle source
# File lib/motion-egg/egg.rb, line 62
def add_code new_direction
  @current_code ||= Array.new
  @current_code.push(new_direction)

  # Only keep the last @current_code.size entries
  @current_code = @current_code[-@options[:secret_code].size..-1] if @current_code.size > @options[:secret_code].size 
  #p @current_code
  activate if @options[:secret_code] == @current_code
end
egg_height() click to toggle source
# File lib/motion-egg/egg.rb, line 84
def egg_height
  180
end
egg_width() click to toggle source
# File lib/motion-egg/egg.rb, line 88
def egg_width
  UIScreen.mainScreen.bounds.size.width
end
hidden_frame() click to toggle source
# File lib/motion-egg/egg.rb, line 76
def hidden_frame
  [[0,UIScreen.mainScreen.bounds.size.height + egg_height],[egg_width, egg_height]]
end
hide_egg(sender) click to toggle source
# File lib/motion-egg/egg.rb, line 36
def hide_egg(sender)
  # Animate the egg out of the view:
  UIView.animateWithDuration(0.5,
    delay:0.0,
    options:UIViewAnimationOptionCurveLinear,
    animations: lambda {
      @egg_view.frame = hidden_frame
    },
    completion:lambda {|finished|
      #consider removing egg subview here.
    }
  )
end
insert_egg() click to toggle source
# File lib/motion-egg/egg.rb, line 50
def insert_egg
  @egg_view ||= begin
    egg_view = UIImageView.alloc.initWithFrame(hidden_frame)
    egg_view.contentMode = UIViewContentModeScaleAspectFit
    egg_view.image = UIImage.imageNamed(@options[:image_file])
    egg_view
  end
 
  # We want to add the view to the root view
  UIApplication.sharedApplication.keyWindow.subviews.objectAtIndex(0).nextResponder.view.addSubview(@egg_view)
end
show_egg(timing = 0.5) click to toggle source
# File lib/motion-egg/egg.rb, line 23
def show_egg(timing = 0.5)
  # Animate the egg into the view:
  UIView.animateWithDuration(timing,
    delay:0.0,
    options:UIViewAnimationOptionCurveLinear,
    animations: lambda {
      @egg_view.frame = show_frame
    },
    completion:lambda {|finished|
    }
  )
end
show_frame() click to toggle source
# File lib/motion-egg/egg.rb, line 80
def show_frame
  [[0,UIScreen.mainScreen.bounds.size.height - egg_height],[egg_width, egg_height]]
end