Class: PirateGame::Background

Inherits:
Object
  • Object
show all
Defined in:
lib/pirate_game/background.rb

Constant Summary collapse

STATES =
[:clear, :foggy, :windy]

Instance Method Summary collapse

Constructor Details

#initialize(shoes, state = nil) ⇒ Background

Returns a new instance of Background.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pirate_game/background.rb', line 5

def initialize shoes, state=nil
  @shoes = shoes
  set_state state

  @items = []

  @items << PirateGame::Wave.new(@shoes, 0)
  @items << PirateGame::Wave.new(@shoes, 20)

  image = File.expand_path '../../../imgs/pirate_ship_sm.png', __FILE__

  @items << PirateGame::Image.new(@shoes, image, 66, 55)

  [40, 60, 80, 100].each do |top|
    @items << PirateGame::Wave.new(@shoes, top)
  end
end

Instance Method Details

#add_extra_item(item) ⇒ Object



82
83
84
# File 'lib/pirate_game/background.rb', line 82

def add_extra_item item
  @extra_items << item
end

#all_itemsObject



46
47
48
# File 'lib/pirate_game/background.rb', line 46

def all_items
  @items + @extra_items
end

#animate(frame) ⇒ Object



86
87
88
89
90
# File 'lib/pirate_game/background.rb', line 86

def animate frame
  all_items.each do |item|
    item.animate frame
  end
end

#colorObject



50
51
52
53
54
55
56
57
# File 'lib/pirate_game/background.rb', line 50

def color
  case @state
  when :foggy
    @shoes.rgb(105, 138, 150, 180)
  else # :clear, :windy
    PirateGame::Boot::SKY_COLOR
  end
end

#drawObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pirate_game/background.rb', line 59

def draw
  randomize_state

  @shoes.background color unless foreground?

  @items.each do |item|
    item.draw
  end

  @extra_items = []

  yield if block_given?

  @extra_items.each do |item|
    item.draw
  end

  # doesn't draw over input items (buttons, text boxes, etc) >:(
  @shoes.background color if foreground?

  send_speed_to_items
end

#foreground?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/pirate_game/background.rb', line 92

def foreground?
  @state == :foggy
end

#randomize_stateObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/pirate_game/background.rb', line 28

def randomize_state
  case rand
  when 0.0..0.1
    @state = :foggy
  when 0.2..0.3
    @state = :windy
  else
    @state = :clear
  end
end

#send_speed_to_itemsObject



39
40
41
42
43
44
# File 'lib/pirate_game/background.rb', line 39

def send_speed_to_items
  case @state
  when :windy
    all_items.each {|item| item.speed = :fast}
  end
end

#set_state(state) ⇒ Object



23
24
25
26
# File 'lib/pirate_game/background.rb', line 23

def set_state state
  @state = state if STATES.include?(state)
  @state ||= :clear
end