Class: DxrbTmx

Inherits:
Object
  • Object
show all
Defined in:
dxrbtmx.rb

Overview

DXRuby用BGマップ管理クラス

マップエディタ Tiled で作成したマップファイル(.tmx)を DXRubyで使えるBGマップデータに変換します。

Version:

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (DxrbTmx) initialize(tmxfile, scrw, scrh)

コンストラクタ

Parameters:

  • tmxfile (String)

    .tmxファイルのパス

  • scrw (int)

    画面横幅ドット数

  • scrh (int)

    画面縦幅ドット数



59
60
61
# File 'dxrbtmx.rb', line 59

def initialize(tmxfile, scrw, scrh)
  init_map(tmxfile, scrw, scrh)
end

Instance Attribute Details

- (int) height (readonly)

Returns BGマップの縦幅タイル個数

Returns:

  • (int)

    BGマップの縦幅タイル個数



32
33
34
# File 'dxrbtmx.rb', line 32

def height
  @height
end

- (hash) layers_name (readonly)

Returns レイヤー名のハッシュ

Returns:

  • (hash)

    レイヤー名のハッシュ



26
27
28
# File 'dxrbtmx.rb', line 26

def layers_name
  @layers_name
end

- (int) map_height (readonly)

Returns BGマップの縦ドット数

Returns:

  • (int)

    BGマップの縦ドット数



44
45
46
# File 'dxrbtmx.rb', line 44

def map_height
  @map_height
end

- (int) map_width (readonly)

Returns BGマップの横ドット数

Returns:

  • (int)

    BGマップの横ドット数



41
42
43
# File 'dxrbtmx.rb', line 41

def map_width
  @map_width
end

- (Array) mapdata (readonly)

Returns 全レイヤーのマップデータ(三次元配列)

Returns:

  • (Array)

    全レイヤーのマップデータ(三次元配列)



23
24
25
# File 'dxrbtmx.rb', line 23

def mapdata
  @mapdata
end

- (int) screenheight (readonly)

Returns 画面を占める縦幅タイル個数

Returns:

  • (int)

    画面を占める縦幅タイル個数



50
51
52
# File 'dxrbtmx.rb', line 50

def screenheight
  @screenheight
end

- (int) screenwidth (readonly)

Returns 画面を占める横幅タイル個数

Returns:

  • (int)

    画面を占める横幅タイル個数



47
48
49
# File 'dxrbtmx.rb', line 47

def screenwidth
  @screenwidth
end

- (int) tileheight (readonly)

Returns タイルの縦ドット数

Returns:

  • (int)

    タイルの縦ドット数



38
39
40
# File 'dxrbtmx.rb', line 38

def tileheight
  @tileheight
end

- (int) tilewidth (readonly)

Returns タイルの横ドット数

Returns:

  • (int)

    タイルの横ドット数



35
36
37
# File 'dxrbtmx.rb', line 35

def tilewidth
  @tilewidth
end

- (Object) tmx

tmxライブラリで読み込んだ .tmx オブジェクト



20
21
22
# File 'dxrbtmx.rb', line 20

def tmx
  @tmx
end

- (int) width (readonly)

Returns BGマップの横幅タイル個数

Returns:

  • (int)

    BGマップの横幅タイル個数



29
30
31
# File 'dxrbtmx.rb', line 29

def width
  @width
end

Instance Method Details

- (Object) dump_layers_info

動作確認用。全レイヤー(タイルレイヤー)の情報をダンプする



155
156
157
158
159
160
161
162
163
164
165
166
# File 'dxrbtmx.rb', line 155

def dump_layers_info
  puts "# all layer info"
  @tmx.layers.each do |layer|
    name = layer.name  # C[
    w = layer.width    # C[TCY
    h = layer.height
    ltype = layer.type # C[
    data = layer.data # C[̃}bve[u
    puts "name: #{name} , #{w} x #{h} , type: #{ltype} , Data length: #{data.length}"
  end
  puts "# end"
end

- (Object) dump_map_size

動作確認用。マップサイズとタイルサイズをダンプする



144
145
146
147
148
149
150
# File 'dxrbtmx.rb', line 144

def dump_map_size
  puts "# tmx size"
  puts "map size = #{self.width} x #{self.height}"
  puts "tile size = #{self.tilewidth} x #{self.tileheight}"
  puts "layer length = #{self.get_layers_length}"
  puts "# end"
end

- (Object) dump_object_group_info

動作確認用。オブジェクトレイヤー情報をダンプする



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'dxrbtmx.rb', line 171

def dump_object_group_info
  puts "# object layer info"
  @tmx.object_groups.each do |objg|
    puts "object layer name : #{objg.name}"
    objg.objects.each do |o|
      name = o.name
      x = o.x
      y = o.y
      puts "#{name} , (#{x},#{y})"
    end
  end
  puts "# end"
end

- (Array) get_layer(layer_name)

レイヤー名を指定して、マップデータ(二次元配列)を得る

Parameters:

  • layer_name (String)

    レイヤー名

Returns:

  • (Array)

    マップデータ(二次元配列)



128
129
130
# File 'dxrbtmx.rb', line 128

def get_layer(layer_name)
  return @mapdata[@layers_name[layer_name]]
end

- (int) get_layers_length

全レイヤー枚数を返す

Returns:

  • (int)

    全レイヤー枚数



137
138
139
# File 'dxrbtmx.rb', line 137

def get_layers_length
  return @tmx.layers.length
end

- (Object) init_map(tmxfile, scrw, scrh)

マップの初期化

Parameters:

  • tmxfile (String)

    .tmxファイルのパス

  • scrw (int)

    画面横幅ドット数

  • scrh (int)

    画面縦幅ドット数



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
108
109
110
111
112
113
114
115
116
117
118
119
# File 'dxrbtmx.rb', line 70

def init_map(tmxfile, scrw, scrh)
  @tmx = Tmx.load(tmxfile)

  # }bv̏c^CL^
  @width = @tmx.width
  @height = @tmx.height

  # ^C̃hbgTCYL^
  @tilewidth = @tmx.tilewidth
  @tileheight = @tmx.tileheight

  # }bṽTCY(hbg) L^
  @map_width = @width * @tilewidth
  @map_height = @height * @tileheight

  @screenwidth = (scrw.to_f / @tilewidth).ceil
  @screenheight = (scrh.to_f / @tileheight).ceil

  # }bvpz
  @mapdata = []

  # C[ƃC[ԍ̑Ή
  @layers_name = {}

  # SC[̃}bvf[^ɌĂ
  layernum = 0
  @tmx.layers.each do |layer|
    name = layer.name # C[
    w = layer.width # C[TCY(^C)
    h = layer.height
    ltype = layer.type # C[

    @layers_name[name] = layernum

    maps = []
    i = 0
    @height.times do |my|
      lines = []
      @width.times do |mx|
        n = layer.data[i]
        # tmx0Ԃ̂ŃR[hԍ𒲐
        lines.push((n <= 0)? 0 : n - 1)
        i += 1
      end
      maps.push(lines)
    end
    @mapdata.push(maps)
    layernum += 1
  end
end