Class: Gemwarrior::Location

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

Constant Summary collapse

DANGER_LEVEL =

CONSTANTS

{ none: 0, low: 15, moderate: 30, high: 55, assured: 100 }
ERROR_ITEM_ADD_INVALID =
'That item cannot be added to the location\'s inventory.'
ERROR_ITEM_REMOVE_INVALID =
'That item cannot be removed as it does not exist here.'
ERROR_ITEM_REF_INVALID =
'That item cannot be accessed.'
ERROR_SPAWN_NAME_INVALID =
'That monster does not exist in the game, and cannot be spawned.'

Instance Attribute Summary collapse

Attributes inherited from Entity

#consumable, #description, #display_shopping_cart, #equippable, #equipped, #name, #name_display, #number_of_uses, #takeable, #talkable, #useable, #useable_battle, #used, #used_again

Instance Method Summary collapse

Methods inherited from Entity

#puts, #use

Constructor Details

#initialize(options) ⇒ Location

Returns a new instance of Location.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gemwarrior/entities/location.rb', line 27

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

Instance Attribute Details

#bosses_aboundingObject

Returns the value of attribute bosses_abounding.



17
18
19
# File 'lib/gemwarrior/entities/location.rb', line 17

def bosses_abounding
  @bosses_abounding
end

#checked_for_monstersObject

Returns the value of attribute checked_for_monsters.



17
18
19
# File 'lib/gemwarrior/entities/location.rb', line 17

def checked_for_monsters
  @checked_for_monsters
end

#coordsObject

Returns the value of attribute coords.



17
18
19
# File 'lib/gemwarrior/entities/location.rb', line 17

def coords
  @coords
end

#danger_levelObject

Returns the value of attribute danger_level.



17
18
19
# File 'lib/gemwarrior/entities/location.rb', line 17

def danger_level
  @danger_level
end

#itemsObject

Returns the value of attribute items.



17
18
19
# File 'lib/gemwarrior/entities/location.rb', line 17

def items
  @items
end

#monster_level_rangeObject

Returns the value of attribute monster_level_range.



17
18
19
# File 'lib/gemwarrior/entities/location.rb', line 17

def monster_level_range
  @monster_level_range
end

#monsters_aboundingObject

Returns the value of attribute monsters_abounding.



17
18
19
# File 'lib/gemwarrior/entities/location.rb', line 17

def monsters_abounding
  @monsters_abounding
end

#pathsObject

Returns the value of attribute paths.



17
18
19
# File 'lib/gemwarrior/entities/location.rb', line 17

def paths
  @paths
end

#visitedObject

Returns the value of attribute visited.



17
18
19
# File 'lib/gemwarrior/entities/location.rb', line 17

def visited
  @visited
end

Instance Method Details

#add_item(item_name_to_add) ⇒ Object



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

def add_item(item_name_to_add)
  all_items = GameItems.data | GameWeapons.data | GameArmor.data
  all_items.each do |game_item|
    if game_item.name.eql?(item_name_to_add)
      self.items.push(game_item)
      return
    end
  end
  return ERROR_ITEM_ADD_INVALID
end

#checked_for_monsters?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/gemwarrior/entities/location.rb', line 135

def checked_for_monsters?
  checked_for_monsters
end

#contains_item?(item_name) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/gemwarrior/entities/location.rb', line 60

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

#describeObject



41
42
43
44
45
46
47
48
49
# File 'lib/gemwarrior/entities/location.rb', line 41

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

#describe_detailedObject



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

def describe_detailed
  desc_text =  "#{name_display.ljust(36).colorize(:yellow)} #{coords.values.to_a.to_s.colorize(:white)}\n"
  desc_text << "(#{name})\n".colorize(:green)
  desc_text << "#{description}\n".colorize(:white)
  desc_text << "DANGER_LEVEL       : #{danger_level}\n".colorize(:white)
  desc_text << "MONSTER_LEVEL_RANGE: #{monster_level_range}\n".colorize(:white)
  desc_text
end

#get_item_ref(item_name) ⇒ Object



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

def get_item_ref(item_name)
  all_items = GameItems.data | GameWeapons.data | GameArmor.data
  all_items.each do |game_item|
    if game_item.name.eql?(item_name)
      item_ref = self.items.find{ |i| i.name.downcase == item_name.downcase }
      return item_ref
    end
  end
  return ERROR_ITEM_REF_INVALID
end

#has_any_monsters?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/gemwarrior/entities/location.rb', line 64

def has_any_monsters?
  monsters_abounding.length > 0
end

#has_boss?(boss_name) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/gemwarrior/entities/location.rb', line 72

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

#has_loc_to_the?(direction) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/gemwarrior/entities/location.rb', line 111

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
  paths[direction.to_sym]
end

#has_monster?(monster_name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#list_actionable_wordsObject



207
208
209
210
211
212
213
# File 'lib/gemwarrior/entities/location.rb', line 207

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



193
194
195
# File 'lib/gemwarrior/entities/location.rb', line 193

def list_bosses
  bosses_abounding.length > 0 ? bosses_abounding.map(&:name_display) : []
end

#list_itemsObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/gemwarrior/entities/location.rb', line 153

def list_items
  if items.empty?
    []
  else
    # build hash out of location's items

    item_hash = {}
    self.items.map(&:name).each do |i|
      i_sym = i.to_sym
      if item_hash.keys.include? i_sym
        item_hash[i_sym] += 1
      else
        item_hash[i_sym] = 1
      end
    end

    # one item? return single element array

    if item_hash.length == 1
      i = item_hash.keys
      q = item_hash.values.join.to_i
      return q > 1 ? ["#{q} #{i}s"] : i
    # multiple items? build an array of strings

    else
      item_arr = []
      item_hash.each do |i, q|
        if q > 1
          item_arr.push("#{i.to_s.colorize(:yellow)}#{'s'.colorize(:yellow)} x#{q}")
        else
          item_arr.push(i)
        end
      end

      return item_arr
    end
  end
end

#list_monstersObject



189
190
191
# File 'lib/gemwarrior/entities/location.rb', line 189

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

#list_pathsObject



197
198
199
200
201
202
203
204
205
# File 'lib/gemwarrior/entities/location.rb', line 197

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

#monster_by_name(monster_name) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/gemwarrior/entities/location.rb', line 125

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 = nil, spawn = false, monster_type = nil) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/gemwarrior/entities/location.rb', line 215

def populate_monsters(monsters_available = nil, spawn = false, monster_type = nil)
  # debug spawn

  if spawn
    random_monster = nil

    # debug spawn random monster

    if monster_type.nil?
      loop do
        random_monster = monsters_available[rand(0..monsters_available.length-1)].clone

        monsters_abounding.push(random_monster)
        break
      end
    # debug spawn w/ monster_type

    else
      monster_type_to_spawn = monsters_available.find { |m| m.name.downcase == monster_type.downcase }
      if monster_type_to_spawn.nil?
        puts ERROR_SPAWN_NAME_INVALID.colorize(:red)
        puts
      else
        monsters_abounding.push(monster_type_to_spawn)
      end
    end
  # normal location spawn

  elsif should_spawn_monster?
    self.checked_for_monsters = true
    self.monsters_abounding = []
    random_monster = nil

    # get random non-boss monster

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

      if !random_monster.is_boss && self.monster_level_range.include?(random_monster.level)
        monsters_abounding.push(random_monster)
        break
      end
    end
  else
    return nil
  end
end

#remove_item(item_name) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/gemwarrior/entities/location.rb', line 98

def remove_item(item_name)
  if contains_item?(item_name)
    self.items.delete_at(items.index(items.find { |i| i.name.downcase == item_name.downcase }))
  else
    ERROR_ITEM_REMOVE_INVALID
  end
end

#remove_monster(name) ⇒ Object



106
107
108
109
# File 'lib/gemwarrior/entities/location.rb', line 106

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

#should_spawn_monster?Boolean

Returns:

  • (Boolean)


139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/gemwarrior/entities/location.rb', line 139

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