Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Shadow
- {
- class AnimetedSprite : Sprite
- {
- public AnimetedSprite(Vector2 position, Texture2D texture, Point FrameSize) : base(position, texture)
- {
- this.sourceRectangle.Width = FrameSize.X;
- sourceRectangle.Height = FrameSize.Y;
- dictAnimation = new Dictionary<string, int[]>();
- framePerSecond = 1;
- currentFrame = 0;
- animationIsFinish = false;
- }
- Dictionary<string, int[]> dictAnimation;
- string currentAnimation;
- float framePerSecond;
- int currentFrame;
- float chrono;
- bool isLooping;
- bool animationIsFinish;
- public void AddAnimation(string animationName, int[] tabAnimation)
- {
- if (!dictAnimation.ContainsKey(animationName))
- {
- dictAnimation.Add(animationName, tabAnimation);
- }
- else
- {
- throw new Exception("L'animation " + animationName + " existe deja.");
- }
- }
- public bool AnimationExist(string animationName)
- {
- return dictAnimation.ContainsKey(animationName);
- }
- public void PlayAnimation(string animationName, float framePerSecond = 1, bool isLooping = false)
- {
- animationIsFinish = false;
- this.isLooping = isLooping;
- currentFrame = 0;
- this.framePerSecond = framePerSecond;
- currentAnimation = animationName;
- if (!dictAnimation.ContainsKey(animationName))
- {
- throw new Exception("L'animation " + animationName + " n'existe pas.");
- }
- UpdateFrame();
- }
- private void UpdateFrame()
- {
- int nbFrameInTextureWidth = texture.Width / sourceRectangle.Width;
- int positionY = dictAnimation[currentAnimation][currentFrame] / nbFrameInTextureWidth;
- sourceRectangle.Y = positionY * sourceRectangle.Height;
- int positionX = dictAnimation[currentAnimation][currentFrame] - positionY * nbFrameInTextureWidth;
- sourceRectangle.X = positionX * sourceRectangle.Width;
- }
- public override void Update(GameTime gameTime)
- {
- base.Update(gameTime);
- #region gestionAnimation
- chrono += (float)gameTime.ElapsedGameTime.TotalSeconds;
- if (!animationIsFinish && chrono >= framePerSecond)
- {
- if (currentFrame + 1 < dictAnimation[currentAnimation].Length)
- {
- currentFrame += 1;
- UpdateFrame();
- }
- else
- {
- if (isLooping)
- {
- currentFrame = 0;
- UpdateFrame();
- }
- else
- {
- animationIsFinish = true;
- }
- }
- chrono = 0;
- }
- #endregion
- }
- public override void Draw(SpriteBatch spriteBatch)
- {
- base.Draw(spriteBatch);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement