Class: Map
- Inherits:
-
Object
- Object
- Map
- Defined in:
- lib/lib/user_interface/map.rb
Instance Attribute Summary collapse
-
#infopane ⇒ Object
Returns the value of attribute infopane.
-
#mapx ⇒ Object
Returns the value of attribute mapx.
-
#mapy ⇒ Object
Returns the value of attribute mapy.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#all_map_units ⇒ Object
Return only units directly on map.
-
#all_tiles ⇒ Object
Return all tiles.
-
#all_transported_units ⇒ Object
Return only units transported by other units.
-
#all_units ⇒ Object
Return both map units and transported units.
-
#draw_tiles ⇒ Object
Draw all tiles.
-
#get_unit(cc, rr) ⇒ Object
Getter for unit at given coordinates.
-
#initialize(infopane) ⇒ Map
constructor
A new instance of Map.
-
#load_head(row) ⇒ Object
Load core info from given head row of file Return number of units to be loaded.
-
#load_map(filename) ⇒ Object
Load map from file.
-
#load_unit(row) ⇒ Object
Load one unit from given row.
-
#set_unit(cc, rr, uu) ⇒ Object
Setter for unit at given coordinates.
-
#tile(cc, rr) ⇒ Object
Getter for tile at given coordinates.
Constructor Details
#initialize(infopane) ⇒ Map
Returns a new instance of Map.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/lib/user_interface/map.rb', line 10 def initialize(infopane) dir_path = File.dirname(__FILE__) @infopane = infopane @known_unit_types = Hash[ [Army, Ship, Town].collect { |ii| [ii.map_symbol, ii] } ] # TODO selectable starting scenario load_map(dir_path + '/../../save/m02.esf') end |
Instance Attribute Details
#infopane ⇒ Object
Returns the value of attribute infopane.
8 9 10 |
# File 'lib/lib/user_interface/map.rb', line 8 def infopane @infopane end |
#mapx ⇒ Object
Returns the value of attribute mapx.
8 9 10 |
# File 'lib/lib/user_interface/map.rb', line 8 def mapx @mapx end |
#mapy ⇒ Object
Returns the value of attribute mapy.
8 9 10 |
# File 'lib/lib/user_interface/map.rb', line 8 def mapy @mapy end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/lib/user_interface/map.rb', line 8 def name @name end |
Instance Method Details
#all_map_units ⇒ Object
Return only units directly on map
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/lib/user_interface/map.rb', line 130 def all_map_units ret = [] ii = 0 0.upto(MAPX) { |rr| 0.upto(MAPX) { |cc| if get_unit(cc, rr) ret[ii] = get_unit(cc, rr) ii += 1 end } } ret end |
#all_tiles ⇒ Object
Return all tiles
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/lib/user_interface/map.rb', line 102 def all_tiles ret = [] ii = 0 0.upto(MAPX) { |rr| 0.upto(MAPX) { |cc| ret[ii] = @tiles[rr][cc] ii += 1 } } ret end |
#all_transported_units ⇒ Object
Return only units transported by other units
145 146 147 148 149 150 151 152 153 |
# File 'lib/lib/user_interface/map.rb', line 145 def all_transported_units ret = [] all_map_units.each { |uu| if uu.is_transporting? ret += uu.cargo end } ret end |
#all_units ⇒ Object
Return both map units and transported units
125 126 127 |
# File 'lib/lib/user_interface/map.rb', line 125 def all_units all_map_units + all_transported_units end |
#draw_tiles ⇒ Object
Draw all tiles
92 93 94 |
# File 'lib/lib/user_interface/map.rb', line 92 def draw_tiles all_tiles.each { |tt| tt.draw} end |
#get_unit(cc, rr) ⇒ Object
Getter for unit at given coordinates
115 116 117 |
# File 'lib/lib/user_interface/map.rb', line 115 def get_unit(cc, rr) @tiles[rr][cc].unit end |
#load_head(row) ⇒ Object
Load core info from given head row of file Return number of units to be loaded
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/lib/user_interface/map.rb', line 46 def load_head(row) head = [] size = [] head = row.split(' ') size = head[1].split('x') @name = head[0] @mapx = size[0].to_i @mapy = size[1].to_i head[2].to_i # unit_count end |
#load_map(filename) ⇒ Object
Load map from file
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/lib/user_interface/map.rb', line 23 def load_map(filename) input = File.open(filename, 'r') unit_count = load_head(input.gets) # Load tiles @tiles = [] 0.upto(@mapy - 1) { |rr| @tiles[rr] = [] map_row = input.gets 0.upto(@mapx - 1) { |cc| @tiles[rr][cc] = Tile.new(cc, rr, map_row[cc], @infopane) } } # Load units unit_count.times { load_unit(input.gets) } puts("Save loaded: #{@name} with #{unit_count} units") input.close end |
#load_unit(row) ⇒ Object
Load one unit from given row
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 |
# File 'lib/lib/user_interface/map.rb', line 61 def load_unit(row) unit = [] coords = [] unit = row.split(' ') # Check coordinates coords = unit[2].split('-') coords_x = coords[0].to_i coords_y = coords[1].to_i if (coords_x < 0 || coords_x >= @mapx || coords_y < 0 || coords_y >= @mapy) abort("map.load_unit(): Unit out of map borders (#{coords_x}-#{coords_y})") end # Check faction fac = unit[1].to_i if(fac < 0 || fac > FACTIONS) abort("map.load_unit(): Bad faction id (#{fac})") end # Create unit unit_type = unit[0] if @known_unit_types.include?(unit_type) @known_unit_types[unit_type].new(coords_x, coords_y, fac, self, @infopane) else abort("map.load_unit(): Unknown unit type symbol (#{unit_type})") end end |
#set_unit(cc, rr, uu) ⇒ Object
Setter for unit at given coordinates
120 121 122 |
# File 'lib/lib/user_interface/map.rb', line 120 def set_unit(cc, rr, uu) @tiles[rr][cc].unit = uu end |
#tile(cc, rr) ⇒ Object
Getter for tile at given coordinates
97 98 99 |
# File 'lib/lib/user_interface/map.rb', line 97 def tile(cc, rr) @tiles[rr][cc] end |