0

So, what I'm having trouble with is a simple ai that runs from the player. There are walls scattered about the level that the enemy has to get by, but each time he collides with the wall, he gets stuck. I would greatly appreciate some help, here is the relevent code:

 public bool EnemyCollision()
        {
            Rectangle hWallRect = new Rectangle(
               (int)hWallPos.X,
               (int)hWallPos.Y,
               hWallWidth ,
               hWallHeight);

            Rectangle enemyRect = new Rectangle(
                (int)enemy.enemyPosition.X,
                (int)enemy.enemyPosition.Y,
                enemy.enemyWidth,
                enemy.enemyHeight);

            return hWallRect.Intersects(enemyRect);
        }

        if (EnemyCollision())
            {
                if (enemy.enemyPosition.X <= hWallPos.X)
                    enemy.enemyPosition.X += enemy.enemySpeed;

                if (enemy.enemyPosition.Y <= hWallPos.Y)
                    enemy.enemyPosition.Y += enemy.enemySpeed;

                if (enemy.enemyPosition.X >= hWallPos.X)
                    enemy.enemyPosition.X -= enemy.enemySpeed;

                if (enemy.enemyPosition.Y >= hWallPos.Y)
                    enemy.enemyPosition.Y -= enemy.enemySpeed;
            }
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Randlathor05
0

Why not use a direction property, then if collision() is true, you can change direction and decrease the x /y away from the wall?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.