Class: Nyan::Stage

Inherits:
Object
  • Object
show all
Includes:
Shape
Defined in:
lib/nyan/stage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Shape

#inside?, #outside?

Constructor Details

#initialize(options = {}) ⇒ Stage

Returns a new instance of Stage.



7
8
9
10
11
12
13
# File 'lib/nyan/stage.rb', line 7

def initialize(options = {})
  @output   = options.fetch(:output, $stdout)
  @height   = options.fetch(:height, ANSI::Terminal.terminal_height)
  @width    = options.fetch(:width, ANSI::Terminal.terminal_width)
  @coloured = options.fetch(:coloured, true)
  clear_buffer
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



5
6
7
# File 'lib/nyan/stage.rb', line 5

def height
  @height
end

#outputObject (readonly)

Returns the value of attribute output.



5
6
7
# File 'lib/nyan/stage.rb', line 5

def output
  @output
end

#widthObject (readonly)

Returns the value of attribute width.



5
6
7
# File 'lib/nyan/stage.rb', line 5

def width
  @width
end

Instance Method Details

#add_sprite(sprite) ⇒ Object



19
20
21
# File 'lib/nyan/stage.rb', line 19

def add_sprite(sprite)
  self.sprites.push(sprite)
end

#animate!Object



39
40
41
# File 'lib/nyan/stage.rb', line 39

def animate!
  sprites.each { |s| s.animate! }
end

#bottomObject



51
52
53
# File 'lib/nyan/stage.rb', line 51

def bottom
  height - 1
end

#clean!Object



43
44
45
# File 'lib/nyan/stage.rb', line 43

def clean!
  sprites.delete_if { |s| s.outside?(self) }
end

#coloured?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/nyan/stage.rb', line 63

def coloured?
  @coloured
end

#leftObject



55
56
57
# File 'lib/nyan/stage.rb', line 55

def left
  0
end

#play!Object



27
28
29
30
31
# File 'lib/nyan/stage.rb', line 27

def play!
  render
  animate!
  clean!
end

#remove_sprite(sprite) ⇒ Object



23
24
25
# File 'lib/nyan/stage.rb', line 23

def remove_sprite(sprite)
  self.sprites.delete(sprite)
end

#renderObject



33
34
35
36
37
# File 'lib/nyan/stage.rb', line 33

def render
  clear_buffer
  sprites.sort.reverse.each { |s| write_to_buffer(s) }
  print_buffer
end

#rightObject



59
60
61
# File 'lib/nyan/stage.rb', line 59

def right
  width - 1
end

#spritesObject



15
16
17
# File 'lib/nyan/stage.rb', line 15

def sprites
  @sprites ||= []
end

#topObject



47
48
49
# File 'lib/nyan/stage.rb', line 47

def top
  0
end