Class: Dare::Sprite

Inherits:
Object
  • Object
show all
Defined in:
lib/dare/sprite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window = Dare.default_canvas) ⇒ Sprite

Returns a new instance of Sprite.



4
5
6
7
8
9
10
11
12
# File 'lib/dare/sprite.rb', line 4

def initialize(window = Dare.default_canvas)
  @window = window
  @images = []
  @state = Hash.new {|h,k| h[k] = Dare::AnimationState.new}
  @current_image = 0
  @ticks_on_current_image = 0
  @x = 0
  @y = 0
end

Instance Attribute Details

#imagesObject

Returns the value of attribute images.



3
4
5
# File 'lib/dare/sprite.rb', line 3

def images
  @images
end

#stateObject

Returns the value of attribute state.



3
4
5
# File 'lib/dare/sprite.rb', line 3

def state
  @state
end

Instance Method Details

#draw(x = 0, y = 0) ⇒ Object



14
15
16
# File 'lib/dare/sprite.rb', line 14

def draw(x = 0, y = 0)
  @images[@current_image].draw(x, y)
end

#standObject



54
55
56
57
# File 'lib/dare/sprite.rb', line 54

def stand
  @current_image = 0
  @ticks_on_current_image = 0
end

#updateObject



18
19
20
21
22
23
24
# File 'lib/dare/sprite.rb', line 18

def update
  if @window.button_down? Dare::KbRight
    walk
  else
    stand
  end
end

#walkObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dare/sprite.rb', line 26

def walk
  if @current_image == 0
    @current_image = 2
    @ticks_on_current_image = 0
  elsif @current_image == 2
    if @ticks_on_current_image >= 5
      @current_image = 3
      @ticks_on_current_image = 0
    else
      @ticks_on_current_image += 1
    end
  elsif @current_image == 3
    if @ticks_on_current_image >= 5
      @current_image = 4
      @ticks_on_current_image = 0
    else
      @ticks_on_current_image += 1
    end
  elsif @current_image == 4
    if @ticks_on_current_image >= 5
      @current_image = 2
      @ticks_on_current_image = 0
    else
      @ticks_on_current_image += 1
    end
  end
end