I have problem with my MoveEntity()
method in a 2D platformer game (like PacMan). When player is on intersection path I want to make:
e.IsAtIntersection = true;
and then call UpdateDirection()
.
But he doesn't stop, instead he goes through paths and it doesn't matter if there are walls or empty paths. The program never reaches the above line. The member stays false
. Maybe there is the possibility to write it in an easier way or that there is a simple mistake I made. Here is the complete method:
private void MoveEntity(Entity e)
{
int angle = 0;
if (e.currentDirection == Direction.RIGHT) angle = 0;
else if (e.currentDirection == Direction.DOWN) angle = 90;
else if (e.currentDirection == Direction.LEFT) angle = 180;
else if (e.currentDirection == Direction.UP) angle = 270;
var scalex = Math.Round(Math.Cos(angle * (Math.PI / 180.0)));
var scaley = Math.Round(Math.Sin(angle * (Math.PI / 180.0)));
var velocityx = (float)(e.Speed * scalex);
var velocityy = (float)(e.Speed * scaley);
Sprite sp = sprites[e.Name];
Vector2f v = new Vector2f(sp.Position.X + velocityx, sp.Position.Y + velocityy);
sp.Position = v;
var eCenterX = sp.Position.X + sp.TextureRect.Width / 2;
var eCenterY = sp.Position.Y + sp.TextureRect.Height / 2;
var tileY = Math.Floor((eCenterY) / TILEHEIGHT);
var tileX = Math.Floor((eCenterX) / TILEWIDTH);
var tileXpos = TILEWIDTH * Math.Floor(tileX + 1);
var tileYpos = TILEHEIGHT * Math.Floor(tileY + 1);
e.X = (int)tileY;
e.Y = (int)tileX;
if (eCenterX == tileXpos && eCenterY == tileYpos)
e.IsAtIntersection = true;
else
e.IsAtIntersection = false;
}