問題は解決しました。まあ、まだ完全ではないですが…。
ソースコードを書きます。
ソースコードの中にサイト名「Javaでゲーム作りますが何か?」の中のコードを引用しています。
URLを下記に示します。
http://javagame.skr.jp/index.php?FrontPage#u1b27b69
#include"DxLib.h"
#include<math.h>
typedef struct P{
float x;
float y;
int width;
int height;
int jumpflag;
float vx;
float vy;
bool onGround;
}P;
typedef struct SLOPE{
int number;//番号
int imagedata;//坂道データ
}SLOPE;
int ScreenHandle;
SLOPE Chip[6]={0};
class Point{
public:
int x;
int y;
Point()
{
x=0;
y=0;
}
Point(int xx,int yy)
{
x=xx;
y=yy;
}
};
P Player={0};
int Map[15][20]={
{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0},
{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0},
{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0},
{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0},
{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0},
{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0},
{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0},
{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0},
{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0},
{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0},
{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0},
{0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0},
{0,0,0,0,0, 0,2,3,0,4, 5,1,1,6,7, 0,0,0,0,0},
{1,3,0,0,0, 2,1,1,3,0, 0,0,2,3,0, 0,2,3,0,1},
{1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1}
};
int pixelsToTiles(double pixels)
{
return (int)floor(pixels / 32);
}
int tilesToPixels(int tiles)
{
return tiles * 32;
}
int CheckHit(int px,int py,int tx1,int ty1,int tx2,int ty2,int tx3,int ty3)
{
HITRESULT_LINE a;
a=HitCheck_Line_Triangle(VGet(px,py, 0 ), VGet(px,py,0),
VGet(tx1,ty1,0), VGet(tx2,ty2,0), VGet(tx3,ty3,0) ) ;
if(a.HitFlag==1)
{
return 1;
}
return 0;
}
void Init()
{
Player.x= 320;
Player.y = 0;
Player.jumpflag = 1;
Player.width = 32;
Player.height = 64;
Player.vx=1;
Player.vy=1;
int i,j,k;
char str[100];
ScreenHandle=MakeARGB8ColorSoftImage(640,480) ;
for(i=0;i<6;i++)
{
Chip.number =i;
sprintf(str,"./Data/%d.bmp",i);
Chip.imagedata = LoadSoftImage(str) ;//0.bmpは右上45度
//1.bmpは右下45度、2.bmpと3.bmpは右上30度、4.bmpと5.bmpは右下30度
}
}
Point gettile(int xx,int yy)
{
int newX = ceil(xx);
int newY = ceil(yy);
double fromX = min(Player.x, newX);
double fromY = min(Player.y, newY);
double toX = max(Player.x, newX);
double toY = max(Player.y, newY);
int fromTileX = pixelsToTiles(fromX);
int fromTileY = pixelsToTiles(fromY);
int toTileX = pixelsToTiles(toX + Player.width-1);
int toTileY = pixelsToTiles(toY + Player.height - 1);
// 衝突しているか調べる
for (int x = fromTileX; x <= toTileX; x++) {
for (int y = fromTileY; y <= toTileY; y++) {
// 画面外は衝突
if (x < 0 || x >= 20) {
return Point(x, y);
}
if (y < 0 || y >= 15) {
return Point(x, y);
}
// ブロックがあったら衝突
if (Map[y][x] == 1) {
return Point(x, y);
}
}
}
return Point(-1,-1);
}
