Module: Action

Defined in:
lib/actions.rb

Class Method Summary collapse

Class Method Details

.bagObject

Method for displaying the contents of the player inventory



73
74
75
76
# File 'lib/actions.rb', line 73

def bag
    bag_contents
    prompt.keypress("Press space or enter to continue".green, keys: [:space, :return])
end

.bag_contentsObject



78
79
80
81
82
83
84
85
86
# File 'lib/actions.rb', line 78

def bag_contents
    if Game::PLAYER_ATTR[:inventory].length > 0
        rows = Game::PLAYER_ATTR[:inventory].map { |v| [v[:name].light_green,v[:examine_text]] }
        bag_table = TTY::Table.new(header: ["Item", "Description"], rows: rows)
        puts bag_table.render(:unicode, multiline: true, padding: [1,0])
    else
        puts "\nYour bag is empty. You shake the bag in your mouth sadly."
    end
end

.exit_gameObject

Method for exiting to main menu



89
90
91
92
93
94
# File 'lib/actions.rb', line 89

def exit_game
    prompt.select("Are you sure you want to quit and return to the main menu? (progress will be lost)".red) do |menu|
        menu.choice "Yes", -> {Game.new.main_menu}
        menu.choice "No", -> {Game.new.game_menu}
    end
end

.interact(interact_object) ⇒ Object



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
51
# File 'lib/actions.rb', line 16

def interact(interact_object)
    prompt.keypress("Press space or enter to continue".green, keys: [:space, :return])

    # If the interactible has not been used, puts the use success message.

    if !interact_object[:used?] 

        # If the object needs something to use, check inventory for anything with a matching item_id.

        if !interact_object[:needs].nil?
            # If there's a match, delete the item from the inventory and continue, else fail.

            if Game::PLAYER_ATTR[:inventory].any? {|h| h[:item_id] == interact_object[:needs]}
                Game::PLAYER_ATTR[:inventory].delete_at(Game::PLAYER_ATTR[:inventory].index { |v| v[:name] == Game::ITEM_LIST[interact_object[:needs]][:name] })
            else
                return puts interact_object[:use_text_fail]
            end
        end
        
        # Indicate that the object is now used and puts the successful use message

        interact_object[:used?] = true
        puts interact_object[:use_text_success]

        # Unless the interactible gives no item, add the item to the player inventory.

        unless interact_object[:contains].nil?
            Game::PLAYER_ATTR[:inventory] << Game::ITEM_LIST[interact_object[:contains]]
            puts "\n#{Game::PLAYER_ATTR[:inventory].last[:name].upcase.light_green} was added to inventory."
        end

        # If the interactible has an affect on the room, call used effect proc

        unless interact_object[:use_effect].nil?
            interact_object[:use_effect].call
        end
    
    # If the interactible has been used, put the used message

    elsif interact_object[:used?] 
        puts interact_object[:used_text] 
    end
end

.moveObject

Method for moving between locations



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/actions.rb', line 4

def move
    prompt.select("Where will you move to?".blue) do |menu|
        Game::ROOM_ATTR[Game::PLAYER_ATTR[:player_location_id]][:move_locations].each do |move_to|
            # If the location is open, list it as an option.

            if move_to[:open?] 
                menu.choice "#{move_to[:name].capitalize}", -> {Game::PLAYER_ATTR[:player_location_id] = move_to[:id]}
            end
        end
        menu.choice "Cancel", -> {next}
    end
end

.useObject

Method for using interactibles in a room if they exist



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/actions.rb', line 54

def use
    inter = Game::ROOM_ATTR[Game::PLAYER_ATTR[:player_location_id]][:interactibles]

    # Unless there's nothing to use, list print out thing to interact with.

    unless inter.nil?
        prompt.select("What will you use?") do |menu|
            inter.each do |interact_object|
                if interact_object[:accessible?]
                    menu.choice "#{interact_object[:name]}", -> {puts "#{interact_object[:use_text_look]}"; interact(interact_object)}
                end
            end
        end
    else
        puts "There is nothing around you that you can use."
    end
    prompt.keypress("Press space or enter to continue".green, keys: [:space, :return])
end