/
location.class.rb
120 lines (72 loc) · 2.57 KB
/
location.class.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#
# The Location class represents a location in a game.
#
class Location
attr_accessor :id, :path, :loaded, :name, :description, :dark, :exits,
:description_notes, :image_file, :command_conditions
include Handles_YAML_Files
# is this function needed?
def initialize(path)
@path = path
# additional description details revealed by player actions get stored here
@description_notes = {}
end
def set_exit(exit, destination)
@exits[exit] = {'destination' => destination}
end
def has_exit(direction)
@exits[direction] ? true : false
end
def add_to_description(text)
@description_notes[@name] ||= ''
@description_notes[@name] += text
end
def describe(doors, props, characters, light = nil)
if @dark and light != true
description = "It is too dark to see.\n"
else
# describe current location, including any additions added during game play
description = @description.dup
description += @description_notes[@name] if @description_notes[@name]
# specify visibility conditions for different types of game components
components_to_describe = {
characters => "components[component_id].location == @id",
doors => "components[component_id].locations.include?(@id) and components[component_id].traits['visible'] == true",
props => "components[component_id].location == @id and components[component_id].traits['visible'] == true"
}
# figure out which game components are visible in this location
visible_components = []
components_to_describe.each do |components, visibility_condition|
visible_components |= components_seen(components, visibility_condition)
end
# add a listing of the visible components to the location description
description += describe_game_components(visible_components)
end
description
end
def components_seen(components, visibility_condition)
components_seen = []
components.each do |component_id, component_data|
if eval(visibility_condition)
components_seen << components[component_id].noun_base
end
end
components_seen
end
def describe_game_components(components)
output = ''
if components.size > 0
output << "You see: "
component_output = ''
components.each do |component|
if component_output != ''
component_output << ', '
end
component_output << component
end
component_output << ".\n"
output << component_output
end
output
end
end