Class: BgAtari
- Inherits:
-
Object
- Object
- BgAtari
- Defined in:
- bgatari.rb
Overview
DXRuby用BGアタリ処理クラス
タイルで構成されたBGマップデータを元にして地形アタリ処理をします。
-
DXRuby が必要。画像スキャン処理に使ってます。
Constant Summary
- ADJUST_NONE =
座標補正しない。当たってるかどうかだけ調べる
0
- ADJUST_UP =
上方向に座標補正
1
- ADJUST_DOWN =
下方向に座標補正
2
- ADJUST_LEFT =
左方向に座標補正
3
- ADJUST_RIGHT =
右方向に座標補正
4
Instance Attribute Summary (collapse)
-
- (int) adjust_x
readonly
補正後の x 座標.
-
- (int) adjust_y
readonly
補正後の y 座標.
-
- (Array) atari_tbls
readonly
タイルアタリ判別用データ.
-
- (int) bg_code
readonly
チェックした座標にあるBGアタリ番号.
-
- (Boolean) continue_tile
readonly
更に補正必要な可能性があるならtrue、そうでなければfalse.
-
- (int) height
readonly
BGマップの縦幅タイル個数.
-
- (int) map_height
readonly
BGマップの縦ドット数.
-
- (int) map_width
readonly
BGマップの横ドット数.
-
- (Array) mapdata
readonly
BGアタリ用マップデータ(二次元配列).
-
- (int) screenheight
readonly
画面を占める縦幅タイル個数.
-
- (int) screenwidth
readonly
画面を占める横幅タイル個数.
-
- (int) tileheight
readonly
タイルの縦ドット数.
-
- (int) tilewidth
readonly
タイルの横ドット数.
-
- (int) width
readonly
BGマップの横幅タイル個数.
Instance Method Summary (collapse)
-
- (Array) adjust_bg_hit(x, y, dir = ADJUST_UP, floor_only = false)
BGアタリで補正した座標値を得る.
-
- (int, ...) check_bg_hit(x, y, dir = ADJUST_NONE, floor_only = false)
BGアタリと当たってるかを返す.
-
- (Object) dump_tile_atari_data
動作確認用。タイルアタリ判定用データをダンプする.
-
- (int) get_bg_code(x, y)
指定座標のBGアタリ番号を取得.
-
- (BgAtari) initialize(imgarr, mapdata, scrw, scrh, nullcode = 0, no_floor_start = 0x7fffffff, no_floor_end = 0x7fffffff)
constructor
コンストラクタ.
-
- (Array) make_tile_atari_table(img)
1タイル分のアタリ判定用データを作成.
Constructor Details
- (BgAtari) initialize(imgarr, mapdata, scrw, scrh, nullcode = 0, no_floor_start = 0x7fffffff, no_floor_end = 0x7fffffff)
画像を渡してアタリ判定用データを作成していますが、 あらかじめ配列データを作って渡すように修正すれば、 他のライブラリでも使えるようになるかもしれません。
コンストラクタ
タイル画像をスキャンしてテーブルを作成する。
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'bgatari.rb', line 92 def initialize(imgarr, mapdata, scrw, scrh, nullcode = 0, no_floor_start = 0x7fffffff, no_floor_end = 0x7fffffff) @bg_code = 0 @continue_tile = false @adjust_x = 0 # ̍W @adjust_y = 0 @nullcode = nullcode @no_floor_start = no_floor_start @no_floor_end = no_floor_end @atari_tbls = {} @mapdata = mapdata # BG}bv̏c^CL^ @width = mapdata[0].length @height = mapdata.length # ^C̃hbgTCYL^ @tilewidth = imgarr[0].width @tileheight = imgarr[0].height # BG}bṽhbgTCYL^ @map_width = @tilewidth * @width @map_height = @tileheight * @height # ʂ߂^CL^ @screenwidth = (scrw.to_f / @tilewidth).ceil @screenheight = (scrh.to_f / @tileheight).ceil # e^C̃A^pf[^쐬B # ŁAe^C摜̃hbgăf[^쐬Ă邪A # 炩߃f[^zēnA # ̃Cułg悤ɂȂƎvc @mapdata.each do |lines| lines.each do |i| unless @atari_tbls.key?(i) img = imgarr[i] @atari_tbls[i] = make_tile_atari_table(img) end end end # pp @atari_tbls end |
Instance Attribute Details
- (int) adjust_x (readonly)
Returns 補正後の x 座標
64 65 66 |
# File 'bgatari.rb', line 64 def adjust_x @adjust_x end |
- (int) adjust_y (readonly)
Returns 補正後の y 座標
67 68 69 |
# File 'bgatari.rb', line 67 def adjust_y @adjust_y end |
- (Array) atari_tbls (readonly)
Returns タイルアタリ判別用データ
73 74 75 |
# File 'bgatari.rb', line 73 def atari_tbls @atari_tbls end |
- (int) bg_code (readonly)
Returns チェックした座標にあるBGアタリ番号
61 62 63 |
# File 'bgatari.rb', line 61 def bg_code @bg_code end |
- (Boolean) continue_tile (readonly)
Returns 更に補正必要な可能性があるならtrue、そうでなければfalse
70 71 72 |
# File 'bgatari.rb', line 70 def continue_tile @continue_tile end |
- (int) height (readonly)
Returns BGマップの縦幅タイル個数
40 41 42 |
# File 'bgatari.rb', line 40 def height @height end |
- (int) map_height (readonly)
Returns BGマップの縦ドット数
52 53 54 |
# File 'bgatari.rb', line 52 def map_height @map_height end |
- (int) map_width (readonly)
Returns BGマップの横ドット数
49 50 51 |
# File 'bgatari.rb', line 49 def map_width @map_width end |
- (Array) mapdata (readonly)
Returns BGアタリ用マップデータ(二次元配列)
34 35 36 |
# File 'bgatari.rb', line 34 def mapdata @mapdata end |
- (int) screenheight (readonly)
Returns 画面を占める縦幅タイル個数
58 59 60 |
# File 'bgatari.rb', line 58 def screenheight @screenheight end |
- (int) screenwidth (readonly)
Returns 画面を占める横幅タイル個数
55 56 57 |
# File 'bgatari.rb', line 55 def screenwidth @screenwidth end |
- (int) tileheight (readonly)
Returns タイルの縦ドット数
46 47 48 |
# File 'bgatari.rb', line 46 def tileheight @tileheight end |
- (int) tilewidth (readonly)
Returns タイルの横ドット数
43 44 45 |
# File 'bgatari.rb', line 43 def tilewidth @tilewidth end |
- (int) width (readonly)
Returns BGマップの横幅タイル個数
37 38 39 |
# File 'bgatari.rb', line 37 def width @width end |
Instance Method Details
- (Array) adjust_bg_hit(x, y, dir = ADJUST_UP, floor_only = false)
BGアタリで補正した座標値を得る
画面横幅、縦幅を超える補正値は求めない。
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'bgatari.rb', line 316 def adjust_bg_hit(x, y, dir = ADJUST_UP, floor_only = false) x = x.to_i y = y.to_i hosei = check_bg_hit(x, y, dir, floor_only) unless hosei # Kv @bg_code = @nullcode return nil, nil end case dir when ADJUST_UP, ADJUST_DOWN # ㉺ɕW code = @bg_code @screenheight.times do # [vɂȂȂ悤ɁA # ʂ߂[v y += hosei break unless @continue_tile hosei = check_bg_hit(x, y, dir, floor_only) unless hosei @bg_code = code break else code = @bg_code end end return x, y when ADJUST_LEFT, ADJUST_RIGHT # EɕW code = @bg_code @screenwidth.times do x += hosei break unless @continue_tile hosei = check_bg_hit(x, y, dir, floor_only) unless hosei @bg_code = code break else code = @bg_code end end return x, y end retun nil, nil end |
- (int, ...) check_bg_hit(x, y, dir = ADJUST_NONE, floor_only = false)
BGアタリと当たってるかを返す
-
最後に当たったBGアタリ番号は @bg_code に記録される。
-
補正座標取得時、まだ補正が必要な可能性があるなら、 @continue_tile が true になる。
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'bgatari.rb', line 223 def check_bg_hit(x, y, dir = ADJUST_NONE, floor_only = false) @continue_tile = false x = x.to_i y = y.to_i hx = x % @map_width hy = y % @map_height tx = (hx / @tilewidth).to_i ty = (hy / @tileheight).to_i code = @mapdata[ty][tx] # ŌɓBGA^ԍL^قȂcH @bg_code = code if code != @nullcode # @bg_code = code return nil if code == @nullcode unless floor_only # Ԃ牺_Ȃǂ́AR[hƂĈ return nil if @no_floor_start <= code and code <= @no_floor_end end ymin, ymax = @atari_tbls[code][0][hx % tilewidth] return nil if ymin == nil or ymax == nil chky = hy % tileheight return nil if chky < ymin or ymax < chky case dir when ADJUST_NONE # Ă邱ƂԂ return true when ADJUST_UP # ւ̕lԂ hosei = ymin - chky - 1 @adjust_x = x @adjust_y = y + hosei @continue_tile = true if ymin == 0 return hosei when ADJUST_DOWN # ւ̕lԂ hosei = ymax - chky + 1 @adjust_x = x @adjust_y = y + hosei @continue_tile = true if ymax == (@tileheight - 1) return hosei else # A͉Eւ̕lԂ xmin, xmax = @atari_tbls[code][1][hy % tileheight] return nil if xmin == nil or xmax == nil chkx = hx % tilewidth # {Aȉ̏͐Ȃ͂cÔ߂ɓĂB # ł𑬂ȂRgAEg return nil if chkx < xmin or xmax < chkx case dir when ADJUST_LEFT # ւ̕lԂ hosei = xmin - chkx - 1 @adjust_x = x + hosei @adjust_y = y @continue_tile = true if xmin == 0 return hosei when ADJUST_RIGHT # Eւ̕lԂ hosei = xmax - chkx + 1 @adjust_x = x + hosei @adjust_y = y @continue_tile = true if xmax == (@tilewidth - 1) return hosei end end return true end |
- (Object) dump_tile_atari_data
動作確認用。タイルアタリ判定用データをダンプする
369 370 371 372 373 374 375 |
# File 'bgatari.rb', line 369 def dump_tile_atari_data self.atari_tbls.keys.sort.each do |i| puts "\n" puts "# code = #{i}" p self.atari_tbls[i] end end |
- (int) get_bg_code(x, y)
指定座標のBGアタリ番号を取得
199 200 201 202 203 204 |
# File 'bgatari.rb', line 199 def get_bg_code(x, y) tx = ((x.to_i % @map_width) / @tilewidth).to_i ty = ((y.to_i % @map_height) / @tileheight).to_i @bg_code = @mapdata[ty][tx] return @bg_code end |
- (Array) make_tile_atari_table(img)
1タイル分のアタリ判定用データを作成
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'bgatari.rb', line 146 def make_tile_atari_table(img) tbls = [] w, h = img.width, img.height # ㉺ŎgA^pe[u쐬 tbl = [] w.times do |x| ymin = nil ymax = nil h.times do |y| unless ymin # hbgH ymin = y if img[x, y][0] > 128 end unless ymax # hbgH ymax = ((h - 1) - y) if img[x, (h - 1) - y][0] > 128 end end tbl.push([ymin, ymax]) end tbls.push(tbl) # ẼA^pe[u쐬 tbl = [] h.times do |y| xmin = nil xmax = nil w.times do |x| unless xmin # hbgH xmin = x if img[x, y][0] > 128 end unless xmax # hbgH xmax = ((w - 1) - x) if img[(w - 1) - x, y][0] > 128 end end tbl.push([xmin, xmax]) end tbls.push(tbl) return tbls end |