2012-11-11 29 views
0

我基本上想讓角色在一個方向上走一段時間,然後停下來,然後沿另一個方向走。現在我的小精靈看上去但是不動,隨便很快就四處奔走,然後等待並再次癲癇發作。我會發布我迄今爲止的代碼以防萬一。如何讓NPC在XNA中隨機移動?

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

你怎麼樣創建一個方法來獲得一個隨機的方向

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正在移動的方向(布爾型movingLeft,movingRight等)。根據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; 
    } 
} 

我想這是用於旋轉精靈(或者繪製一個不同),所以現在你只需要一個開關:

public override void Draw(GameTime gameTime, SpriteBatch spriteBatch) 
{ 
    MoveDirection moveDirection = GetMoveDirection(this.direction); 
    switch(moveDirection) 
    { 
     case MoveDirection.Up: 
      //Draw up-facing sprite, or assign a value to a rotation variable. 
      break; 
     case MoveDirection.UpLeft: 
      //... 
    } 
}