/src/Graphics/Animation.cs
C# | 220 lines | 179 code | 33 blank | 8 comment | 17 complexity | f53fd07027f3f17a14dfc79b2b36a8fd MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Audio;
-
- namespace TheFat
- {
- public class SoundEffectArgs : EventArgs
- {
- public string SoundName
- {
- get;
- protected set;
- }
-
- public Cue Sound
- {
- get;
- set;
- }
-
- public SoundEffectArgs(string soundName) {
- SoundName = soundName;
- }
- }
-
- class Animation : ICloneable
- {
- public String animationName;
- //public string myName;
- List<string> assetNames;
- List<Texture2D> frames;
- double playTime;
- int currentFrameIndex;
- bool frameJustChanged;
- bool animationJustChanged;
- bool animationJustRestarted;
-
- public event EventHandler<SoundEffectArgs> SoundEffect;
-
- public string Sound
- {
- get;
- set;
- }
-
- public List<KeyValuePair<int, string>> FrameSounds
- {
- get;
- set;
- }
-
- public Boolean Finished
- {
- get {
- return (playTime / FrameDelay > frames.Count);
- }
- }
-
- public Boolean Repeat
- {
- get;
- set;
- }
-
-
- public float FrameDelay
- {
- get;
- set;
- }
-
- public Texture2D CurrentFrame
- {
- get;
- protected set;
- }
-
- public int CurrentFrameIndex
- {
- get { return currentFrameIndex;}
- protected set
- {
- currentFrameIndex = value;
- CurrentFrame = frames[value];
- }
- }
-
-
- public Animation(IEnumerable<string> assetNamesParam, float frameDelay)
- {
- this.assetNames = assetNamesParam.ToList();
- this.FrameDelay = frameDelay;
- this.frames = new List<Texture2D>(this.assetNames.Count);
- this.frameJustChanged = true;
- this.animationJustChanged = true;
- this.animationJustRestarted = true;
- FrameSounds = new List<KeyValuePair<int, string>>();
-
- Repeat = false;
- }
-
- public Animation(List<string> assetNamesParam, float frameDelay, List<KeyValuePair<int, string>> frameSounds)
- {
- this.assetNames = assetNamesParam;
- this.FrameDelay = frameDelay;
- this.frames = new List<Texture2D>(this.assetNames.Count);
- this.FrameSounds = frameSounds;
- this.frameJustChanged = true;
- this.animationJustChanged = true;
- this.animationJustRestarted = true;
-
- Repeat = false;
- }
-
- public Animation(List<string> assetNamesParam, float frameDelay, string sound)
- {
- this.assetNames = assetNamesParam;
- this.FrameDelay = frameDelay;
- this.frames = new List<Texture2D>(this.assetNames.Count);
- this.Sound = sound;
- this.frameJustChanged = true;
- this.animationJustChanged = true;
- this.animationJustRestarted = true;
- FrameSounds = new List<KeyValuePair<int, string>>();
-
- Repeat = false;
- }
-
-
- public void LoadContent(ContentManager content)
- {
- foreach (string asset in assetNames)
- {
- frames.Add(content.Load<Texture2D>(asset));
- }
- CurrentFrameIndex = 0;
- }
-
- public void Update(GameTime gameTime)
- {
- playTime += gameTime.ElapsedGameTime.TotalMilliseconds;
- int index = (int)(playTime / FrameDelay) % frames.Count;
- int frameIndex = 0;
-
- //condicion de repetir animacion
- //determina el frame actual correspondiente
- if (!this.Repeat && (playTime / FrameDelay) > frames.Count)
- {
- frameIndex = frames.Count - 1;
- }
- else frameIndex = index;
-
- if (CurrentFrameIndex != frameIndex)
- {
- frameJustChanged = true;
- if (CurrentFrameIndex > frameIndex)
- {
- this.animationJustRestarted = true;
- }
- }
-
-
- //frames
- if (frameJustChanged)
- {
- //sonido
- if (FrameSounds != null)
- {
- for (int i = CurrentFrameIndex; i <= frameIndex; i++)
- {
- foreach (var pair in FrameSounds.Where(pair => pair.Key == i))
- {
- SoundEffect(this, new SoundEffectArgs(pair.Value));
- }
- }
- }
-
- //grafica
- CurrentFrameIndex = frameIndex;
-
- frameJustChanged = false;
- }
-
- //animacion
- if (animationJustRestarted)
- {
- //sonido
- if (Sound != null)
- {
- if (SoundEffect != null)
- {
- SoundEffect(this, new SoundEffectArgs(Sound));
- }
- }
-
- animationJustRestarted = false;
- }
-
- if (animationJustChanged)
- {
- animationJustChanged = false;
- }
- }
- public Object Clone()
- {
- Animation toReturn = (Animation)this.MemberwiseClone();
- return toReturn;
-
- }
- public void Reset()
- {
- playTime = 0;
- }
- }
- }