Class: Nyan::Sprite

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Shape

#inside?, #outside?

Constructor Details

#initialize(options = {}, &blk) ⇒ Sprite

Returns a new instance of Sprite.



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

def initialize(options = {}, &blk)
  @options = options
  self.x = @options.fetch(:x, 0)
  self.y = @options.fetch(:y, 0)
  self.z = @options.fetch(:z, 0)
  self.frames      = @options.fetch(:frames, [])
  self.frame_index = @options.fetch(:frame_index, 0)
  @blk = blk || @options[:animation]
end

Instance Attribute Details

#blkObject (readonly)

Returns the value of attribute blk.



7
8
9
# File 'lib/nyan/sprite.rb', line 7

def blk
  @blk
end

#frame_indexObject

Returns the value of attribute frame_index.



6
7
8
# File 'lib/nyan/sprite.rb', line 6

def frame_index
  @frame_index
end

#framesObject

Returns the value of attribute frames.



6
7
8
# File 'lib/nyan/sprite.rb', line 6

def frames
  @frames
end

#xObject

Returns the value of attribute x.



6
7
8
# File 'lib/nyan/sprite.rb', line 6

def x
  @x
end

#yObject

Returns the value of attribute y.



6
7
8
# File 'lib/nyan/sprite.rb', line 6

def y
  @y
end

#zObject

Returns the value of attribute z.



6
7
8
# File 'lib/nyan/sprite.rb', line 6

def z
  @z
end

Instance Method Details

#<=>(other) ⇒ Object



56
57
58
# File 'lib/nyan/sprite.rb', line 56

def <=>(other)
  z <=> other.z
end

#animate!Object



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

def animate!
  self.frame_index = (self.frame_index + 1) % frames.length
  self.instance_eval(&blk) if blk
end

#bottomObject



44
45
46
# File 'lib/nyan/sprite.rb', line 44

def bottom
  y + height
end

#frameObject



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

def frame
  frames[frame_index]
end

#heightObject



32
33
34
# File 'lib/nyan/sprite.rb', line 32

def height
  @height ||= frame.lines.count
end

#leftObject



48
49
50
# File 'lib/nyan/sprite.rb', line 48

def left
  x
end

#linesObject



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

def lines
  frame.lines    
end

#rightObject



52
53
54
# File 'lib/nyan/sprite.rb', line 52

def right
  x + width
end

#topObject



40
41
42
# File 'lib/nyan/sprite.rb', line 40

def top
  y
end

#widthObject



36
37
38
# File 'lib/nyan/sprite.rb', line 36

def width
  @width ||= frame.lines.map { |l| l.chomp.unansi.length }.max
end