Class: Engine
- Inherits:
-
Object
- Object
- Engine
- Defined in:
- lib/Engine.rb
Instance Method Summary collapse
- #action_map ⇒ Object
- #default_action ⇒ Object
- #display ⇒ Object
- #export_to_txt ⇒ Object
- #get_answer ⇒ Object
-
#initialize(prompt, display) ⇒ Engine
constructor
A new instance of Engine.
- #initialize_key_prompt ⇒ Object
- #is_running ⇒ Object
- #is_stopped ⇒ Object
- #key_map ⇒ Object
- #name_world ⇒ Object
- #proccess_answer(answer) ⇒ Object
- #prompt ⇒ Object
- #run ⇒ Object
- #run_v1 ⇒ Object
- #show_key_prompts ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #world ⇒ Object
Constructor Details
#initialize(prompt, display) ⇒ Engine
Returns a new instance of Engine.
6 7 8 9 10 |
# File 'lib/Engine.rb', line 6 def initialize(prompt, display) @running = false @prompt = prompt @display = display end |
Instance Method Details
#action_map ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/Engine.rb', line 37 def action_map { 'REGENERATE' => Action.new(world.method(:regenerate)), 'ZOOM_IN' => Action.new(world.method(:zoom_in)), 'ZOOM_OUT' => Action.new(world.method(:zoom_out)), 'SCROLL_UP' => Action.new(method(:default_action)), 'SCROLL_RIGHT' => Action.new(method(:default_action)), 'SCROLL_DOWN' => Action.new(method(:default_action)), 'SCROLL_LEFT' => Action.new(method(:default_action)), 'QUIT' => Action.new(method(:stop)), } end |
#default_action ⇒ Object
106 107 108 |
# File 'lib/Engine.rb', line 106 def default_action return true end |
#display ⇒ Object
114 115 116 |
# File 'lib/Engine.rb', line 114 def display @display end |
#export_to_txt ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/Engine.rb', line 96 def export_to_txt content = world.render_map_as_text(display.tile_size) directory = "exported_worlds" extension = 'txt' file_name = "#{DateTime.now.strftime('%Y_%m_%d__%H%M%S')}__#{world.name}.txt".gsub!(" ","_") TTY::File.create_file("#{directory}/#{file_name}", content) end |
#get_answer ⇒ Object
28 29 30 |
# File 'lib/Engine.rb', line 28 def get_answer prompt.select('', action_map.keys, cycle: true) end |
#initialize_key_prompt ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/Engine.rb', line 65 def initialize_key_prompt prompt.on(:keypress) { |event| event_key = event.value event_name = event.key.name key_map[event_key].call if key_map.keys.include?(event_key) key_map[event_name].call if key_map.keys.include?(event_name) } end |
#is_running ⇒ Object
135 136 137 |
# File 'lib/Engine.rb', line 135 def is_running @running end |
#is_stopped ⇒ Object
139 140 141 |
# File 'lib/Engine.rb', line 139 def is_stopped !is_running end |
#key_map ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/Engine.rb', line 50 def key_map { 'q' => Action.new(method(:stop)), 'r' => Action.new(world.method(:regenerate)), 'z' => Action.new(world.method(:zoom_in)), 'x' => Action.new(world.method(:zoom_out)), 'n' => Action.new(method(:name_world)), 'e' => Action.new(method(:export_to_txt)), :up => Action.new(world.method(:scroll_up)), # scroll up :down => Action.new(world.method(:scroll_down)), # scroll down :right => Action.new(world.method(:scroll_right)), # scroll right :left => Action.new(world.method(:scroll_left)), # scroll left } end |
#name_world ⇒ Object
91 92 93 94 |
# File 'lib/Engine.rb', line 91 def name_world new_name = prompt.ask('what do people call this world?') world.set_name(new_name) end |
#proccess_answer(answer) ⇒ Object
32 33 34 35 |
# File 'lib/Engine.rb', line 32 def proccess_answer(answer) action = action_map[answer] action.call end |
#prompt ⇒ Object
110 111 112 |
# File 'lib/Engine.rb', line 110 def prompt @prompt end |
#run ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/Engine.rb', line 12 def run initialize_key_prompt while is_running display.draw show_key_prompts end end |
#run_v1 ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/Engine.rb', line 20 def run_v1 while is_running display.draw answer = get_answer proccess_answer(answer) end end |
#show_key_prompts ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/Engine.rb', line 74 def show_key_prompts prompt.keypress( [ "", "-----------------------", "r to generate new world", "arrow keys to explore", "z to zoom in", "x to zoom out", "n to rename this world", "e to save this world in text", "q to quit", "" ].join("\n") ) end |
#start ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'lib/Engine.rb', line 122 def start if is_running throw "engine already running" end @running = true run end |
#stop ⇒ Object
131 132 133 |
# File 'lib/Engine.rb', line 131 def stop @running = false end |
#world ⇒ Object
118 119 120 |
# File 'lib/Engine.rb', line 118 def world @display.world end |