その中で、Platinumというマップエディタを使用し出力されたcsvデータを元となるマップチップの画像に割り当てて表示してほしいとの要望を受け取り組んでいます。
しかし、画像自体の表示は出来たものの、csvのデータと合致しないものが表示され、見た目がおかしくなっています。
配列の中身を表示させると、数字自体はcsvで読み込んだ通りなので余計混乱しています。
自分でも文章でこの問題を表現するのが難しいのでわかりにくいかとは思いますが、以下コードを置きますので、おかしいところがあったら指摘を下さると助かります。
※マップチップのサイズは32*32
※csvのデータは[50][50]
/// <summary>
/// マップを管理するクラス
/// </summary>
class Map
{
const int maxX = 50; // マップの最大横列
const int maxY = 50; // マップの最大縦列
const int chipSize = 32; // マップチップサイズ
static int[,] mapData = new int[maxY, maxX];// マップのデータ
byte[] bs; // 読み込み用バイトデータ
Texture2D mapImage; // 画像
Vector2 position; // 座標
/// <summary>
/// コンストラクタ
/// </summary>
public Map()
{
position = new Vector2(0, 0);// 座標
}
/// <summary>
/// コンテンツの読み込み
/// </summary>
/// <param name="content"></param>
public void LoadContent(ContentManager content)
{
mapImage = content.Load<Texture2D>("map");
// マップデータ読み込み
MapRead("map.csv");
}
/// <summary>
/// 描画
/// </summary>
/// <param name="spriteBatch"></param>
public void Draw(SpriteBatch spriteBatch)
{
for (int x = 0; x < mapData.GetLength(0); x++)
{
for (int y = 0; y < mapData.GetLength(1); y++)
{
int a = mapData[y, x] % mapData.GetLength(0);// 横列
int b = mapData[y, x] / mapData.GetLength(1);// 縦列
// マップチップ描画
spriteBatch.Draw(
mapImage,
new Vector2(x * chipSize, y * chipSize),
new Rectangle(a * chipSize, b * chipSize, chipSize, chipSize),
Color.White,
0,
new Vector2(0, 0),
new Vector2(1.0f, 1.0f),
SpriteEffects.None,
0);
// マップデータ(数字)
spriteBatch.DrawString(
Main.font,
String.Format(mapData[y, x].ToString()),
new Vector2(x * chipSize + position.X, y * chipSize + position.Y),
Color.Yellow);
}
}
}
/// <summary>
/// マップデータをファイルから読み込み
/// </summary>
private void MapRead(string path)
{
int a;
int b;
int count;
int x, y, p;
// ファイルをバイト配列に読み込み
bs = System.IO.File.ReadAllBytes(path);
x = 0; // 現在の列
y = 0; // 現在の行
p = 0; // 現在読み込んでいる文字(データポイント)
// 最後の行が終わるまでループ
while (true)
{
a = 0;
count = 0; // 文字数カウンタ
// このループは読み込むデータの最大桁数分行う?
for (int e = 0; e < 6; ++e)
{
// 数字に変換
b = bs[p + e] - 0x30;
// 0~9の数字なら
if (b >= 0 && b <= 9)
{
a *= 10; // 次の位へ
a = a + b; // ↑で出来た1の位へ,変換された数字を入れる
count++; // カウント
}
// 数字以外なら
else
{
mapData[y, x] = a; // その前までをマップデータに代入
x++; // 列を進める
count++; // カウンタ
// 今の配列がLF(改行) またはCR(復帰)なら
if (bs[p + e] == 0xa || bs[p + e] == 0xd)
{
count++;// カウント
x = 0; // 最初の列へ
y++; // 行を進める
}
break; // 抜ける
}
}
p += count; // データポイント更新
// 最後の行の処理が終了したらループを抜ける
if (y >= mapData.GetLength(1))
break;
}
}
}