|
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SkyWar
{
class AnimatedSprite
{
Texture2D t2dTexture;
float fFrameRate = 0.02f;
float fElapsed = 0.0f;
int iFrameOffsetX = 0;
int iFrameOffsetY = 0;
int iFrameWidth = 32;
int iFrameHeight = 32;
int iFrameCount = 1;
int iCurrentFrame = 0;
int iScreenX = 0;
int iScreenY = 0;
public bool IsActive { get; set; }
public bool OneShotAnimation { get; set; }
public bool DeactivateOnAnimationOver { get; set; }
Color cTinting = Color.White;
public Color Tint
{
get { return cTinting; }
set { cTinting = value; }
}
bool bAnimating = true;
public int X
{
get { return iScreenX; }
set { iScreenX = value; }
}
public int Y
{
get { return iScreenY; }
set { iScreenY = value; }
}
public int Frame
{
get { return iCurrentFrame; }
set { iCurrentFrame = (int)MathHelper.Clamp(value, 0, iFrameCount); }
}
public float FrameLength
{
get { return fFrameRate; }
set { fFrameRate = (float)Math.Max(value, 0f); }
}
public bool IsAnimating
{
get { return bAnimating; }
set { bAnimating = value; }
}
public AnimatedSprite(Texture2D texture, int FrameOffsetX, int FrameOffsetY, int FrameWidth, int FrameHeight, int FrameCount)
{
t2dTexture = texture;
iFrameOffsetX = FrameOffsetX;
iFrameOffsetY = FrameOffsetY;
iFrameWidth = FrameWidth;
iFrameHeight = FrameHeight;
iFrameCount = FrameCount;
IsActive = true;
}
public Rectangle GetSourceRect()
{
return new Rectangle(iFrameOffsetX + (iFrameWidth * iCurrentFrame), iFrameOffsetY, iFrameWidth, iFrameHeight);
}
public void Update(GameTime gameTime)
{
if (IsActive)
{
if (bAnimating)
{
// Accumulate elapsed time...
fElapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;
// Until it passes our frame length
if (fElapsed > fFrameRate)
{
// Increment the current frame, wrapping back to 0 at iFrameCount
iCurrentFrame = (iCurrentFrame + 1) % iFrameCount;
if (iCurrentFrame >= iFrameCount - 1 && OneShotAnimation && DeactivateOnAnimationOver)
IsActive = false;
// Reset the elapsed frame time.
fElapsed = 0.0f;
}
}
}
}
public void Draw(SpriteBatch spriteBatch, int XOffset, int YOffset, bool NeedBeginEnd)
{
//if (NeedBeginEnd)
// spriteBatch.Begin();
if (IsActive)
spriteBatch.Draw(t2dTexture, new Rectangle(iScreenX + XOffset, iScreenY + YOffset, iFrameWidth, iFrameHeight), GetSourceRect(), cTinting);
//if (NeedBeginEnd)
// spriteBatch.End();
}
public void Draw(SpriteBatch spriteBatch, int XOffset, int YOffset)
{
Draw(spriteBatch, XOffset, YOffset, true);
}
public void Draw(SpriteBatch spriteBatch, Rectangle boundingbox, bool NeedBeginEnd)
{
//if (NeedBeginEnd)
// spriteBatch.Begin();
if (IsActive)
spriteBatch.Draw(t2dTexture, boundingbox, GetSourceRect(), cTinting);
//if (NeedBeginEnd)
// spriteBatch.End();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
2+ year of experience in Information Technology with the extensive exposure of Treasury & Capital, Health Care and ERP domain.
Area of interest - Multithreading, Files, WPF, WCF, Jquery, Mvc, Sql Server, Perceptual programming
Heavily Worked on desktop application ,Web application and WCF Services.
Also worked on small games application for learning purpose.
Latest interest - Windows 8 , Perceptual Programming, Windows Phone, WCF etc