Class: Gemwarrior::World
- Inherits:
-
Object
- Object
- Gemwarrior::World
- Defined in:
- lib/gemwarrior/world.rb
Constant Summary collapse
- WORLD_DIM_WIDTH =
CONSTANTS WORLD DIMENSIONS
10- WORLD_DIM_HEIGHT =
10- ERROR_LIST_PARAM_INVALID =
ERRORS
'That is not something that can be listed.'- ERROR_LOCATION_DESCRIBE_ENTITY_INVALID =
'You do not see that here.'
Instance Attribute Summary collapse
-
#locations ⇒ Object
Returns the value of attribute locations.
-
#monsters ⇒ Object
Returns the value of attribute monsters.
-
#player ⇒ Object
Returns the value of attribute player.
Instance Method Summary collapse
- #can_move?(direction) ⇒ Boolean
- #describe(point) ⇒ Object
- #describe_entity(point, entity_name) ⇒ Object
- #has_monster_to_attack?(monster_name) ⇒ Boolean
-
#initialize ⇒ World
constructor
A new instance of World.
- #list(param, details = false) ⇒ Object
- #location_by_coords(coords) ⇒ Object
- #location_coords_by_name(name) ⇒ Object
- #print_all_vars ⇒ Object
- #print_map ⇒ Object
Constructor Details
#initialize ⇒ World
Returns a new instance of World.
20 21 22 23 24 |
# File 'lib/gemwarrior/world.rb', line 20 def initialize self.monsters = init_monsters self.locations = init_locations self.player = nil end |
Instance Attribute Details
#locations ⇒ Object
Returns the value of attribute locations.
18 19 20 |
# File 'lib/gemwarrior/world.rb', line 18 def locations @locations end |
#monsters ⇒ Object
Returns the value of attribute monsters.
18 19 20 |
# File 'lib/gemwarrior/world.rb', line 18 def monsters @monsters end |
#player ⇒ Object
Returns the value of attribute player.
18 19 20 |
# File 'lib/gemwarrior/world.rb', line 18 def player @player end |
Instance Method Details
#can_move?(direction) ⇒ Boolean
168 169 170 |
# File 'lib/gemwarrior/world.rb', line 168 def can_move?(direction) location_by_coords(player.cur_coords).has_loc_to_the?(direction) end |
#describe(point) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/gemwarrior/world.rb', line 127 def describe(point) desc_text = "" desc_text << "[ #{point.name} ]\n" desc_text << point.description point.populate_monsters(self.monsters) unless point.checked_for_monsters? desc_text << point.list_items unless point.list_items.nil? desc_text << point.list_monsters unless point.list_monsters.nil? desc_text << point.list_bosses unless point.list_bosses.nil? desc_text << point.list_paths end |
#describe_entity(point, entity_name) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/gemwarrior/world.rb', line 140 def describe_entity(point, entity_name) if point.items.map(&:name).include?(entity_name) point.items.each do |i| if i.name.eql?(entity_name) return "#{i.description}" end end elsif if point.monsters_abounding.map(&:name).include?(entity_name) point.monsters_abounding.each do |m| if m.name.eql?(entity_name) return "#{m.description}" end end end elsif if point.bosses_abounding.map(&:name).include?(entity_name) point.bosses_abounding.each do |b| if b.name.eql?(entity_name) return "#{b.description}" end end end else ERROR_LOCATION_DESCRIBE_ENTITY_INVALID end end |
#has_monster_to_attack?(monster_name) ⇒ Boolean
172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/gemwarrior/world.rb', line 172 def has_monster_to_attack?(monster_name) possible_combatants = location_by_coords(player.cur_coords).monsters_abounding.map(&:name) | location_by_coords(player.cur_coords).bosses_abounding.map(&:name) possible_combatants.each do |combatant| if combatant.downcase.eql?(monster_name.downcase) return true end end return false end |
#list(param, details = false) ⇒ Object
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 |
# File 'lib/gemwarrior/world.rb', line 68 def list(param, details = false) case param when 'players' puts '[PLAYERS]' player.check_self(false) when 'monsters' puts '[MONSTERS]' if details monsters.map { |m| print m.describe unless m.is_boss} monsters.map { |m| print m.describe if m.is_boss } return else monster_text = ">> monsters: #{monsters.map(&:name).join(', ')}" end when 'items' puts '[ITEMS]' if details locations.each do |l| l.items.map { |i| print i.status } end return else item_list = [] locations.each do |l| l.items.map { |i| item_list << i.name } end ">> #{item_list.sort.join(', ')}" end when 'locations' puts '[LOCATIONS]' if details locations.map { |l| print l.status } return else ">> #{locations.map(&:name).join(', ')}" end else ERROR_LIST_PARAM_INVALID end end |
#location_by_coords(coords) ⇒ Object
109 110 111 112 113 114 115 116 |
# File 'lib/gemwarrior/world.rb', line 109 def location_by_coords(coords) locations.each do |l| if l.coords.eql?(coords) return l end end return nil end |
#location_coords_by_name(name) ⇒ Object
118 119 120 121 122 123 124 125 |
# File 'lib/gemwarrior/world.rb', line 118 def location_coords_by_name(name) locations.each do |l| if l.name.eql? name return l.coords end end return nil end |
#print_all_vars ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/gemwarrior/world.rb', line 26 def print_all_vars puts "======================\n" puts "All Variables in World\n" puts "======================\n" puts "#{list("players", true)}\n" puts "#{list("monsters", true)}\n\n" puts "#{list("items", true)}\n\n" puts "#{list("locations", true)}\n" end |
#print_map ⇒ Object
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 |
# File 'lib/gemwarrior/world.rb', line 36 def print_map 0.upto(WORLD_DIM_HEIGHT-1) do |count_y| print ' ' 0.upto(WORLD_DIM_WIDTH-1) do print '---' end print "\n" print "#{(WORLD_DIM_HEIGHT-1) - count_y} " 0.upto(WORLD_DIM_WIDTH-1) do |count_x| cur_map_coords = {:x => count_x, :y => (WORLD_DIM_HEIGHT-1) - count_y} if self.player.cur_coords.eql?(cur_map_coords) print '|O|' elsif location_by_coords(cur_map_coords) print '|X|' else print '| |' end end print "\n" end print ' ' 0.upto(WORLD_DIM_WIDTH-1) do print '---' end puts print ' ' 0.upto(WORLD_DIM_WIDTH-1) do |count_x| print "#{count_x} " end return end |