daily pastebin goal
22%
SHARE
TWEET

AnimatedSprite.cs

dallonz88 Dec 9th, 2013 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Content;
  5. using Microsoft.Xna.Framework.Input;
  6.  
  7. namespace GameTest
  8. {
  9.     public class AnimatedSprite
  10.     {
  11.         private AnimatedTexture[] SpriteTextures;
  12.         public AnimatedTexture ActiveSpriteTexture;
  13.  
  14.         private Vector2 Origin { get; set; }
  15.         private Vector2 Movement { get; set; }
  16.         private Vector2 oldPosition;
  17.         private float Rotation = 0;
  18.         private float Scale = 1.0f;
  19.         private float Depth = 0.5f;
  20.  
  21.         private const float FrictionSpeed = 0.05f;
  22.         private const float FrictionSpeedOnGround = 0.15f;
  23.         private const float Gravity = 0.65f;
  24.         private const int JumpHeight = 20;
  25.         private const bool UpKeyToJump = true;
  26.  
  27.         public AnimatedSprite(Vector2 position)
  28.         {
  29.             Origin = position;
  30.         }
  31.  
  32.         public AnimatedSprite(Vector2 position, float rotation, float scale, float depth)
  33.         {
  34.             Origin = position;
  35.             Rotation = rotation;
  36.             Scale = scale;
  37.             Depth = depth;
  38.         }
  39.  
  40.         public void Load(ArrayList[] textures, ContentManager content)
  41.         {
  42.             ActiveSpriteTexture = new AnimatedTexture(Origin, Rotation, Scale, Depth);
  43.             ActiveSpriteTexture.Load(content, textures[0][0].ToString(), (int)textures[0][1], (int)textures[0][2]);
  44.         }
  45.  
  46.         public void Update(GameTime gameTime)
  47.         {
  48.             CheckKeyboardAndUpdateMovement();
  49.             //AffectWithGravity();
  50.             //SimulateFriction();
  51.             //MoveAsFarAsPossible(gameTime);
  52.             //StopMovingIfBlocked();
  53. /* I commented all these function because they where based on a single texture, but now I've a .dds and it's completely different... */
  54.             ActiveSpriteTexture.Position += Movement * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 15;
  55.             float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  56.             ActiveSpriteTexture.UpdateFrame(elapsed);
  57.         }
  58.  
  59.         private void CheckKeyboardAndUpdateMovement()
  60.         {
  61.             KeyboardState keyboardState = Keyboard.GetState();
  62.             if (keyboardState.IsKeyDown(Keys.Left)) { Movement -= Vector2.UnitX; }
  63.             if (keyboardState.IsKeyDown(Keys.Right)) { Movement += Vector2.UnitX; }
  64.             if ((keyboardState.IsKeyDown(Keys.Space) || keyboardState.IsKeyDown(Keys.Up)) && IsOnFirmGround())
  65.             {
  66.                 Movement = -Vector2.UnitY * JumpHeight;
  67.             }
  68.         }
  69.  
  70.         private void AffectWithGravity()
  71.         {
  72.             Movement += Vector2.UnitY * Gravity;
  73.         }
  74.  
  75.         private void SimulateFriction()
  76.         {
  77.             if (IsOnFirmGround()) { Movement -= Movement * Vector2.One * FrictionSpeedOnGround; }
  78.             else { Movement -= Movement * Vector2.One * FrictionSpeed; }
  79.         }
  80.  
  81.         private void MoveAsFarAsPossible(GameTime gameTime)
  82.         {
  83.             oldPosition = ActiveSpriteTexture.Position;
  84.             UpdatePositionBasedOnMovement(gameTime);
  85.             ActiveSpriteTexture.Position = Board.CurrentBoard.WhereCanIGetTo(oldPosition, ActiveSpriteTexture.Position, Bounds);
  86.         }
  87.  
  88.         private void UpdatePositionBasedOnMovement(GameTime gameTime)
  89.         {
  90.             ActiveSpriteTexture.Position += Movement * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 15;
  91.         }
  92.  
  93.         public bool IsOnFirmGround()
  94.         {
  95.             Rectangle onePixelLower = Bounds;
  96.             onePixelLower.Offset(0, 1);
  97.             return !Board.CurrentBoard.HasRoomForRectangle(onePixelLower);
  98.         }
  99.  
  100.         private void StopMovingIfBlocked()
  101.         {
  102.             Vector2 lastMovement = ActiveSpriteTexture.Position - oldPosition;
  103.             if (lastMovement.X == 0) { Movement *= Vector2.UnitY; }
  104.             if (lastMovement.Y == 0) { Movement *= Vector2.UnitX; }
  105.         }
  106.  
  107.         public Rectangle Bounds
  108.         {
  109.             get
  110.             {
  111.                 return new Rectangle((int)ActiveSpriteTexture.Position.X, (int)ActiveSpriteTexture.Position.Y, ActiveSpriteTexture.myTexture.Width, ActiveSpriteTexture.myTexture.Height);
  112.             }
  113.         }
  114.     }
  115. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top