Module: RSokoban::GameUI
Overview
I extends/implements the API of module Game, to run the game in a console window. I don’t define the user interface, you can use me to run game with curses, n-curses, straight text-mode, etc.
Instance Attribute Summary
Attributes included from Game
Instance Method Summary collapse
- #initialize(ui, setname = 'microban.xsb') ⇒ Object
- #load_a_new_set(setname) ⇒ Object
-
#run ⇒ Object
I am the game loop for UIs.
- #start_level ⇒ PlayerAction
Methods included from Game
#level_height, #level_title, #level_width, #load_level, #man_x, #man_y, #map_as_array, #move, #move_number, #next_level, #record, #redo, #restart_set, #set_size, #set_title, #undo, #update_record
Instance Method Details
#initialize(ui, setname = 'microban.xsb') ⇒ Object
10 11 12 |
# File 'lib/rsokoban/game/game_ui.rb', line 10 def initialize ui, setname = 'microban.xsb' super(ui, setname) end |
#load_a_new_set(setname) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/rsokoban/game/game_ui.rb', line 62 def load_a_new_set setname begin super(setname) @ui.get_action(get_hash_after_loading_set) rescue NoFileError error = "Error, no such file : #{setname}" @ui.get_action(get_hash_after_loading_set.merge({:error=>error})) end end |
#run ⇒ Object
I am the game loop for UIs.
15 16 17 18 19 20 21 22 23 24 25 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 |
# File 'lib/rsokoban/game/game_ui.rb', line 15 def run player_action = start_level loop do if player_action.level_number? player_action = load_level player_action.action next elsif player_action.set_name? player_action = load_a_new_set player_action.action next elsif player_action.quit? break elsif player_action.next? player_action = next_level next elsif player_action.retry? player_action = start_level next elsif player_action.move? result = @level.move(player_action.action) elsif player_action.undo? result = @level.undo end hash = {:map=>@level.map_as_array, :move=>result[:move_number]} case result[:status] when :win update_record player_action = @ui.get_action(hash.merge({:type=>:win})) when :ok then player_action = @ui.get_action(hash.merge({:type=>:display})) else player_action = @ui.get_action(:type=>:display, :map=>@level.map_as_array, :error=>result[:message]) end end true end |
#start_level ⇒ PlayerAction
53 54 55 56 57 58 59 60 |
# File 'lib/rsokoban/game/game_ui.rb', line 53 def start_level begin @level = @level_loader.level(@level_number) @ui.get_action(get_hash_after_loading_set) rescue LevelNumberTooHighError @ui.get_action(:type=>:end_of_set, :map=>Map.new) end end |