Class: Map

Inherits:
Object
  • Object
show all
Defined in:
lib/lib/user_interface/map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, infopane) ⇒ Map

Returns a new instance of Map.



10
11
12
13
14
15
16
17
18
19
# File 'lib/lib/user_interface/map.rb', line 10

def initialize(file, infopane)
  dir_path = File.dirname(__FILE__)

  @infopane = infopane
  @known_unit_types = Hash[
    [Army, Ship, Town].collect { |ii| [ii.map_symbol, ii] }
  ]

  load_map!(dir_path + "/../../save/#{file}.esf")
end

Instance Attribute Details

#infopaneObject

Returns the value of attribute infopane.



8
9
10
# File 'lib/lib/user_interface/map.rb', line 8

def infopane
  @infopane
end

#mapxObject

Returns the value of attribute mapx.



8
9
10
# File 'lib/lib/user_interface/map.rb', line 8

def mapx
  @mapx
end

#mapyObject

Returns the value of attribute mapy.



8
9
10
# File 'lib/lib/user_interface/map.rb', line 8

def mapy
  @mapy
end

#nameObject

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_unitsObject

Return only units directly on map



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/lib/user_interface/map.rb', line 133

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_tilesObject

Return all tiles



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/lib/user_interface/map.rb', line 105

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_unitsObject

Return only units transported by other units



148
149
150
151
152
153
154
155
156
# File 'lib/lib/user_interface/map.rb', line 148

def all_transported_units
  ret = []
  all_map_units.each { |uu|
    if uu.is_transporting?
      ret += uu.cargo
    end
  }
  ret
end

#all_unitsObject

Return both map units and transported units



128
129
130
# File 'lib/lib/user_interface/map.rb', line 128

def all_units
  all_map_units + all_transported_units
end

#draw_tilesObject

Draw all tiles



95
96
97
# File 'lib/lib/user_interface/map.rb', line 95

def draw_tiles
  all_tiles.each { |tt| tt.draw}
end

#get_unit(cc, rr) ⇒ Object

Getter for unit at given coordinates



118
119
120
# File 'lib/lib/user_interface/map.rb', line 118

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



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lib/user_interface/map.rb', line 49

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!(file_path) ⇒ Object

Load map from file



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lib/user_interface/map.rb', line 22

def load_map!(file_path)
  unless File.file?(file_path)
    abort("Map.load_map!(): File not found (#{file_path})")
  end

  input = File.open(file_path, '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



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
# File 'lib/lib/user_interface/map.rb', line 64

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



123
124
125
# File 'lib/lib/user_interface/map.rb', line 123

def set_unit(cc, rr, uu)
  @tiles[rr][cc].unit = uu
end

#tile(cc, rr) ⇒ Object

Getter for tile at given coordinates



100
101
102
# File 'lib/lib/user_interface/map.rb', line 100

def tile(cc, rr)
  @tiles[rr][cc]
end