Class: Gemwarrior::Location

Inherits:
Entity
  • Object
show all
Defined in:
lib/gemwarrior/entities/location.rb

Constant Summary collapse

DANGER_LEVEL =

CONSTANTS HASHES

{:none => 0, :low => 15, :moderate => 30, :high => 55, :assured => 100}
ERROR_LOCATION_ITEM_REMOVE_INVALID =

ERRORS

'That item cannot be removed as it does not exist here.'

Instance Attribute Summary collapse

Attributes inherited from Entity

#description, #name

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Location

Returns a new instance of Location.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gemwarrior/entities/location.rb', line 18

def initialize(options)
  self.name                 = options.fetch(:name)
  self.description          = options.fetch(:description)
  self.coords               = options.fetch(:coords)
  self.locs_connected       = options.fetch(:locs_connected)
  self.danger_level         = options.fetch(:danger_level)
  self.monster_level_range  = options.fetch(:monster_level_range)
  self.items                = options.fetch(:items)
  self.monsters_abounding   = []
  self.bosses_abounding     = options[:bosses_abounding]
  self.checked_for_monsters = false
end

Instance Attribute Details

#bosses_aboundingObject

Returns the value of attribute bosses_abounding.



15
16
17
# File 'lib/gemwarrior/entities/location.rb', line 15

def bosses_abounding
  @bosses_abounding
end

#checked_for_monstersObject

Returns the value of attribute checked_for_monsters.



15
16
17
# File 'lib/gemwarrior/entities/location.rb', line 15

def checked_for_monsters
  @checked_for_monsters
end

#coordsObject

Returns the value of attribute coords.



15
16
17
# File 'lib/gemwarrior/entities/location.rb', line 15

def coords
  @coords
end

#danger_levelObject

Returns the value of attribute danger_level.



15
16
17
# File 'lib/gemwarrior/entities/location.rb', line 15

def danger_level
  @danger_level
end

#itemsObject

Returns the value of attribute items.



15
16
17
# File 'lib/gemwarrior/entities/location.rb', line 15

def items
  @items
end

#locs_connectedObject

Returns the value of attribute locs_connected.



15
16
17
# File 'lib/gemwarrior/entities/location.rb', line 15

def locs_connected
  @locs_connected
end

#monster_level_rangeObject

Returns the value of attribute monster_level_range.



15
16
17
# File 'lib/gemwarrior/entities/location.rb', line 15

def monster_level_range
  @monster_level_range
end

#monsters_aboundingObject

Returns the value of attribute monsters_abounding.



15
16
17
# File 'lib/gemwarrior/entities/location.rb', line 15

def monsters_abounding
  @monsters_abounding
end

Instance Method Details

#add_item(item_name) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/gemwarrior/entities/location.rb', line 51

def add_item(item_name)
  Dir.glob('lib/gemwarrior/items/*.rb').each do |item|
    require_relative item[item.index('/', item.index('/')+1)+1..item.length]
  end

  self.items.push(eval(item_name).new)
end

#checked_for_monsters?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/gemwarrior/entities/location.rb', line 96

def checked_for_monsters?
  checked_for_monsters
end

#has_boss?(boss_name) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/gemwarrior/entities/location.rb', line 47

def has_boss?(boss_name)
  bosses_abounding.map{|b| b.name.downcase}.include?(boss_name)
end

#has_item?(item_name) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/gemwarrior/entities/location.rb', line 39

def has_item?(item_name)
  items.map{|i| i.name.downcase}.include?(item_name)
end

#has_loc_to_the?(direction) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gemwarrior/entities/location.rb', line 72

def has_loc_to_the?(direction)
  case direction
  when 'n'
    direction = 'north'
  when 'e'
    direction = 'east'
  when 's'
    direction = 'south'
  when 'w'
    direction = 'west'
  end
  locs_connected[direction.to_sym]
end

#has_monster?(monster_name) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/gemwarrior/entities/location.rb', line 43

