class Basher::Game
Attributes
base_view[R]
characters[R]
debug[R]
difficulty[R]
handler[R]
level[R]
misses[R]
state[R]
timer[R]
words[R]
Public Class Methods
new(base_view, state: :menu, debug: false, bindings: {})
click to toggle source
# File lib/basher/game.rb, line 64 def initialize(base_view, state: :menu, debug: false, bindings: {}) @debug = debug @base_view = base_view base_view.refresh @state = State.new(state) setup_default_bindings @handler = Handler.new(bindings) transition_to @state.current @timer = Timer.new end
Public Instance Methods
accuracy()
click to toggle source
# File lib/basher/game.rb, line 125 def accuracy return 0 if total_presses.zero? value = (total_presses - misses).to_f / total_presses * 100 value.round(2) end
chars_per_minute()
click to toggle source
# File lib/basher/game.rb, line 136 def chars_per_minute total_presses * 60 / timer.total_elapsed_in_seconds end
Also aliased as: cpm
clear()
click to toggle source
# File lib/basher/game.rb, line 150 def clear base_view.clear current_views.each(&:clear) end
execute_logic(char)
click to toggle source
# File lib/basher/game.rb, line 89 def execute_logic(char) if char == word.char next_letter! else @misses += 1 level.timer.advance(200) end end
handle(input)
click to toggle source
# File lib/basher/game.rb, line 79 def handle(input) debug_view.last_input = input if debugging? if state.in_game? && handler.letter?(input) execute_logic input end handler.invoke(input) end
next_letter!()
click to toggle source
# File lib/basher/game.rb, line 102 def next_letter! @characters += 1 word.advance! next_word! if word.finished? end
next_level!()
click to toggle source
# File lib/basher/game.rb, line 116 def next_level! @difficulty += 1 level.finish if level @level = Level.start(difficulty) do stop_game end end
next_word!()
click to toggle source
# File lib/basher/game.rb, line 109 def next_word! @words += 1 level.advance! next_level! if level.finished? end
refresh()
click to toggle source
# File lib/basher/game.rb, line 155 def refresh base_view.refresh current_views.each(&:refresh) end
render()
click to toggle source
# File lib/basher/game.rb, line 145 def render base_view.render current_views.each(&:render) end
resize_and_reposition()
click to toggle source
# File lib/basher/game.rb, line 160 def resize_and_reposition clear views.each(&:will_resize!) current_views.each(&:resize_and_reposition) render end
total_presses()
click to toggle source
# File lib/basher/game.rb, line 141 def total_presses characters + misses end
transition_to(new_state)
click to toggle source
# File lib/basher/game.rb, line 167 def transition_to(new_state) before_transition state.transition_to(new_state) after_transition end
word()
click to toggle source
# File lib/basher/game.rb, line 98 def word level.word end
words_per_minute()
click to toggle source
# File lib/basher/game.rb, line 131 def words_per_minute words * 60 / timer.total_elapsed_in_seconds end
Also aliased as: wpm
Private Instance Methods
after_transition()
click to toggle source
# File lib/basher/game.rb, line 188 def after_transition views.each do |view| view.resize_and_reposition if view.should_redraw end render end
back_to_game()
click to toggle source
# File lib/basher/game.rb, line 207 def back_to_game timer.start level.timer.start transition_to(:in_game) end
before_transition()
click to toggle source
# File lib/basher/game.rb, line 183 def before_transition clear refresh end
debugging?()
click to toggle source
# File lib/basher/game.rb, line 175 def debugging? @debug end
pause_game()
click to toggle source
# File lib/basher/game.rb, line 196 def pause_game timer.stop level.timer.stop transition_to(:paused) end
playing?()
click to toggle source
# File lib/basher/game.rb, line 179 def playing? state.playing? && input.letter? end
setup_default_bindings()
click to toggle source
# File lib/basher/game.rb, line 25 def setup_default_bindings Handler.bind :resize do resize_and_reposition end Handler.bind 'q' do case state.current when :paused then back_to_menu when :menu then :quit when :score then back_to_menu end end Handler.bind :escape do case state.current when :in_game pause_game when :paused back_to_game when :score back_to_menu end end Handler.bind :enter do case state.current when :score back_to_menu end end Handler.bind 's' do case state.current when :menu start_game end end end
setup_game()
click to toggle source
# File lib/basher/game.rb, line 226 def setup_game @difficulty = 0 @characters = 0 @misses = 0 @words = 0 next_level! end
start_game()
click to toggle source
# File lib/basher/game.rb, line 213 def start_game transition_to(:loading) Basher::Dictionary.preload setup_game transition_to(:in_game) timer.start end
stop_game()
click to toggle source
# File lib/basher/game.rb, line 221 def stop_game timer.stop transition_to(:score) end