Class: Map

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(infopane) ⇒ Map

Fill tile map and prepare



14
15
16
17
18
19
20
21
# File 'lib/map.rb', line 14

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

  @infopane = infopane
  @sea_image = Gosu::Image.new(dir_path + '/media/sea.png')
  @ground_image = Gosu::Image.new(dir_path + '/media/ground.png')
  load_map(dir_path + '/save/m03.esf')
end

Instance Attribute Details

#infopaneObject

Returns the value of attribute infopane.



11
12
13
# File 'lib/map.rb', line 11

def infopane
  @infopane
end

#mapxObject

Returns the value of attribute mapx.



11
12
13
# File 'lib/map.rb', line 11

def mapx
  @mapx
end

#mapyObject

Returns the value of attribute mapy.



11
12
13
# File 'lib/map.rb', line 11

def mapy
  @mapy
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/map.rb', line 11

def name
  @name
end

Instance Method Details

#all_map_unitsObject

Return only units directly on map



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/map.rb', line 148

def all_map_units
  ret = []
  ii = 0
  0.upto(MAPX) { |rr|
    0.upto(MAPX) { |cc|
      if @units[rr][cc]
        ret[ii] = @units[rr][cc]
        ii += 1
      end
    }
  }
  ret
end

#all_transported_unitsObject

Return only units transported by other units



163
164
165
166
167
168
169
170
171
# File 'lib/map.rb', line 163

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



143
144
145
# File 'lib/map.rb', line 143

def all_units
  all_map_units + all_transported_units
end

#draw_tilesObject

Draw all tiles



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/map.rb', line 106

def draw_tiles
  # TODO combine with draw_units()

  0.upto(MAPX) { |rr|
    0.upto(MAPX) { |cc|
      if @tiles[rr][cc] == TILE_SEA
        # TODO tiles (z1) and units (z2) together
        im = @sea_image
      else
        im = @ground_image
      end
      im.draw(cc * TILESIZE, (rr + 1) * TILESIZE, ZTILE)
    }
  }
end

#draw_unitsObject

Draw all map units



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

def draw_units
  all_map_units.each { |uu| uu.draw} # TODO do not draw transported units? only draw them on selection (with their tile)?
end

#get_unit(cc, rr) ⇒ Object

Getter for unit in given coordinates



133
134
135
# File 'lib/map.rb', line 133

def get_unit(cc, rr)
  @units[rr][cc]
end

#load_head(row) ⇒ Object

Load core info from given head row of file Return number of units to be loaded



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/map.rb', line 56

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/map.rb', line 24

def load_map(filename)
  input = File.open(filename, 'r')
  unit_count = load_head(input.gets)

  # Load tiles
  @tiles = []
  @units = []
  0.upto(@mapy - 1) { |rr|
    @tiles[rr] = []
    @units[rr] = []
    map_row = input.gets
    0.upto(@mapx - 1) { |cc|
      case(map_row[cc])
      when SYMBOL_SEA then
        @tiles[rr][cc] = TILE_SEA
      when SYMBOL_GROUND then
        @tiles[rr][cc] = TILE_GROUND
      else
        abort("map.load_map(): Unknown terrain symbol (#{map_row[cc]})")
      end
    }
  }

  # 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 line



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

def load_unit(row)
  unit = []
  coords = []

  unit = row.split(' ')
  coords = unit[2].split('-')

  # Check faction
  fac = unit[1].to_i
  if(fac < 0 || fac > FACTIONS)
    abort("map.load_unit(): Bad faction id (#{fac})")
  end

  # Check coordinates
  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

  # Create unit
  case(unit[0])
  when SYMBOL_TOWN then
    Town.new(coords_x, coords_y, fac, self, @infopane)
  when SYMBOL_ARMY then
    Army.new(coords_x, coords_y, fac, self, @infopane)
  when SYMBOL_SHIP then
    Ship.new(coords_x, coords_y, fac, self, @infopane)
  else
    abort("map.load_unit(): Unknown unit type symbol (#{unit[0]})")
  end
end

#set_unit(cc, rr, uu) ⇒ Object

Setter for tile type in given coordinates



138
139
140
# File 'lib/map.rb', line 138

def set_unit(cc, rr, uu)
  @units[rr][cc] = uu
end

#tile(cc, rr) ⇒ Object

Getter for tile type in given coordinates



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

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