def has_monster?(monster_name)
  monsters_abounding.map{|m| m.name.downcase}.include?(monster_name)
end

#list_actionable_wordsObject



148
149
150
151
152
153
154
# File 'lib/gemwarrior/entities/location.rb', line 148

def list_actionable_words
  actionable_words =  []
  actionable_words.push(monsters_abounding.map(&:name)) unless monsters_abounding.empty?
  actionable_words.push(bosses_abounding.map(&:name))   unless bosses_abounding.empty?
  actionable_words.push(items.map(&:name))              unless items.empty?
  actionable_words.join(', ')
end

#list_bossesObject



130
131
132
133
134
135
136
# File 'lib/gemwarrior/entities/location.rb', line 130

def list_bosses
  if bosses_abounding.length > 0
    bosses_abounding.map(&:name)
  else
    []
  end
end

#list_itemsObject



114
115
116
117
118
119
120
# File 'lib/gemwarrior/entities/location.rb', line 114

def list_items
  if items.length > 0
    items.map(&:name)
  else
    []
  end
end

#list_monstersObject



122
123
124
125
126
127
128
# File 'lib/gemwarrior/entities/location.rb', line 122

def list_monsters
  if monsters_abounding.length > 0
    monsters_abounding.map(&:name)
  else
    []
  end
end

#list_pathsObject



138
139
140
141
142
143
144
145
146
# File 'lib/gemwarrior/entities/location.rb', line 138

def list_paths
  valid_paths = []
  locs_connected.each do |key, value|
    if value
      valid_paths.push(key.to_s)
    end
  end
  return valid_paths
end

#monster_by_name(monster_name) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/gemwarrior/entities/location.rb', line 86

def monster_by_name(monster_name)
  monsters_list = monsters_abounding | bosses_abounding

  monsters_list.each do |m|
    if m.name.downcase.eql?(monster_name.downcase)
      return m.clone
    end
  end
end

#populate_monsters(monsters_available, spawn = false) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/gemwarrior/entities/location.rb', line 156

def populate_monsters(monsters_available, spawn = false)
  if should_spawn_monster? || spawn
    self.checked_for_monsters = true unless spawn
    self.monsters_abounding = [] unless spawn
    random_monster = nil

    # get random non-boss monster

    loop do
      random_monster = monsters_available[rand(0..monsters_available.length-1)]

      if spawn
        break
      else
        unless random_monster.is_boss || !self.monster_level_range.include?(random_monster.level)
          break
        end
      end
    end

    monsters_abounding.push(random_monster)
  else
    return nil
  end
end

#remove_item(item_name) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/gemwarrior/entities/location.rb', line 59

def remove_item(item_name)
  if has_item?(item_name)
    items.reject! { |item| item.name == item_name }
  else
    ERROR_LOCATION_ITEM_REMOVE_INVALID
  end
end

#remove_monster(name) ⇒ Object



67
68
69
70
# File 'lib/gemwarrior/entities/location.rb', line 67

def remove_monster(name)
  monsters_abounding.reject! { |monster| monster.name == name }
  bosses_abounding.reject! { |monster| monster.name == name }
end

#should_spawn_monster?Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/gemwarrior/entities/location.rb', line 100

def should_spawn_monster?
  found = false
  unless danger_level.eql?(:none)
    max = DANGER_LEVEL[danger_level]
    trigger_values = 0..max
    actual_value = rand(1..100)

    if trigger_values.include?(actual_value)
      found = true
    end
  end
  return found
end

#status(debug_mode = false) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/gemwarrior/entities/location.rb', line 31

def status(debug_mode = false)
  status_text =  name.ljust(30).upcase.colorize(:green)
  status_text << coords.values.to_a.to_s.colorize(:white)
  status_text << " DL[#{danger_level.to_s.ljust(8)}] ".colorize(:white) if debug_mode
  status_text << " MLR[#{monster_level_range.to_s.ljust(6)}] ".colorize(:white) if debug_mode
  status_text << "\n#{description}\n".colorize(:white)
end