オブジェクトのパスを含む配列[15][15]があります。0は壁で、他のものはパスを作成します(1-> 2-> 3-> ...-> end)。これは、450px x 450pxのゲームフィールドを反映しています(30px x 30pxは1つのフィールドです)。
次のようなarray[15][15]の場合:
080000000000000
010111011101110
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
011101110111010
000000000000090
私は得る:
私のオブジェクトはある速度*速度で移動します:
void Enemy::move()
{
this->get_dir();
this->x += this->velX * this->speed; // velX = -1 is left, 1 is right
this->y += this->velY * this->speed; // velY = -1 is up, 1 is down
}
get_dir()は、次のように、設定する必要のある方向(速度)をチェックします。
void Enemy::get_dir()
{
Point p; // {x, y} struct
p = this->get_grid(); // it tries to calculate X, Y axis into an array number
if (p.y < 14 && path_grid[p.y + 1][p.x] - 1 == path_grid[p.y][p.x])
{
this->velY = 1;
this->velX = 0;
return;
}
/* same goes for rest of directions */
this->velX = this->velY = 0; // if none matched it stops (reached end)
return;
}
Point Enemy::get_grid()
{
int x, y;
for(y = 0;y < 15;y++)
{
if (this -> y >= y * 30 && this->y < (y + 1) * 30) break;
}
for(x = 0;x < 15;x++)
{
if (this -> x >= x * 30 && this->x < (x + 1) * 30) break;
}
return make_point(x, y);
}
しかし、お気づきかもしれませんが、これにより、私のオブジェクトは次のようなパスをたどります。
左上隅をチェックしているので、右に動かすと原点が変わるはずですが、どうしたらいいのかわかりません。30(オブジェクトビットマップサイズ)を追加しようとすると、うまくいけば上→右隅で止まります。ここでの解決策は何ですか?