Class: Engine

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

Instance Method Summary collapse

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_mapObject



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_actionObject



106
107
108
# File 'lib/Engine.rb', line 106

def default_action
    return true
end

#displayObject



114
115
116
# File 'lib/Engine.rb', line 114

def display
    @display
end

#export_to_txtObject



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_answerObject



28
29
30
# File 'lib/Engine.rb', line 28

def get_answer
    prompt.select('', action_map.keys, cycle: true)
end

#initialize_key_promptObject



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_runningObject



135
136
137
# File 'lib/Engine.rb', line 135

def is_running
    @running
end

#is_stoppedObject



139
140
141
# File 'lib/Engine.rb', line 139

def is_stopped
    !is_running
end

#key_mapObject



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_worldObject



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

#promptObject



110
111
112
# File 'lib/Engine.rb', line 110

def prompt
    @prompt
end

#runObject



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_v1Object



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_promptsObject



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

#startObject



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

#stopObject



131
132
133
# File 'lib/Engine.rb', line 131

def stop
    @running = false
end

#worldObject



118
119
120
# File 'lib/Engine.rb', line 118

def world
    @display.world
end