这是我检查瓷砖类型的代码。如果它是一个倾斜的瓷砖,那么它会更新角色的位置。如图所示,我的角色不会沿着斜坡移动。我究竟做错了什么?
bool Entity::PosValidTile(Tile* Tile, int tileX, int tileY)
{
if(Tile->TypeID == TILE_TYPE_ANGLEUP)
{
int Slope, Intercept;
int newY;
Vector P1,P2;
P1.X = tileX; P1.Y = tileY + TILE_SIZE;
P2.X = tileX + TILE_SIZE; P2.Y = tileY;
if(X + (Width/2) > tileX)
{
Slope = -(P2.Y- P1.Y) / (P2.X - P1.X);
Intercept = Slope * (-P1.X + P1.Y);
newY = Slope * ((X + Width/2) + Intercept);
Y = (newY - Height);
return true;
}
}
}
字符的尺寸为 48 X 96;分别是宽度和高度。他的原点如瓷砖,在左上角。
这是该功能的使用方式
bool Entity::PosValid(int NewX, int NewY)
{
bool Return = true;
int StartX = (NewX + Col_X) / TILE_SIZE;
int StartY = (NewY + Col_Y) / TILE_SIZE;
int EndX = ((NewX + Col_X) + Width - Col_Width - 1) / TILE_SIZE;
int EndY = ((NewY + Col_Y) + Height - Col_Height - 1) / TILE_SIZE;
if(Flags & ENTITY_FLAG_IGNOREMAP)
{
}else
{
for(int iY = StartY; iY <= EndY;iY++)
{
for(int iX = StartX;iX <= EndX;iX++)
{
Tile* Tile = Area::AreaControl.GetTile(iX * TILE_SIZE, iY * TILE_SIZE);
int atileY = iY * TILE_SIZE;
int atileX = iX * TILE_SIZE;
if(PosValidTile(Tile,atileX,atileY) == false)
{
Return = false;
}
}
}
}
if(Flags & ENTITY_FLAG_MAPONLY)
{
}else
{
for(int i = 0;i < (int)EntityList.size();i++)
{
if(PosValidEntity(EntityList[i], NewX, NewY) == false)
{
Return = false;
}
}
}
return Return;
}
调用前一个的函数是我的 OnMove 函数。
void Entity::OnMove(float MoveX, float MoveY)
{
if(MoveX == 0 && MoveY == 0) return;
double NewX = 0;
double NewY = 0;
CanJump = false;
MoveX *= Time::TimeControl.GetDeltaTime();
MoveY *= Time::TimeControl.GetDeltaTime();
if(MoveX != 0)
{
if(MoveX >= 0) NewX = Time::TimeControl.GetDeltaTime();
else NewX = -Time::TimeControl.GetDeltaTime();
}
if(MoveY != 0)
{
if(MoveY >= 0) NewY = Time::TimeControl.GetDeltaTime();
else NewY = -Time::TimeControl.GetDeltaTime();
}
while(true)
{
if(Flags & ENTITY_FLAG_GHOST)
{
PosValid((float)(X + NewX), (float)(Y + NewY));
X += (float)NewX;
Y += (float)NewY;
}else
{
if(PosValid((float)(X + NewX), (int)(Y)))
{
X += (float)NewX;
}else
{
SpeedX = 0;
}
if(PosValid((float)(X), (float)(Y + NewY)))
{
Y += (float)NewY;
}else
{
if(MoveY > 0)
{
CanJump = true;
}
SpeedY = 0;
}
}
MoveX += (float)-NewX;
MoveY += (float)-NewY;
if(NewX > 0 && MoveX <= 0) NewX = 0;
if(NewX < 0 && MoveX >= 0) NewX = 0;
if(NewY > 0 && MoveY <= 0) NewY = 0;
if(NewY < 0 && MoveY >= 0) NewY = 0;
if(MoveX == 0) NewX = 0;
if(MoveY == 0) NewY = 0;
if(MoveX == 0 && MoveY == 0) break;
if(NewX == 0 && NewY == 0) break;
}
}
然后在主循环中调用这个函数,就像这样 OnMove(SpeedX,SpeedY); 速度计算为休耕
SpeedX += AccelX * Time::TimeControl.GetDeltaTime();
SpeedY += AccelY * Time::TimeControl.GetDeltaTime();