Class: Nyan::Flight

Inherits:
Object
  • Object
show all
Defined in:
lib/nyan/flight.rb

Instance Method Summary collapse

Constructor Details

#initializeFlight

Returns a new instance of Flight.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/nyan/flight.rb', line 3

def initialize
  @stage   = Stage.new
  
  @rainbow  = Sprite.new(:x => 0, :y => 17, :z => 1, :frames => Nyan::Rainbow.to_frames)

  @cat_head = Sprite.new(:x        => Nyan::Rainbow::TOTAL_LENGTH - 2,
                        :y         => 17,
                        :z         => 100,
                        :frames    => Nyan::CatHead.to_frames,
                        :animation => Proc.new do
                          @initial_x ||= @x
                          @initial_y ||= @y
                          case offset = [@x == @initial_x && :initial || :offset, @y == @initial_y && :initial || :offset]
                          when [:initial, :initial] then @y -= 2
                          when [:initial, :offset]  then @x += 2
                          when [:offset, :offset]   then @y += 2
                          when [:offset, :initial]  then @x -= 2
                          end
                        end)

  @pop_tart = Sprite.new(:x => @cat_head.x - 20,
                        :y => @cat_head.y - 2,
                        :z => @cat_head.z - 1,
                        :frames => Nyan::PopTart.to_frames)
  
  @stage.add_sprite(@rainbow)
  @stage.add_sprite(@cat_head)
  @stage.add_sprite(@pop_tart)
  @tick_time = Time.now
end

Instance Method Details

#next!Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/nyan/flight.rb', line 34

def next!
  if rand > 0.85
    speed = rand > 0.5 ? 2 : 5
    star = Sprite.new(:x           => @stage.width,
                      :y           => rand(@stage.height - 11),
                      :z           => 0,
                      :speed       => speed,
                      :frames      => Nyan::Star.to_frames,
                      :frame_index => rand(Nyan::Star.to_frames.length - 1),
                      :animation   => Proc.new { self.x -= @options[:speed] })
    @stage.add_sprite(star)
  end
  @stage.play!
  loop until Time.now - @tick_time >= 0.09
  @tick_time = Time.now
end