Getting back on track

I have gotten the animation to occur and the character to move correctly again.

The Animated Sprite class is as follows:

 using System;  
 using Microsoft.Xna.Framework;  
 using Microsoft.Xna.Framework.Graphics;  
 using Microsoft.Xna.Framework.Input;  
 namespace ShinyRPG2._5  
 {  
   internal class AnimatedSprite {  
    // Variables  
    private Texture2D spriteTexture;  
    private float timer = 0f;  
    private float interval = 200f;  
    private int currentFrame = 0;  
    private int spriteWidth = 24;  
    private int spriteHeight = 24;  
    private int spriteSpeed = 4;  
    private Rectangle sourceRect;  
    private Vector2 position;  
    private Vector2 origin;  
    private KeyboardState currentKBState;  
    private KeyboardState previousKBState;  
    private GamePadState currentGamePadState;  
    private GamePadState previousGamePadState;  
    // Properties  
    public Vector2 Position {  
      get { return position; }  
      set { position = value; }  
    }  
    public Vector2 Origin {  
      get { return origin; }  
      set { origin = value; }  
    }  
    public Texture2D Texture {  
      get { return spriteTexture; }  
      set { spriteTexture = value; }  
    }  
    public Rectangle SourceRect {  
      get { return sourceRect; }  
      set { sourceRect = value; }  
    }  
    // Constructor  
    public AnimatedSprite(Texture2D texture, int currentFrame, int spriteWidth, int spriteHeight) {  
      this.spriteTexture = texture;  
      this.currentFrame = currentFrame;  
      this.spriteWidth = spriteWidth;  
      this.spriteHeight = spriteHeight;  
    }  
    public void HandleSpriteMovement(GameTime gameTime) {  
      previousKBState = currentKBState;  
      currentKBState = Keyboard.GetState();  
      previousGamePadState = currentGamePadState;  
      currentGamePadState = GamePad.GetState(PlayerIndex.One);  
      sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);  
      if ( currentKBState.GetPressedKeys().Length == 0 ) {  
       if ( currentFrame > 0 && currentFrame < 2 ) {  
         currentFrame = 0;  
       }  
       if ( currentFrame > 2 && currentFrame < 4 ) {  
         currentFrame = 2;  
       }  
       if ( currentFrame > 4 && currentFrame < 6 ) {  
         currentFrame = 4;  
       }  
       if ( currentFrame > 6 && currentFrame < 8 ) {  
         currentFrame = 6;  
       }  
      }  
      // For Sprinting  
      //if ( currentKBState.IsKeyDown(Keys.LeftShift)) {  
      //  spriteSpeed = 8;  
      //  interval = 100;  
      //}  
      //else {  
      //  spriteSpeed = 4;  
      //  interval = 200;  
      //}  
      // Right, Left, Up, Down  
      if ( currentKBState.IsKeyDown(Keys.Right) || currentGamePadState.DPad.Right == ButtonState.Pressed) {  
       AnimateRight(gameTime);  
       if ( position.X < 1050 ) {  
         position.X += spriteSpeed;  
       }  
      }  
      if ( currentKBState.IsKeyDown(Keys.Left) || currentGamePadState.DPad.Left == ButtonState.Pressed) {  
       AnimateLeft(gameTime);  
       if ( position.X > 20 ) {  
         position.X -= spriteSpeed;  
       }  
      }  
      if ( currentKBState.IsKeyDown(Keys.Up) || currentGamePadState.DPad.Up == ButtonState.Pressed) {  
       AnimateUp(gameTime);  
       if ( position.Y < 700 ) {  
         position.Y -= spriteSpeed;  
       }  
      }  
      if ( currentKBState.IsKeyDown(Keys.Down) || currentGamePadState.DPad.Down == ButtonState.Pressed) {  
       AnimateDown(gameTime);  
       if ( position.Y > 25 ) {  
         position.Y += spriteSpeed;  
       }  
      }  
      origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);  
    }  
    // Animating the sprites.  
    public void AnimateRight(GameTime gameTime) {  
      if (( currentKBState != previousKBState )||(currentGamePadState != previousGamePadState)){  
      currentFrame = 2;  
    }  
    timer += ( float )gameTime.ElapsedGameTime.TotalMilliseconds;  
      if (timer > interval) {  
       currentFrame++;  
       if (currentFrame > 3) {  
         currentFrame = 2;  
       }  
       timer = 0f;  
      }  
    }  
    public void AnimateUp(GameTime gameTime){  
      if ((currentKBState != previousKBState) || (currentGamePadState != previousGamePadState))  
      {  
       currentFrame = 0;  
      }  
      timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;  
      if (timer > interval){  
       currentFrame++;  
       if (currentFrame > 1){  
         currentFrame = 0;  
       }  
       timer = 0f;  
      }  
    }  
    public void AnimateDown(GameTime gameTime){  
      if ((currentKBState != previousKBState) || (currentGamePadState != previousGamePadState))  
      {  
       currentFrame = 6;  
      }  
      timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;  
      if (timer > interval){  
       currentFrame++;  
       if (currentFrame > 7){  
         currentFrame = 6;  
       }  
       timer = 0f;  
      }  
    }  
    public void AnimateLeft(GameTime gameTime){  
      if ((currentKBState != previousKBState) || (currentGamePadState != previousGamePadState))  
      {  
       currentFrame = 4;  
      }  
      timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;  
      if (timer > interval){  
       currentFrame++;  
       if (currentFrame > 5){  
         currentFrame = 4;  
       }  
       timer = 0f;  
      }  
    }  
   }  
 }  


In the main Game class I had to implement this class as follows:

private AnimatedSprite _animatedSprite;

in the LoadContent method:

 _animatedSprite = new AnimatedSprite(Content.Load<Texture2D>("Sprites//max"), 0, 24, 24);

The parameters are the Texture, the currentFrame, Width, and Height so he is only 24 pixels tall.

_animatedSprite.Position = new Vector2(400,400);

This is just to start him close to the center.

 in the Draw method:
_animatedSprite.Position = TileEngine.PartlyLeaderPosition.ScreenPosition;
This is to draw him in the location that the TileEngine uses for tracking the party leader.

spriteBatch.Draw(_animatedSPrite.Texture, _animatedSprite.Position, _animatedSprite.SourceRect, Color.White, 0f, _animatesSprite.Origin, 3.0f, SpriteEffects.None, 0);
The 3f is there so scale him to the size I think looks good, otherwise he is tiny.

in the Update method:
_animatedSprite.HandleSpriteMovement(gameTime);

My next plans will be to implement the menu screens again. Drawing an enemy and then loading the 2d attack screens. I also would like to create an input manager to clean up some of the keyboard state and gamepad state stuff. Currently the Tile engine is what tracks and moves, just pressing the arrow keys or the dpad animates.
We'll see how far I get in the next week.




I have really picked up on Dark Souls lately and Batman Arkham City, and Tales of Graces f. I have lots to work on
Until next Thursday.

Comments

Popular Posts