Commits

Tim Eicher  committed 9e9f557

Created the Animation class and using it to draw the Player.
(Part 5 - Animating the Player)

  • Participants
  • Parent commits 417b3c5

Comments (0)

Files changed (4)

File ShooterTutorial/Animation.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Content;
+using Microsoft.Xna.Framework.Graphics;
+
+namespace ShooterTutorial
+{
+    public class Animation
+    {
+        // The image representing the collection of images used for animation
+        Texture2D spriteStrip;
+
+        // The scale used to display the sprite strip
+        float scale;
+
+        // The time since we last updated the frame
+        int elapsedTime;
+
+        // The time we display a frame until the next one
+        int frameTime;
+
+        // The number of frames that the animation contains
+        int frameCount;
+
+        // The index of the current frame we are displaying
+        int currentFrame;
+
+        // The color of the frame we will be displaying
+        Color color;
+
+        // The area of the image strip we want to display
+        Rectangle sourceRect = new Rectangle();
+
+        // The area where we want to display the image strip in the game
+        Rectangle destinationRect = new Rectangle();
+
+        // Width of a given frame
+        public int FrameWidth;
+
+        // Height of a given frame
+        public int FrameHeight;
+
+        // The state of the Animation
+        public bool Active;
+
+        // Determines if the animation will keep playing or deactivate after one run
+        public bool Looping;
+
+        // Width of a given frame
+        public Vector2 Position;
+
+        public void Initialize(Texture2D texture, Vector2 position, int frameWidth, int frameHeight, int frameCount, int frametime, Color color, float scale, bool looping)
+        {
+            this.color = color;
+            this.FrameWidth = frameWidth;
+            this.FrameHeight = frameHeight;
+            this.frameCount = frameCount;
+            this.frameTime = frametime;
+            this.scale = scale;
+
+            Looping = looping;
+            Position = position;
+            spriteStrip = texture;
+
+            elapsedTime = 0;
+            currentFrame = 0;
+
+            // Set the Animation to active by default.
+            Active = true;
+        }
+
+        public void Update(GameTime gameTime)
+        {
+            // Do not update the game if we are not active
+            if(Active == false) return;
+
+            elapsedTime += (int) gameTime.ElapsedGameTime.TotalMilliseconds;
+
+            if (elapsedTime > frameTime)
+            {
+                currentFrame++;
+
+                if (currentFrame == frameCount)
+                {
+                    currentFrame = 0;
+                    if (Looping == false)
+                        Active = false;
+                }
+
+                elapsedTime = 0;
+            }
+
+            // Grab the correct frame in the image strip by multiplying the currentFrame index by the Frame width
+            sourceRect = new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight);
+
+            // Grab the frame in the image strip by multiplying the currentFrame index by the frame width.
+            destinationRect = new Rectangle((int) Position.X - (int) (FrameWidth * scale) / 2,
+                                            (int) Position.Y - (int) (FrameHeight * scale) / 2,
+                                            (int) (FrameWidth * scale),
+                                            (int) (FrameHeight * scale));
+        }
+
+        public void Draw(SpriteBatch spriteBatch)
+        {
+            if (Active)
+                spriteBatch.Draw(spriteStrip, destinationRect, sourceRect, color);
+        }
+    }
+}

File ShooterTutorial/Game1.cs

             // Create a new SpriteBatch, which can be used to draw textures.
             _spriteBatch = new SpriteBatch(GraphicsDevice);
 
-            // TODO: use this.Content to load your game content here
             // Load the player resources
-
             Rectangle titleSafeArea = GraphicsDevice.Viewport.TitleSafeArea;
             var playerPosition = new Vector2(titleSafeArea.X, titleSafeArea.Y + titleSafeArea.Height/2);
-            Texture2D playerTexture = Content.Load<Texture2D>("Graphics\\player");
-            _player.Initialize(playerTexture, playerPosition);
-
+            
+            Texture2D playerTexture = Content.Load<Texture2D>("Graphics\\shipAnimation");
+            Animation playerAnimation = new Animation();
+            playerAnimation.Initialize(playerTexture, playerPosition, 115, 69, 8, 30, Color.White, 1, true);
+            
+            _player.Initialize(playerAnimation, playerPosition);
         }
 
         /// <summary>
 
         void UpdatePlayer(GameTime gameTime)
         {
+            _player.Update(gameTime);
+
             // Touch inputs
             while (TouchPanel.IsGestureAvailable)
             {
             // Make sure that the player does not go out of bounds
             _player.Position.X = MathHelper.Clamp(_player.Position.X, 0, GraphicsDevice.Viewport.Width - _player.Width);
             _player.Position.Y = MathHelper.Clamp(_player.Position.Y, 0, GraphicsDevice.Viewport.Height - _player.Height);
-
-
         }
 
         /// <summary>

File ShooterTutorial/Player.cs

 {
     class Player
     {
-        public Texture2D PlayerTexture;
+        public Animation PlayerAnimation;
 
         public Vector2 Position;
 
         public int Health;
 
         public int Width
-        { get { return PlayerTexture.Width; } }
+        { get { return PlayerAnimation.FrameWidth; } }
         
         public int Height
-        { get { return PlayerTexture.Height; } }
+        { get { return PlayerAnimation.FrameHeight; } }
 
-        public void Initialize(Texture2D playerTexture, Vector2 playerPosition)
+        public void Initialize(Animation animation, Vector2 position)
         {
-            PlayerTexture = playerTexture;
-            Position = playerPosition;
+            PlayerAnimation = animation;
 
+
+            Position = position;
             Active = true;
             Health = 100;
         }
 
-        public void Update()
+        public void Update(GameTime gameTime)
         {
-            
+            PlayerAnimation.Position = Position;
+            PlayerAnimation.Update(gameTime);
         }
 
         public void Draw(SpriteBatch spriteBatch)
         {
-            spriteBatch.Draw(PlayerTexture, Position, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None,
-                             0f);
+            PlayerAnimation.Draw(spriteBatch);
         }
     }
 }

File ShooterTutorial/ShooterTutorial.csproj

     </Reference>
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Animation.cs" />
     <Compile Include="App.xaml.cs">
       <DependentUpon>App.xaml</DependentUpon>
     </Compile>