Time for action – enemy AI methods

  1. Add movement-related methods to the Enemy class:
    #region AI Methods
    private Vector2 determineMoveDirection()
    {
        if (reachedTargetSquare())
        {
            currentTargetSquare = getNewTargetSquare();
        }
    
        Vector2 squareCenter = TileMap.GetSquareCenter(
            currentTargetSquare);
    
        return squareCenter - EnemyBase.WorldCenter;
    }
    
    private bool reachedTargetSquare()
    {
        return (
            Vector2.Distance(
                EnemyBase.WorldCenter,
                TileMap.GetSquareCenter(currentTargetSquare))
            <= 2);
    }
    
    private Vector2 getNewTargetSquare()
    { List<Vector2> path = PathFinder.FindPath( TileMap.GetSquareAtPixel(EnemyBase.WorldCenter), TileMap.GetSquareAtPixel(Player.BaseSprite.WorldCenter)); if (path.Count > 1) { return new Vector2(path[1].X, path[1].Y); } else { return TileMap.GetSquareAtPixel( ...

Get XNA 4.0 Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.