aboutsummaryrefslogtreecommitdiff
path: root/V3/Effects/AbstractEffect.cs
diff options
context:
space:
mode:
Diffstat (limited to 'V3/Effects/AbstractEffect.cs')
-rw-r--r--V3/Effects/AbstractEffect.cs74
1 files changed, 74 insertions, 0 deletions
diff --git a/V3/Effects/AbstractEffect.cs b/V3/Effects/AbstractEffect.cs
new file mode 100644
index 0000000..c3468c0
--- /dev/null
+++ b/V3/Effects/AbstractEffect.cs
@@ -0,0 +1,74 @@
+using System;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Audio;
+using Microsoft.Xna.Framework.Content;
+using Microsoft.Xna.Framework.Graphics;
+using V3.Data;
+
+namespace V3.Effects
+{
+ public abstract class AbstractEffect : IEffect
+ {
+ private Texture2D mTexture;
+ private int mSpritesPerRow;
+ private int mTotalSprites;
+ private int mAnimationState;
+ private Rectangle mAnimationRectangle;
+ private SoundEffect mSoundEffect;
+ private SoundEffectInstance mSoundEffectInstance;
+
+ public bool IsPlaying { get; private set; }
+ protected virtual UpdatesPerSecond UpdatesPerSecond { get; } = new UpdatesPerSecond(24);
+ protected abstract string TextureFile { get; }
+ protected virtual Point SpriteSize { get; } = new Point(128, 128);
+ protected virtual string SoundFile { get; } = String.Empty;
+
+ public void LoadContent(ContentManager contentManager)
+ {
+ mTexture = contentManager.Load<Texture2D>("Effects/" + TextureFile);
+ mSpritesPerRow = mTexture.Width / SpriteSize.X;
+ mTotalSprites = mSpritesPerRow * (mTexture.Height / SpriteSize.Y);
+ if (SoundFile.Length != 0)
+ {
+ mSoundEffect = contentManager.Load<SoundEffect>("Sounds/" + SoundFile);
+ mSoundEffectInstance = mSoundEffect.CreateInstance();
+ }
+ }
+
+ public void PlayOnce(Point position, Point size, IOptionsManager optionsManager)
+ {
+ mAnimationRectangle = new Rectangle(position - size / new Point(2, 2), size);
+ IsPlaying = true;
+ if (SoundFile.Length != 0)
+ {
+ mSoundEffectInstance.Volume = optionsManager.Options.GetEffectiveVolume();
+ mSoundEffectInstance.Play();
+ }
+ }
+
+ public void Update(GameTime gameTime)
+ {
+ if (!IsPlaying) return;
+ if (UpdatesPerSecond.IsItTime(gameTime))
+ {
+ if (mAnimationState < mTotalSprites)
+ {
+ mAnimationState++;
+ }
+ else
+ {
+ mAnimationState = 0;
+ IsPlaying = false;
+ }
+ }
+ }
+
+ public void Draw(SpriteBatch spriteBatch)
+ {
+ if (!IsPlaying) return;
+ spriteBatch.Draw(mTexture, mAnimationRectangle,
+ new Rectangle(new Point(mAnimationState % mSpritesPerRow * SpriteSize.X, mAnimationState / mSpritesPerRow * SpriteSize.Y), SpriteSize),
+ Color.White);
+ }
+ }
+} \ No newline at end of file