如何让NPC在XNA中随机移动?

时间:2012-11-11 00:14:54

标签: random xna 2d xna-4.0

我基本上想要一个角色在一个方向走一段时间,停下来,然后再向另一个方向走。现在我的精灵看起来但不动,在各个方向上随机快速地移动,然后等待又有一次癫痫发作。我将发布到目前为止的代码,以防有用。

class NPC: Mover
{
    int movementTimer = 0;

    public override Vector2 direction
    {
        get
        {
            Random rand = new Random();
            int randDirection = rand.Next(8);

            Vector2 inputDirection = Vector2.Zero;


            if (movementTimer >= 50)
            {
                if (randDirection == 4)
                {
                    inputDirection.X -= 1;
                    movingLeft = true;
                }
                else movingLeft = false;

                if (randDirection == 1)
                {
                    inputDirection.X += 1;
                    movingRight = true;
                }
                else movingRight = false;

                if (randDirection == 2)
                {
                    inputDirection.Y -= 1;
                    movingUp = true;
                }
                else movingUp = false;

                if (randDirection == 3)
                {
                    inputDirection.Y += 25;
                    movingDown = true;
                }
                else movingDown = false;

                if (movementTimer >= 100)
                {
                    movementTimer = 0;
                }
            }

            return inputDirection * speed;
        }
    }

    public NPC(Texture2D textureImage, Vector2 position,
        Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
        Vector2 speed)
        : base(textureImage, position, frameSize, collisionOffset, currentFrame,
        sheetSize, speed)
    {
    }

    public NPC(Texture2D textureImage, Vector2 position,
        Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
        Vector2 speed, int millisecondsPerframe)
        : base(textureImage, position, frameSize, collisionOffset, currentFrame,
        sheetSize, speed, millisecondsPerframe)
    {
    }

    public override void Update(GameTime gameTime, Rectangle clientBounds)
    {

        movementTimer++;
        position += direction;

        if (position.X < 0)
            position.X = 0;
        if (position.Y < 0)
            position.Y = 0;
        if (position.X > clientBounds.Width - frameSize.X)
            position.X = clientBounds.Width - frameSize.X;
        if (position.Y > clientBounds.Height - frameSize.Y)
            position.Y = clientBounds.Height - frameSize.Y;


        base.Update(gameTime, clientBounds);
    }
}

2 个答案:

答案 0 :(得分:2)

你怎么样创建一个获得随机方向的方法

Vector2 GetRandomDirection()
{
    Random random = new Random();
    int randomDirection = random.Next(8);

    switch (randomDirection)
    {
        case 1:
            return new Vector2(-1, 0);
        case 2:
            return new Vector2(1, 0);
        case 3:
            return new Vector2(0, -1);
        case 4:
            return new Vector2(0, 1);
        //plus perhaps additional directions?
        default:
            return Vector2.Zero;
    }
}

然后,当设定的时间过去时,您调用该方法来改变方向:

double totalElapsedSeconds = 0;
const double MovementChangeTimeSeconds = 2.0; //seconds

public override void Update(GameTime gameTime, Rectangle clientBounds)
{
    totalElapsedSeconds += gameTime.ElapsedGameTime.TotalSeconds;

    if (totalElapsedSeconds >= MovementChangeTimeSeconds)
    {
        totalElapsedSeconds -= MovementChangeTimeSeconds;
        this.direction = GetRandomDirection();
    }

    position += direction;

    //...
}

使用不同的代码来检测NPC正在移动的方向(布尔值movingLeftmovingRight等)。根据{{​​1}}向量检测这些值。这样您不必分配冗余值

direction

我认为这用于旋转精灵(或者可能是绘制另一个精灵),所以现在你只需要一个开关:

enum MoveDirection
{
    Up, Down, Left, Right, UpLeft, UpRight, DownLeft, DownRight, None
}

public MoveDirection GetMoveDirection(Vector2 direction)
{
    if (direction.Y < 0)
    {
        if (direction.X < 0)
            return MoveDirection.UpLeft;
        else if (direction.X > 0)
            return MoveDirection.UpRight;
        else
            return MoveDirection.Up;
    }
    else if (direction.Y > 0)
    {
        if (direction.X < 0)
            return MoveDirection.DownLeft;
        else if (direction.X > 0)
            return MoveDirection.DownRight;
        else
            return MoveDirection.Down;
    }
    else
    {
        if (direction.X < 0)
            return MoveDirection.Left;
        else if (direction.X > 0)
            return MoveDirection.Right;
        else
            return MoveDirection.None;
    }
}

答案 1 :(得分:0)

您可以查看Platformer入门套件,了解敌人的移动方式。

由于Microsoft停止了Windows 8的XNA开发,他删除了下载XNA 4入门套件的所有链接。

我将原始的Platformer源代码上传到bitbucket。

在这里,您可以下载XNA 4 http://vackup.blogspot.com.ar/2012/11/download-original-platformer-starter.html

的原始Platformer入门套件源代码