#15
by ISLe » 6年前
CCollision::CheckMapを改造。
戻り値のRECTは、矩形ではなく、各方向に何ピクセルめり込んでいるかを表す。
rcMoverは、CMoverクラスから直接持ってきたいところだが、引数周りを変えると変更点が多くなりすぎるので先送りとする。
コード:
RECT CCollision::CheckMap(int x, int y) {
RECT rcMover = {
x + mPt[0].x,
y + mPt[0].y,
x + mPt[7].x + 1,
y + mPt[7].y + 1,
};
RECT rc, rcResult;
CopyRect(&rcResult, &rcMover);
DWORD w_map = Game->MapData().GetMapWidth();
DWORD h_map = Game->MapData().GetMapHeight();
DWORD w_chip = Game->MapData().GetChipWidth();
DWORD h_chip = Game->MapData().GetChipHeight();
//レイヤー1が四角形床。
for (DWORD y_map = 0; y_map < h_map; y_map++) {
for (DWORD x_map = 0; x_map < w_map; x_map++) {
int index = Game->MapData().GetValue(1, x_map, y_map);
#if 0
//斜め床の判定。
int indexslope = GetChipLayerNum(x, y);
int src_x = (index % Game->GetBitCount()) * cwidth;
int src_y = (index / Game->GetBitCount()) * cheight;
//坂道の当たり判定があるのなら、-2を返す。
if (indexslope != 0) {
//res.x = -2;
//res.y = -2;
return res;
}
#endif
if (index == 0) {
// 四角いブロック
RECT rcBlock = { x_map*w_chip, y_map*h_chip, (x_map + 1)*w_chip, (y_map + 1)*h_chip };
if (IntersectRect(&rc, &rcMover, &rcBlock)) {
// めり込んでいる分だけ領域を削る
if (rc.left == rcMover.left) { // 左寄り
if (rc.right > rcResult.left) rcResult.left = rc.right;
}
if (rc.top == rcMover.top) { // 上寄り
if (rc.bottom > rcResult.top) rcResult.top = rc.bottom;
}
if (rc.right == rcMover.right) { // 右寄り
if (rc.left < rcResult.right) rcResult.right = rc.left;
}
if (rc.bottom == rcMover.bottom) { // 下寄り
if (rc.top < rcResult.bottom) rcResult.bottom = rc.top;
}
}
}
}
}
// めり込んでいる量に変換
rcResult.left -= rcMover.left;
rcResult.top -= rcMover.top;
rcResult.right = rcMover.right - rcResult.right;
rcResult.bottom = rcMover.bottom - rcResult.bottom;
return rcResult;
}
CCollision::CheckMapを改造。
戻り値のRECTは、矩形ではなく、各方向に何ピクセルめり込んでいるかを表す。
rcMoverは、CMoverクラスから直接持ってきたいところだが、引数周りを変えると変更点が多くなりすぎるので先送りとする。
[code=cpp]RECT CCollision::CheckMap(int x, int y) {
RECT rcMover = {
x + mPt[0].x,
y + mPt[0].y,
x + mPt[7].x + 1,
y + mPt[7].y + 1,
};
RECT rc, rcResult;
CopyRect(&rcResult, &rcMover);
DWORD w_map = Game->MapData().GetMapWidth();
DWORD h_map = Game->MapData().GetMapHeight();
DWORD w_chip = Game->MapData().GetChipWidth();
DWORD h_chip = Game->MapData().GetChipHeight();
//レイヤー1が四角形床。
for (DWORD y_map = 0; y_map < h_map; y_map++) {
for (DWORD x_map = 0; x_map < w_map; x_map++) {
int index = Game->MapData().GetValue(1, x_map, y_map);
#if 0
//斜め床の判定。
int indexslope = GetChipLayerNum(x, y);
int src_x = (index % Game->GetBitCount()) * cwidth;
int src_y = (index / Game->GetBitCount()) * cheight;
//坂道の当たり判定があるのなら、-2を返す。
if (indexslope != 0) {
//res.x = -2;
//res.y = -2;
return res;
}
#endif
if (index == 0) {
// 四角いブロック
RECT rcBlock = { x_map*w_chip, y_map*h_chip, (x_map + 1)*w_chip, (y_map + 1)*h_chip };
if (IntersectRect(&rc, &rcMover, &rcBlock)) {
// めり込んでいる分だけ領域を削る
if (rc.left == rcMover.left) { // 左寄り
if (rc.right > rcResult.left) rcResult.left = rc.right;
}
if (rc.top == rcMover.top) { // 上寄り
if (rc.bottom > rcResult.top) rcResult.top = rc.bottom;
}
if (rc.right == rcMover.right) { // 右寄り
if (rc.left < rcResult.right) rcResult.right = rc.left;
}
if (rc.bottom == rcMover.bottom) { // 下寄り
if (rc.top < rcResult.bottom) rcResult.bottom = rc.top;
}
}
}
}
}
// めり込んでいる量に変換
rcResult.left -= rcMover.left;
rcResult.top -= rcMover.top;
rcResult.right = rcMover.right - rcResult.right;
rcResult.bottom = rcMover.bottom - rcResult.bottom;
return rcResult;
}
[/code]