Class: Nyan::Stage

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

Constant Summary collapse

SPACE =
"\x20"
NOTHING =
""

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.



10
11
12
13
14
15
16
# File 'lib/nyan/stage.rb', line 10

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.



8
9
10
# File 'lib/nyan/stage.rb', line 8

def height
  @height
end

#outputObject (readonly)

Returns the value of attribute output.



8
9
10
# File 'lib/nyan/stage.rb', line 8

def output
  @output
end

#widthObject (readonly)

Returns the value of attribute width.



8
9
10
# File 'lib/nyan/stage.rb', line 8

def width
  @width
end

Instance Method Details

#add_sprite(sprite) ⇒ Object



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

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

#animate!Object



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

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

#bottomObject



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

def bottom
  height - 1
end

#clean!Object



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

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

#coloured?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/nyan/stage.rb', line 66

def coloured?
  @coloured
end

#leftObject



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

def left
  0
end

#play!Object



30
31
32
33
34
# File 'lib/nyan/stage.rb', line 30

def play!
  render
  animate!
  clean!
end

#remove_sprite(sprite) ⇒ Object



26
27
28
# File 'lib/nyan/stage.rb', line 26

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

#renderObject



36
37
38
39
40
# File 'lib/nyan/stage.rb', line 36

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

#rightObject



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

def right
  width - 1
end

#spritesObject



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

def sprites
  @sprites ||= []
end

#topObject



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

def top
  0
end