Class: PlayLevel

Inherits:
Object
  • Object
show all
Defined in:
lib/escape_to_rubyconf/play_level.rb

Overview

inherit from Level? why?!?

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window) ⇒ PlayLevel

Returns a new instance of PlayLevel.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/escape_to_rubyconf/play_level.rb', line 8

def initialize window
  @window = window

  @begin = Gosu::Sample.new @window, File.dirname(__FILE__) + '/assets/begin.wav'

  # Create level elements
  @background = Gosu::Image.new @window, File.dirname(__FILE__) + '/assets/rubyconf-background.png'
  @ninja = Ninja.new self
  @snakes = []
  3.times { add_snake! }

  # Set ninja start and win positions
  offset = @ninja.boundary/2
  @start_position = [offset, offset]
  @win_position   = [@window.width - offset, @window.height - offset]

  # Add callback holders
  # @start_callbacks = []
  @win_callbacks  = []
  @fail_callbacks = []
  @quit_callbacks = []
end

Instance Attribute Details

#soundsObject

Returns the value of attribute sounds.



7
8
9
# File 'lib/escape_to_rubyconf/play_level.rb', line 7

def sounds
  @sounds
end

#stateObject

Returns the value of attribute state.



7
8
9
# File 'lib/escape_to_rubyconf/play_level.rb', line 7

def state
  @state
end

#windowObject

Returns the value of attribute window.



7
8
9
# File 'lib/escape_to_rubyconf/play_level.rb', line 7

def window
  @window
end

Instance Method Details

#add_snake!Object



80
81
82
# File 'lib/escape_to_rubyconf/play_level.rb', line 80

def add_snake!
  @snakes << Snake.new(self)
end

#drawObject



120
121
122
123
124
# File 'lib/escape_to_rubyconf/play_level.rb', line 120

def draw
  @background.draw 0, 0, 0
  @ninja.draw
  @snakes.each { |snake| snake.draw }
end

#fail!Object



89
90
91
92
# File 'lib/escape_to_rubyconf/play_level.rb', line 89

def fail!
  stop
  @fail_callbacks.each { |c| c.call }
end

#ninja_escaped?Boolean

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/escape_to_rubyconf/play_level.rb', line 71

def ninja_escaped?
  ( (Math::round(@ninja.x) == @win_position.first) &&
    (Math::round(@ninja.y) == @win_position.last) )
end

#on_fail(&block) ⇒ Object



39
40
41
# File 'lib/escape_to_rubyconf/play_level.rb', line 39

def on_fail &block
  @fail_callbacks << block
end

#on_quit(&block) ⇒ Object



43
44
45
# File 'lib/escape_to_rubyconf/play_level.rb', line 43

def on_quit &block
  @quit_callbacks << block
end

#on_win(&block) ⇒ Object

def on_start &block

@start_callbacks << block

end



35
36
37
# File 'lib/escape_to_rubyconf/play_level.rb', line 35

def on_win &block
  @win_callbacks << block
end

#play_begin_soundObject



58
59
60
# File 'lib/escape_to_rubyconf/play_level.rb', line 58

def play_begin_sound
  @begin.play
end

#quit!Object



94
95
96
97
# File 'lib/escape_to_rubyconf/play_level.rb', line 94

def quit!
  stop
  @quit_callbacks.each { |c| c.call }
end

#rand_positionObject



62
63
64
65
66
67
68
69
# File 'lib/escape_to_rubyconf/play_level.rb', line 62

def rand_position
  x, y = 0, 0
  begin
    x, y = rand(@window.width), rand(@window.height)
  end while ((x < 120 && y < 120) ||
             (x > @window.width-120 && y > @window.height-120))
  [x, y]
end

#snakes_countObject



76
77
78
# File 'lib/escape_to_rubyconf/play_level.rb', line 76

def snakes_count
  @snakes.size
end

#start!Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/escape_to_rubyconf/play_level.rb', line 47

def start!
  # @start_callbacks.each { |c| c.call }
  @ninja.x = @start_position[0]
  @ninja.y = @start_position[1]
  @snakes.each do |snake|
    snake.start *rand_position
    snake.end   *rand_position
  end
  play_begin_sound
end

#stopObject



99
100
101
# File 'lib/escape_to_rubyconf/play_level.rb', line 99

def stop
  @ninja.stop_sneak
end

#updateObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/escape_to_rubyconf/play_level.rb', line 103

def update
  quit! if @window.button_down? Gosu::KbEscape
  @ninja.update
  @snakes.each { |snake| snake.update }
  # Did the player make it?
  if ninja_escaped?
    win!
    return
  end
  # Check for collision and attacks
  @snakes.each do |snake|
    range = Gosu.distance @ninja.x, @ninja.y, snake.x, snake.y
    fail!         if range < snake.range
    snake.attack! if range < snake.range*2.5
  end
end

#win!Object



84
85
86
87
# File 'lib/escape_to_rubyconf/play_level.rb', line 84

def win!
  stop
  @win_callbacks.each { |c| c.call }
end