Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Input;
- namespace GameTest
- {
- public class AnimatedSprite
- {
- private AnimatedTexture[] SpriteTextures;
- public AnimatedTexture ActiveSpriteTexture;
- private Vector2 Origin { get; set; }
- private Vector2 Movement { get; set; }
- private Vector2 oldPosition;
- private float Rotation = 0;
- private float Scale = 1.0f;
- private float Depth = 0.5f;
- private const float FrictionSpeed = 0.05f;
- private const float FrictionSpeedOnGround = 0.15f;
- private const float Gravity = 0.65f;
- private const int JumpHeight = 20;
- private const bool UpKeyToJump = true;
- public AnimatedSprite(Vector2 position)
- {
- Origin = position;
- }
- public AnimatedSprite(Vector2 position, float rotation, float scale, float depth)
- {
- Origin = position;
- Rotation = rotation;
- Scale = scale;
- Depth = depth;
- }
- public void Load(ArrayList[] textures, ContentManager content)
- {
- ActiveSpriteTexture = new AnimatedTexture(Origin, Rotation, Scale, Depth);
- ActiveSpriteTexture.Load(content, textures[0][0].ToString(), (int)textures[0][1], (int)textures[0][2]);
- }
- public void Update(GameTime gameTime)
- {
- CheckKeyboardAndUpdateMovement();
- //AffectWithGravity();
- //SimulateFriction();
- //MoveAsFarAsPossible(gameTime);
- //StopMovingIfBlocked();
- /* I commented all these function because they where based on a single texture, but now I've a .dds and it's completely different... */
- ActiveSpriteTexture.Position += Movement * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 15;
- float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
- ActiveSpriteTexture.UpdateFrame(elapsed);
- }
- private void CheckKeyboardAndUpdateMovement()
- {
- KeyboardState keyboardState = Keyboard.GetState();
- if (keyboardState.IsKeyDown(Keys.Left)) { Movement -= Vector2.UnitX; }
- if (keyboardState.IsKeyDown(Keys.Right)) { Movement += Vector2.UnitX; }
- if ((keyboardState.IsKeyDown(Keys.Space) || keyboardState.IsKeyDown(Keys.Up)) && IsOnFirmGround())
- {
- Movement = -Vector2.UnitY * JumpHeight;
- }
- }
- private void AffectWithGravity()
- {
- Movement += Vector2.UnitY * Gravity;
- }
- private void SimulateFriction()
- {
- if (IsOnFirmGround()) { Movement -= Movement * Vector2.One * FrictionSpeedOnGround; }
- else { Movement -= Movement * Vector2.One * FrictionSpeed; }
- }
- private void MoveAsFarAsPossible(GameTime gameTime)
- {
- oldPosition = ActiveSpriteTexture.Position;
- UpdatePositionBasedOnMovement(gameTime);
- ActiveSpriteTexture.Position = Board.CurrentBoard.WhereCanIGetTo(oldPosition, ActiveSpriteTexture.Position, Bounds);
- }
- private void UpdatePositionBasedOnMovement(GameTime gameTime)
- {
- ActiveSpriteTexture.Position += Movement * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 15;
- }
- public bool IsOnFirmGround()
- {
- Rectangle onePixelLower = Bounds;
- onePixelLower.Offset(0, 1);
- return !Board.CurrentBoard.HasRoomForRectangle(onePixelLower);
- }
- private void StopMovingIfBlocked()
- {
- Vector2 lastMovement = ActiveSpriteTexture.Position - oldPosition;
- if (lastMovement.X == 0) { Movement *= Vector2.UnitY; }
- if (lastMovement.Y == 0) { Movement *= Vector2.UnitX; }
- }
- public Rectangle Bounds
- {
- get
- {
- return new Rectangle((int)ActiveSpriteTexture.Position.X, (int)ActiveSpriteTexture.Position.Y, ActiveSpriteTexture.myTexture.Width, ActiveSpriteTexture.myTexture.Height);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement